借助Sigar API获取网络信息

        Sigar(全称System Information Gatherer And Reporter,即系统信息收集报表器),它提供了一个开源的跨平台的收集计算机硬件和操作系统信息的API(该API底层接口用C语言编写),本文将演示如何借助Sigar API获取网络信息:

package com.ghj.packageoftest;

import org.hyperic.sigar.NetFlags;
import org.hyperic.sigar.NetInterfaceConfig;
import org.hyperic.sigar.NetInterfaceStat;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;

/**
 * 借助Sigar API获取网络信息
 * 
 * @author GaoHuanjie
 */
public class NetworkTool {

	public static void main(String[] args) throws SigarException {
		Sigar sigar = new Sigar();

		// 当前机器的正式域名
		try {
			System.out.println(sigar.getFQDN());// 即Fully Qualified Domain Name,中文为:全称域名
		} catch (SigarException e) {
			e.printStackTrace();
		}

		String[] netInterfaceList = sigar.getNetInterfaceList();

		// 获取网络流量信息
		for (int i = 0; i < netInterfaceList.length; i++) {
			String netInterface = netInterfaceList[i];// 网络接口
			System.out.println("netInterface:" + netInterface);
			NetInterfaceConfig netInterfaceConfig = sigar.getNetInterfaceConfig(netInterface);
			System.out.println("Address = " + netInterfaceConfig.getAddress());// IP地址
			System.out.println("Netmask = " + netInterfaceConfig.getNetmask());// 子网掩码
			if ((netInterfaceConfig.getFlags() & 1L) <= 0L) {// 网络装置是否正常启用
				System.out.println("!IFF_UP...skipping getNetInterfaceStat");
				continue;
			}
			NetInterfaceStat netInterfaceStat = sigar.getNetInterfaceStat(netInterface);
			System.out.println("netInterfaceStat rxPackets:" + netInterfaceStat.getRxPackets());// 接收的总包裹数
			System.out.println("netInterfaceStat txPackets:" + netInterfaceStat.getTxPackets());// 发送的总包裹数
			System.out.println("netInterfaceStat rxBytes:" + netInterfaceStat.getRxBytes());// 接收到的总字节数
			System.out.println("netInterfaceStat txBytes:" + netInterfaceStat.getTxBytes());// 发送的总字节数
			System.out.println("netInterfaceStat rxErrors:" + netInterfaceStat.getRxErrors());// 接收到的错误包数
			System.out.println("netInterfaceStat txErrors:" + netInterfaceStat.getTxErrors());// 发送数据包时的错误数
			System.out.println("netInterfaceStat rxDropped:" + netInterfaceStat.getRxDropped());// 接收时丢弃的包数
			System.out.println("netInterfaceStat txDropped:" + netInterfaceStat.getTxDropped());// 发送时丢弃的包数
			System.out.println("netInterfaceStat rxOverruns:" + netInterfaceStat.getRxOverruns());
			System.out.println("netInterfaceStat txOverruns:" + netInterfaceStat.getTxOverruns());
			System.out.println("netInterfaceStat rxFrame:" + netInterfaceStat.getRxFrame());
			System.out.println("netInterfaceStat txCollisions:" + netInterfaceStat.getTxCollisions());
			System.out.println("netInterfaceStat txCarrier:" + netInterfaceStat.getTxCarrier());
			System.out.println("netInterfaceStat speed:" + netInterfaceStat.getSpeed());
		}

		// 一些其它的信息
		for (int i = 0; i < netInterfaceList.length; i++) {
			String netInterface = netInterfaceList[i];// 网络接口
			NetInterfaceConfig netInterfaceConfig = sigar.getNetInterfaceConfig(netInterface);
			if (NetFlags.LOOPBACK_ADDRESS.equals(netInterfaceConfig.getAddress())
				|| (netInterfaceConfig.getFlags() & NetFlags.IFF_LOOPBACK) != 0
				|| NetFlags.NULL_HWADDR.equals(netInterfaceConfig.getHwaddr())) {
				continue;
			}

			System.out.println("netInterfaceConfig name:" + netInterfaceConfig.getName());
			System.out.println("netInterfaceConfig hwaddr:" + netInterfaceConfig.getHwaddr());// 网卡MAC地址
			System.out.println("netInterfaceConfig type:" + netInterfaceConfig.getType());
			System.out.println("netInterfaceConfig description:" + netInterfaceConfig.getDescription());// 网卡描述信息
			System.out.println("netInterfaceConfig address:" + netInterfaceConfig.getAddress());// IP地址
			System.out.println("netInterfaceConfig destination:" + netInterfaceConfig.getDestination());
			System.out.println("netInterfaceConfig broadcast:" + netInterfaceConfig.getBroadcast());// 网关广播地址
			System.out.println("netInterfaceConfig netmask:" + netInterfaceConfig.getNetmask());// 子网掩码
			System.out.println("netInterfaceConfig flags:" + netInterfaceConfig.getFlags());
			System.out.println("netInterfaceConfig mtu:" + netInterfaceConfig.getMtu());
			System.out.println("netInterfaceConfig metric:" + netInterfaceConfig.getMetric());
		}
		sigar.close();
	}
}

        【0分下载示例工程

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Sigar 是一个跨平台的系统信息收集库,可以获取系统的 CPU、内存、磁盘、网络信息。以下是获取系统信息的示例代码: ```java import org.hyperic.sigar.*; public class SigarDemo { public static void main(String[] args) throws SigarException { Sigar sigar = new Sigar(); System.out.println("CPU信息:"); CpuInfo[] cpuInfos = sigar.getCpuInfoList(); for (CpuInfo cpuInfo : cpuInfos) { System.out.println("CPU型号:" + cpuInfo.getModel()); System.out.println("CPU频率:" + cpuInfo.getMhz() + "MHz"); System.out.println("CPU核数:" + cpuInfo.getTotalCores()); } System.out.println("内存信息:"); Mem mem = sigar.getMem(); System.out.println("总内存:" + mem.getTotal() / 1024 / 1024 + "MB"); System.out.println("已用内存:" + mem.getUsed() / 1024 / 1024 + "MB"); System.out.println("剩余内存:" + mem.getFree() / 1024 / 1024 + "MB"); System.out.println("磁盘信息:"); FileSystem[] fileSystems = sigar.getFileSystemList(); for (FileSystem fileSystem : fileSystems) { System.out.println("盘符:" + fileSystem.getDirName()); System.out.println("盘符类型:" + fileSystem.getTypeName()); FileSystemUsage usage = sigar.getFileSystemUsage(fileSystem.getDirName()); System.out.println("总大小:" + usage.getTotal() / 1024 / 1024 + "MB"); System.out.println("已用大小:" + usage.getUsed() / 1024 / 1024 + "MB"); System.out.println("剩余大小:" + usage.getFree() / 1024 / 1024 + "MB"); } System.out.println("网络信息:"); NetInterfaceConfig[] netConfigs = sigar.getNetInterfaceConfigList(); for (NetInterfaceConfig netConfig : netConfigs) { System.out.println("网络设备名:" + netConfig.getName()); System.out.println("IP地址:" + netConfig.getAddress()); System.out.println("子网掩码:" + netConfig.getNetmask()); } } } ``` 注意:需要引入 sigar.jar 和 sigar-amd64-winnt.dll(或其它平台对应的库文件)。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序猿老高

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值