Java如何获取系统cpu、内存、硬盘信息-程序员宅基地

技术标签: java  操作系统  runtime  

1 概述

  前段时间摸索在Java中怎么获取系统信息包括cpu、内存、硬盘信息等,刚开始使用Java自带的包进行获取,但这样获取的内存信息不够准确并且容易出现找不到相应包等错误,所以后面使用sigar插件进行获取。下面列举出了这两种方式获取系统信息的方式及代码。

2 使用Java自带包获取系统信息

2.1 使用Java自带包获取系统信息代码如下:

2.1.1 Bytes.java

复制代码
public class Bytes {
public static String substring(String src, int start_idx, int end_idx){
byte[] b = src.getBytes();
String tgt = "";
for(int i=start_idx; i<=end_idx; i++){
tgt +=(char)b[i];
}
return tgt;
}
}
复制代码

2.1.2 IMonitorService.java

public interface IMonitorService {
public MonitorInfoBean getMonitorInfoBean() throws Exception;
}

2.1.3 MonitorInfoBean.java

复制代码
public class MonitorInfoBean {
private long totalMemory;
private long freeMemory;
private long maxMemory;
private String osName;
private long totalMemorySize;
private long freePhysicalMemorySize;
private long usedMemory;
private int totalThread;
private double cpuRatio;

public long getFreeMemory() {
    return freeMemory;
}

public void setFreeMemory(long freeMemory) {
    this.freeMemory = freeMemory;
}

public long getFreePhysicalMemorySize() {
    return freePhysicalMemorySize;
}

public void setFreePhysicalMemorySize(long freePhysicalMemorySize) {
    this.freePhysicalMemorySize = freePhysicalMemorySize;
}

public long getMaxMemory() {
    return maxMemory;
}

public void setMaxMemory(long maxMemory) {
    this.maxMemory = maxMemory;
}

public String getOsName() {
    return osName;
}

public void setOsName(String osName) {
    this.osName = osName;
}

public long getTotalMemory() {
    return totalMemory;
}

public void setTotalMemory(long totalMemory) {
    this.totalMemory = totalMemory;
}

public long getTotalMemorySize() {
    return totalMemorySize;
}

public void setTotalMemorySize(long totalMemorySize) {
    this.totalMemorySize = totalMemorySize;
}

public int getTotalThread() {
    return totalThread;
}

public void setTotalThread(int totalThread) {
    this.totalThread = totalThread;
}

public long getUsedMemory() {
    return usedMemory;
}

public void setUsedMemory(long usedMemory) {
    this.usedMemory = usedMemory;
}

public double getCpuRatio() {
    return cpuRatio;
}

public void setCpuRatio(double cpuRatio) {
    this.cpuRatio = cpuRatio;
}

}
复制代码
2.1.4

复制代码
import java.io.InputStreamReader;
import java.io.LineNumberReader;

//import sun.management.ManagementFactory;
//import com.sun.management.OperatingSystemMXBean;
import java.io.*;
import java.lang.management.ManagementFactory;
import java.util.StringTokenizer;

public class MonitorServiceImpl implements IMonitorService {
private static final int CPUTIME = 30;
private static final int PERCENT = 100;
private static final int FAULTLENGTH = 10;
private static final File versionFile = new File("/proc/version");
private static String linuxVersion = null;

public MonitorInfoBean getMonitorInfoBean() throws Exception {
    int kb = 1024;
    long totalMemory = Runtime.getRuntime().totalMemory() / kb;
    long freeMemory = Runtime.getRuntime().freeMemory() / kb;
    long maxMemory = Runtime.getRuntime().maxMemory() / kb;

    // OperatingSystemMXBean osmxb = (OperatingSystemMXBean)
    // ManagementFactory
    // .getOperatingSystemMXBean();
    // String osName = System.getProperty("os.name");
    // long totalMemorySize = osmxb.getTotalPhysicalMemorySize() / kb;
    // long freePhysicalMemorySize = osmxb.getFreePhysicalMemorySize() / kb;
    // long usedMemory = (osmxb.getTotalPhysicalMemorySize() - osmxb
    // .getFreePhysicalMemorySize()) / kb;

    ThreadGroup parentThread;
    for (parentThread = Thread.currentThread().getThreadGroup(); parentThread.getParent() != null; parentThread = parentThread.getParent());
    int totalThread = parentThread.activeCount();
    double cpuRatio = 0;

    // if (osName.toLowerCase().startsWith("windows")) {
    // cpuRatio = this.getCpuRatioForWindows();
    // } else {
    // cpuRatio = this.getCpuRateForLinux();
    // }

    MonitorInfoBean infoBean = new MonitorInfoBean();
    infoBean.setFreeMemory(freeMemory);
    // infoBean.setFreePhysicalMemorySize(freePhysicalMemorySize);
    infoBean.setMaxMemory(maxMemory);
    // infoBean.setOsName(osName);
    infoBean.setTotalMemory(totalMemory);
    // infoBean.setTotalMemorySize(totalMemorySize);
    infoBean.setTotalThread(totalThread);
    // infoBean.setUsedMemory(usedMemory);
    infoBean.setCpuRatio(cpuRatio);
    return infoBean;
}

private static double getCpuRateForLinux() {
    InputStream is = null;
    InputStreamReader isr = null;
    BufferedReader brStat = null;
    StringTokenizer tokenStat = null;
    try {

        System.out.println("Get usage rate of CUP , linux version: " + linuxVersion);

        Process process = Runtime.getRuntime().exec("top -b -n 1");
        is = process.getInputStream();
        isr = new InputStreamReader(is);
        brStat = new BufferedReader(isr);

        if (linuxVersion.equals("2.4")) {
            brStat.readLine();
            brStat.readLine();
            brStat.readLine();
            brStat.readLine();
            tokenStat = new StringTokenizer(brStat.readLine());
            tokenStat.nextToken();
            tokenStat.nextToken();

            String user = tokenStat.nextToken();

            tokenStat.nextToken();
            String system = tokenStat.nextToken();
            tokenStat.nextToken();
            String nice = tokenStat.nextToken();
            System.out.println(user + " , " + system + " , " + nice);
            user = user.substring(0, user.indexOf("%"));
            system = system.substring(0, system.indexOf("%"));
            nice = nice.substring(0, nice.indexOf("%"));
            float userUsage = new Float(user).floatValue();
            float systemUsage = new Float(system).floatValue();
            float niceUsage = new Float(nice).floatValue();

            return (userUsage + systemUsage + niceUsage) / 100;

        } else {
            brStat.readLine();
            brStat.readLine();
            tokenStat = new StringTokenizer(brStat.readLine());
            tokenStat.nextToken();
            tokenStat.nextToken();
            tokenStat.nextToken();
            tokenStat.nextToken();
            tokenStat.nextToken();
            tokenStat.nextToken();
            tokenStat.nextToken();
            String cpuUsage = tokenStat.nextToken();
            System.out.println("CPU idle : " + cpuUsage);
            Float usage = new Float(cpuUsage.substring(0, cpuUsage.indexOf("%")));
            return (1 - usage.floatValue() / 100);
        }
    } catch (IOException ioe) {
        System.out.println(ioe.getMessage());
        freeResource(is, isr, brStat);
        return 1;
    } finally {
        freeResource(is, isr, brStat);
    }
}

private static void freeResource(InputStream is, InputStreamReader isr, BufferedReader br) {
    try {
        if (is != null)
            is.close();
        if (isr != null)
            isr.close();
        if (br != null)
            br.close();
    } catch (IOException ioe) {
        System.out.println(ioe.getMessage());
    }
}

private double getCpuRatioForWindows() {
    try {
        String procCmd = System.getenv("windir")
                + "\\system32\\wbem\\wmic.exe process get Caption,CommandLine,"
                + "KernelModeTime,ReadOperationCount,ThreadCount,UserModeTime,WriteOperationCount";
        long[] c0 = readCpu(Runtime.getRuntime().exec(procCmd));
        Thread.sleep(CPUTIME);
        long[] c1 = readCpu(Runtime.getRuntime().exec(procCmd));
        if (c0 != null && c1 != null) {
            long idletime = c1[0] - c0[0];
            long busytime = c1[1] - c0[1];
            return Double.valueOf(
                    PERCENT * (busytime) / (busytime + idletime)).doubleValue();
        } else {
            return 0.0;
        }
    } catch (Exception ex) {
        ex.printStackTrace();
        return 0.0;
    }
}

private long[] readCpu(final Process proc) {

    long[] retn = new long[2];
    try {
        proc.getOutputStream().close();
        InputStreamReader ir = new InputStreamReader(proc.getInputStream());
        LineNumberReader input = new LineNumberReader(ir);
        String line = input.readLine();

        if (line == null || line.length() < FAULTLENGTH) {
            return null;
        }

        int capidx = line.indexOf("Caption");
        int cmdidx = line.indexOf("CommandLine");
        int rocidx = line.indexOf("ReadOperationCount");
        int umtidx = line.indexOf("UserModeTime");
        int kmtidx = line.indexOf("KernelModeTime");
        int wocidx = line.indexOf("WriteOperationCount");
        long idletime = 0;
        long kneltime = 0;
        long usertime = 0;

        while ((line = input.readLine()) != null) {
            if (line.length() < wocidx) {
                continue;
            }

            String caption = Bytes.substring(line, capidx, cmdidx - 1) .trim();
            String cmd = Bytes.substring(line, cmdidx, kmtidx - 1).trim();
            if (cmd.indexOf("wmic.exe") >= 0) {
                continue;
            }

            // log.info("line="+line);
            if (caption.equals("System Idle Process") || caption.equals("System")) {
                idletime += Long.valueOf(
                        Bytes.substring(line, kmtidx, rocidx - 1).trim()).longValue();
                idletime += Long.valueOf(
                        Bytes.substring(line, umtidx, wocidx - 1).trim()).longValue();
                continue;
            }

            kneltime += Long.valueOf(
                    Bytes.substring(line, kmtidx, rocidx - 1).trim()).longValue();
            usertime += Long.valueOf(
                    Bytes.substring(line, umtidx, wocidx - 1).trim()).longValue();
        }
        retn[0] = idletime;
        retn[1] = kneltime + usertime;
        return retn;
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        try {
            proc.getInputStream().close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return null;
}

public static void main(String[] args) throws Exception {
    IMonitorService service = new MonitorServiceImpl();
    MonitorInfoBean monitorInfo = service.getMonitorInfoBean();
    System.out.println("cpu percent: " + monitorInfo.getCpuRatio());
    System.out.println("can use memory: " + monitorInfo.getTotalMemory());
    System.out.println("ideal memory: " + monitorInfo.getFreeMemory());
    System.out.println("largest memory: " + monitorInfo.getMaxMemory());
    System.out.println("all memory: " + monitorInfo.getTotalMemorySize() + "kb");
    System.out.println("ideal memory: " + monitorInfo.getFreeMemory() + "kb");
    System.out.println("used memory: " + monitorInfo.getUsedMemory() + "kb");
    System.out.println("thread num: " + monitorInfo.getTotalThread() + "kb");
}

}
复制代码
2.2 执行结果如下图所示:

3 使用sigar获取系统信息

3.1 下载安装sigar-1.6.4.zip

使用java自带的包获取系统数据,容易找不到包,尤其是内存信息不够准确,所以选择使用sigar获取系统信息。   

下载地址:http://sourceforge.net/projects/sigar/files/latest/download?source=files

解压压缩包,将lib下sigar.jar导入eclipse的CLASSPATH中,再将sigar-x86-winnt.dll存入Java的bin目录即可。

3.2 代码实现如下:

复制代码
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Map;
import java.util.Properties;
import org.hyperic.sigar.CpuInfo;
import org.hyperic.sigar.CpuPerc;
import org.hyperic.sigar.FileSystem;
import org.hyperic.sigar.FileSystemUsage;
import org.hyperic.sigar.Mem;
import org.hyperic.sigar.NetFlags;
import org.hyperic.sigar.NetInterfaceConfig;
import org.hyperic.sigar.NetInterfaceStat;
import org.hyperic.sigar.OperatingSystem;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;
import org.hyperic.sigar.Swap;
import org.hyperic.sigar.Who;

public class RuntimeTest {
public static void main(String[] args) {
try {
// System信息,从jvm获取
property();
System.out.println("----------------------------------");
// cpu信息
cpu();
System.out.println("----------------------------------");
// 内存信息
memory();
System.out.println("----------------------------------");
// 操作系统信息
os();
System.out.println("----------------------------------");
// 用户信息
who();
System.out.println("----------------------------------");
// 文件系统信息
file();
System.out.println("----------------------------------");
// 网络信息
net();
System.out.println("----------------------------------");
// 以太网信息
ethernet();
System.out.println("----------------------------------");
} catch (Exception e1) {
e1.printStackTrace();
}
}

private static void property() throws UnknownHostException {
    Runtime r = Runtime.getRuntime();
    Properties props = System.getProperties();
    InetAddress addr;
    addr = InetAddress.getLocalHost();
    String ip = addr.getHostAddress();
    Map<String, String> map = System.getenv();
    String userName = map.get("USERNAME");// 获取用户名
    String computerName = map.get("COMPUTERNAME");// 获取计算机名
    String userDomain = map.get("USERDOMAIN");// 获取计算机域名
    System.out.println("用户名:    " + userName);
    System.out.println("计算机名:    " + computerName);
    System.out.println("计算机域名:    " + userDomain);
    System.out.println("本地ip地址:    " + ip);
    System.out.println("本地主机名:    " + addr.getHostName());
    System.out.println("JVM可以使用的总内存:    " + r.totalMemory());
    System.out.println("JVM可以使用的剩余内存:    " + r.freeMemory());
    System.out.println("JVM可以使用的处理器个数:    " + r.availableProcessors());
    System.out.println("Java的运行环境版本:    " + props.getProperty("java.version"));
    System.out.println("Java的运行环境供应商:    " + props.getProperty("java.vendor"));
    System.out.println("Java供应商的URL:    " + props.getProperty("java.vendor.url"));
    System.out.println("Java的安装路径:    " + props.getProperty("java.home"));
    System.out.println("Java的虚拟机规范版本:    " + props.getProperty("java.vm.specification.version"));
    System.out.println("Java的虚拟机规范供应商:    " + props.getProperty("java.vm.specification.vendor"));
    System.out.println("Java的虚拟机规范名称:    " + props.getProperty("java.vm.specification.name"));
    System.out.println("Java的虚拟机实现版本:    " + props.getProperty("java.vm.version"));
    System.out.println("Java的虚拟机实现供应商:    " + props.getProperty("java.vm.vendor"));
    System.out.println("Java的虚拟机实现名称:    " + props.getProperty("java.vm.name"));
    System.out.println("Java运行时环境规范版本:    " + props.getProperty("java.specification.version"));
    System.out.println("Java运行时环境规范供应商:    " + props.getProperty("java.specification.vender"));
    System.out.println("Java运行时环境规范名称:    " + props.getProperty("java.specification.name"));
    System.out.println("Java的类格式版本号:    " + props.getProperty("java.class.version"));
    System.out.println("Java的类路径:    " + props.getProperty("java.class.path"));
    System.out.println("加载库时搜索的路径列表:    " + props.getProperty("java.library.path"));
    System.out.println("默认的临时文件路径:    " + props.getProperty("java.io.tmpdir"));
    System.out.println("一个或多个扩展目录的路径:    " + props.getProperty("java.ext.dirs"));
    System.out.println("操作系统的名称:    " + props.getProperty("os.name"));
    System.out.println("操作系统的构架:    " + props.getProperty("os.arch"));
    System.out.println("操作系统的版本:    " + props.getProperty("os.version"));
    System.out.println("文件分隔符:    " + props.getProperty("file.separator"));
    System.out.println("路径分隔符:    " + props.getProperty("path.separator"));
    System.out.println("行分隔符:    " + props.getProperty("line.separator"));
    System.out.println("用户的账户名称:    " + props.getProperty("user.name"));
    System.out.println("用户的主目录:    " + props.getProperty("user.home"));
    System.out.println("用户的当前工作目录:    " + props.getProperty("user.dir"));
}

private static void memory() throws SigarException {
    Sigar sigar = new Sigar();
    Mem mem = sigar.getMem();
    // 内存总量
    System.out.println("内存总量:    " + mem.getTotal() / 1024L + "K av");
    // 当前内存使用量
    System.out.println("当前内存使用量:    " + mem.getUsed() / 1024L + "K used");
    // 当前内存剩余量
    System.out.println("当前内存剩余量:    " + mem.getFree() / 1024L + "K free");
    Swap swap = sigar.getSwap();
    // 交换区总量
    System.out.println("交换区总量:    " + swap.getTotal() / 1024L + "K av");
    // 当前交换区使用量
    System.out.println("当前交换区使用量:    " + swap.getUsed() / 1024L + "K used");
    // 当前交换区剩余量
    System.out.println("当前交换区剩余量:    " + swap.getFree() / 1024L + "K free");
}

private static void cpu() throws SigarException {
    Sigar sigar = new Sigar();
    CpuInfo infos[] = sigar.getCpuInfoList();
    CpuPerc cpuList[] = null;
    cpuList = sigar.getCpuPercList();
    for (int i = 0; i < infos.length; i++) {// 不管是单块CPU还是多CPU都适用
        CpuInfo info = infos[i];
        System.out.println("第" + (i + 1) + "块CPU信息");
        System.out.println("CPU的总量MHz:    " + info.getMhz());// CPU的总量MHz
        System.out.println("CPU生产商:    " + info.getVendor());// 获得CPU的卖主,如:Intel
        System.out.println("CPU类别:    " + info.getModel());// 获得CPU的类别,如:Celeron
        System.out.println("CPU缓存数量:    " + info.getCacheSize());// 缓冲存储器数量
        printCpuPerc(cpuList[i]);
    }
}

private static void printCpuPerc(CpuPerc cpu) {
    System.out.println("CPU用户使用率:    " + CpuPerc.format(cpu.getUser()));// 用户使用率
    System.out.println("CPU系统使用率:    " + CpuPerc.format(cpu.getSys()));// 系统使用率
    System.out.println("CPU当前等待率:    " + CpuPerc.format(cpu.getWait()));// 当前等待率
    System.out.println("CPU当前错误率:    " + CpuPerc.format(cpu.getNice()));//
    System.out.println("CPU当前空闲率:    " + CpuPerc.format(cpu.getIdle()));// 当前空闲率
    System.out.println("CPU总的使用率:    " + CpuPerc.format(cpu.getCombined()));// 总的使用率
}

private static void os() {
    OperatingSystem OS = OperatingSystem.getInstance();
    // 操作系统内核类型如: 386、486、586等x86
    System.out.println("操作系统:    " + OS.getArch());
    System.out.println("操作系统CpuEndian():    " + OS.getCpuEndian());//
    System.out.println("操作系统DataModel():    " + OS.getDataModel());//
    // 系统描述
    System.out.println("操作系统的描述:    " + OS.getDescription());
    // 操作系统类型
    // System.out.println("OS.getName():    " + OS.getName());
    // System.out.println("OS.getPatchLevel():    " + OS.getPatchLevel());//
    // 操作系统的卖主
    System.out.println("操作系统的卖主:    " + OS.getVendor());
    // 卖主名称
    System.out.println("操作系统的卖主名:    " + OS.getVendorCodeName());
    // 操作系统名称
    System.out.println("操作系统名称:    " + OS.getVendorName());
    // 操作系统卖主类型
    System.out.println("操作系统卖主类型:    " + OS.getVendorVersion());
    // 操作系统的版本号
    System.out.println("操作系统的版本号:    " + OS.getVersion());
}

private static void who() throws SigarException {
    Sigar sigar = new Sigar();
    Who who[] = sigar.getWhoList();
    if (who != null && who.length > 0) {
        for (int i = 0; i < who.length; i++) {
            // System.out.println("当前系统进程表中的用户名" + String.valueOf(i));
            Who _who = who[i];
            System.out.println("用户控制台:    " + _who.getDevice());
            System.out.println("用户host:    " + _who.getHost());
            // System.out.println("getTime():    " + _who.getTime());
            // 当前系统进程表中的用户名
            System.out.println("当前系统进程表中的用户名:    " + _who.getUser());
        }
    }
}

private static void file() throws Exception {
    Sigar sigar = new Sigar();
    FileSystem fslist[] = sigar.getFileSystemList();
    for (int i = 0; i < fslist.length; i++) {
        System.out.println("分区的盘符名称" + i);
        FileSystem fs = fslist[i];
        // 分区的盘符名称
        System.out.println("盘符名称:    " + fs.getDevName());
        // 分区的盘符名称
        System.out.println("盘符路径:    " + fs.getDirName());
        System.out.println("盘符标志:    " + fs.getFlags());//
        // 文件系统类型,比如 FAT32、NTFS
        System.out.println("盘符类型:    " + fs.getSysTypeName());
        // 文件系统类型名,比如本地硬盘、光驱、网络文件系统等
        System.out.println("盘符类型名:    " + fs.getTypeName());
        // 文件系统类型
        System.out.println("盘符文件系统类型:    " + fs.getType());
        FileSystemUsage usage = null;
        usage = sigar.getFileSystemUsage(fs.getDirName());
        switch (fs.getType()) {
        case 0: // TYPE_UNKNOWN :未知
            break;
        case 1: // TYPE_NONE
            break;
        case 2: // TYPE_LOCAL_DISK : 本地硬盘
            // 文件系统总大小
            System.out.println(fs.getDevName() + "总大小:    " + usage.getTotal() + "KB");
            // 文件系统剩余大小
            System.out.println(fs.getDevName() + "剩余大小:    " + usage.getFree() + "KB");
            // 文件系统可用大小
            System.out.println(fs.getDevName() + "可用大小:    " + usage.getAvail() + "KB");
            // 文件系统已经使用量
            System.out.println(fs.getDevName() + "已经使用量:    " + usage.getUsed() + "KB");
            double usePercent = usage.getUsePercent() * 100D;
            // 文件系统资源的利用率
            System.out.println(fs.getDevName() + "资源的利用率:    " + usePercent + "%");
            break;
        case 3:// TYPE_NETWORK :网络
            break;
        case 4:// TYPE_RAM_DISK :闪存
            break;
        case 5:// TYPE_CDROM :光驱
            break;
        case 6:// TYPE_SWAP :页面交换
            break;
        }
        System.out.println(fs.getDevName() + "读出:    " + usage.getDiskReads());
        System.out.println(fs.getDevName() + "写入:    " + usage.getDiskWrites());
    }
    return;
}

private static void net() throws Exception {
    Sigar sigar = new Sigar();
    String ifNames[] = sigar.getNetInterfaceList();
    for (int i = 0; i < ifNames.length; i++) {
        String name = ifNames[i];
        NetInterfaceConfig ifconfig = sigar.getNetInterfaceConfig(name);
        System.out.println("网络设备名:    " + name);// 网络设备名
        System.out.println("IP地址:    " + ifconfig.getAddress());// IP地址
        System.out.println("子网掩码:    " + ifconfig.getNetmask());// 子网掩码
        if ((ifconfig.getFlags() & 1L) <= 0L) {
            System.out.println("!IFF_UP...skipping getNetInterfaceStat");
            continue;
        }
        NetInterfaceStat ifstat = sigar.getNetInterfaceStat(name);
        System.out.println(name + "接收的总包裹数:" + ifstat.getRxPackets());// 接收的总包裹数
        System.out.println(name + "发送的总包裹数:" + ifstat.getTxPackets());// 发送的总包裹数
        System.out.println(name + "接收到的总字节数:" + ifstat.getRxBytes());// 接收到的总字节数
        System.out.println(name + "发送的总字节数:" + ifstat.getTxBytes());// 发送的总字节数
        System.out.println(name + "接收到的错误包数:" + ifstat.getRxErrors());// 接收到的错误包数
        System.out.println(name + "发送数据包时的错误数:" + ifstat.getTxErrors());// 发送数据包时的错误数
        System.out.println(name + "接收时丢弃的包数:" + ifstat.getRxDropped());// 接收时丢弃的包数
        System.out.println(name + "发送时丢弃的包数:" + ifstat.getTxDropped());// 发送时丢弃的包数
    }
}

private static void ethernet() throws SigarException {
    Sigar sigar = null;
    sigar = new Sigar();
    String[] ifaces = sigar.getNetInterfaceList();
    for (int i = 0; i < ifaces.length; i++) {
        NetInterfaceConfig cfg = sigar.getNetInterfaceConfig(ifaces[i]);
        if (NetFlags.LOOPBACK_ADDRESS.equals(cfg.getAddress()) || (cfg.getFlags() & NetFlags.IFF_LOOPBACK) != 0
                || NetFlags.NULL_HWADDR.equals(cfg.getHwaddr())) {
            continue;
        }
        System.out.println(cfg.getName() + "IP地址:" + cfg.getAddress());// IP地址
        System.out.println(cfg.getName() + "网关广播地址:" + cfg.getBroadcast());// 网关广播地址
        System.out.println(cfg.getName() + "网卡MAC地址:" + cfg.getHwaddr());// 网卡MAC地址
        System.out.println(cfg.getName() + "子网掩码:" + cfg.getNetmask());// 子网掩码
        System.out.println(cfg.getName() + "网卡描述信息:" + cfg.getDescription());// 网卡描述信息
        System.out.println(cfg.getName() + "网卡类型" + cfg.getType());//
    }
}

}
复制代码

转载于:https://blog.51cto.com/13287327/2160480

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_34417635/article/details/92719194

智能推荐

攻防世界_难度8_happy_puzzle_攻防世界困难模式攻略图文-程序员宅基地

文章浏览阅读645次。这个肯定是末尾的IDAT了,因为IDAT必须要满了才会开始一下个IDAT,这个明显就是末尾的IDAT了。,对应下面的create_head()代码。,对应下面的create_tail()代码。不要考虑爆破,我已经试了一下,太多情况了。题目来源:UNCTF。_攻防世界困难模式攻略图文

达梦数据库的导出(备份)、导入_达梦数据库导入导出-程序员宅基地

文章浏览阅读2.9k次,点赞3次,收藏10次。偶尔会用到,记录、分享。1. 数据库导出1.1 切换到dmdba用户su - dmdba1.2 进入达梦数据库安装路径的bin目录,执行导库操作  导出语句:./dexp cwy_init/[email protected]:5236 file=cwy_init.dmp log=cwy_init_exp.log 注释:   cwy_init/init_123..._达梦数据库导入导出

js引入kindeditor富文本编辑器的使用_kindeditor.js-程序员宅基地

文章浏览阅读1.9k次。1. 在官网上下载KindEditor文件,可以删掉不需要要到的jsp,asp,asp.net和php文件夹。接着把文件夹放到项目文件目录下。2. 修改html文件,在页面引入js文件:<script type="text/javascript" src="./kindeditor/kindeditor-all.js"></script><script type="text/javascript" src="./kindeditor/lang/zh-CN.js"_kindeditor.js

STM32学习过程记录11——基于STM32G431CBU6硬件SPI+DMA的高效WS2812B控制方法-程序员宅基地

文章浏览阅读2.3k次,点赞6次,收藏14次。SPI的详情简介不必赘述。假设我们通过SPI发送0xAA,我们的数据线就会变为10101010,通过修改不同的内容,即可修改SPI中0和1的持续时间。比如0xF0即为前半周期为高电平,后半周期为低电平的状态。在SPI的通信模式中,CPHA配置会影响该实验,下图展示了不同采样位置的SPI时序图[1]。CPOL = 0,CPHA = 1:CLK空闲状态 = 低电平,数据在下降沿采样,并在上升沿移出CPOL = 0,CPHA = 0:CLK空闲状态 = 低电平,数据在上升沿采样,并在下降沿移出。_stm32g431cbu6

计算机网络-数据链路层_接收方收到链路层数据后,使用crc检验后,余数为0,说明链路层的传输时可靠传输-程序员宅基地

文章浏览阅读1.2k次,点赞2次,收藏8次。数据链路层习题自测问题1.数据链路(即逻辑链路)与链路(即物理链路)有何区别?“电路接通了”与”数据链路接通了”的区别何在?2.数据链路层中的链路控制包括哪些功能?试讨论数据链路层做成可靠的链路层有哪些优点和缺点。3.网络适配器的作用是什么?网络适配器工作在哪一层?4.数据链路层的三个基本问题(帧定界、透明传输和差错检测)为什么都必须加以解决?5.如果在数据链路层不进行帧定界,会发生什么问题?6.PPP协议的主要特点是什么?为什么PPP不使用帧的编号?PPP适用于什么情况?为什么PPP协议不_接收方收到链路层数据后,使用crc检验后,余数为0,说明链路层的传输时可靠传输

软件测试工程师移民加拿大_无证移民,未受过软件工程师的教育(第1部分)-程序员宅基地

文章浏览阅读587次。软件测试工程师移民加拿大 无证移民,未受过软件工程师的教育(第1部分) (Undocumented Immigrant With No Education to Software Engineer(Part 1))Before I start, I want you to please bear with me on the way I write, I have very little gen...

随便推点

Thinkpad X250 secure boot failed 启动失败问题解决_安装完系统提示secureboot failure-程序员宅基地

文章浏览阅读304次。Thinkpad X250笔记本电脑,装的是FreeBSD,进入BIOS修改虚拟化配置(其后可能是误设置了安全开机),保存退出后系统无法启动,显示:secure boot failed ,把自己惊出一身冷汗,因为这台笔记本刚好还没开始做备份.....根据错误提示,到bios里面去找相关配置,在Security里面找到了Secure Boot选项,发现果然被设置为Enabled,将其修改为Disabled ,再开机,终于正常启动了。_安装完系统提示secureboot failure

C++如何做字符串分割(5种方法)_c++ 字符串分割-程序员宅基地

文章浏览阅读10w+次,点赞93次,收藏352次。1、用strtok函数进行字符串分割原型: char *strtok(char *str, const char *delim);功能:分解字符串为一组字符串。参数说明:str为要分解的字符串,delim为分隔符字符串。返回值:从str开头开始的一个个被分割的串。当没有被分割的串时则返回NULL。其它:strtok函数线程不安全,可以使用strtok_r替代。示例://借助strtok实现split#include <string.h>#include <stdio.h&_c++ 字符串分割

2013第四届蓝桥杯 C/C++本科A组 真题答案解析_2013年第四届c a组蓝桥杯省赛真题解答-程序员宅基地

文章浏览阅读2.3k次。1 .高斯日记 大数学家高斯有个好习惯:无论如何都要记日记。他的日记有个与众不同的地方,他从不注明年月日,而是用一个整数代替,比如:4210后来人们知道,那个整数就是日期,它表示那一天是高斯出生后的第几天。这或许也是个好习惯,它时时刻刻提醒着主人:日子又过去一天,还有多少时光可以用于浪费呢?高斯出生于:1777年4月30日。在高斯发现的一个重要定理的日记_2013年第四届c a组蓝桥杯省赛真题解答

基于供需算法优化的核极限学习机(KELM)分类算法-程序员宅基地

文章浏览阅读851次,点赞17次,收藏22次。摘要:本文利用供需算法对核极限学习机(KELM)进行优化,并用于分类。

metasploitable2渗透测试_metasploitable2怎么进入-程序员宅基地

文章浏览阅读1.1k次。一、系统弱密码登录1、在kali上执行命令行telnet 192.168.26.1292、Login和password都输入msfadmin3、登录成功,进入系统4、测试如下:二、MySQL弱密码登录:1、在kali上执行mysql –h 192.168.26.129 –u root2、登录成功,进入MySQL系统3、测试效果:三、PostgreSQL弱密码登录1、在Kali上执行psql -h 192.168.26.129 –U post..._metasploitable2怎么进入

Python学习之路:从入门到精通的指南_python人工智能开发从入门到精通pdf-程序员宅基地

文章浏览阅读257次。本文将为初学者提供Python学习的详细指南,从Python的历史、基础语法和数据类型到面向对象编程、模块和库的使用。通过本文,您将能够掌握Python编程的核心概念,为今后的编程学习和实践打下坚实基础。_python人工智能开发从入门到精通pdf

推荐文章

热门文章

相关标签