SIGAR (full Name System Information Gatherer and Reporter, which is the Systems information Collection Report), provides an open source, cross-platform API for collecting computer hardware and operating system information (the API's underlying interface is written in C). This article shows you how to get network information with the 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 /** * Get network information with SIGAR API * * @author Gaohuanjie */public class Networktool {public static void main (string[] args) throws sigarexception {SIGAR SIGAR = new SIGAR ();//The official domain name of the current machine try {System.out.println (Sigar.getfqdn ());//i.e. fully qualified Domai N Name, Chinese as: Full name} catch (Sigarexception e) {e.printstacktrace ();} string[] netinterfacelist = Sigar.getnetinterfacelist ();//Get network traffic information for (int i = 0; i < netinterfacelist.length; i++) {S Tring netinterface = netinterfacelist[i];//network Interface System.out.println ("NetInterface:" + netinterface); Netinterfaceconfig netinterfaceconfig = Sigar.getnetinterfaceconfig (netinterface); SYSTEM.OUT.PRINTLN ("address =" + netinterfaceconfig.getaddress ());//IP address System.out.println ("Netmask =" + Netinterfaceconfig.getnetmask ());//Subnet mask if (netinterfaceconfiG.getflags () & 1L) <= 0L) {//Network appliance is enabled SYSTEM.OUT.PRINTLN ("! Iff_up...skipping Getnetinterfacestat "); continue;} Netinterfacestat Netinterfacestat = Sigar.getnetinterfacestat (netinterface); System.out.println ("Netinterfacestat rxpackets:" + netinterfacestat.getrxpackets ());//The total number of packages received SYSTEM.OUT.PRINTLN (" Netinterfacestat txpackets: "+ netinterfacestat.gettxpackets ());//The total number of packages sent System.out.println (" Netinterfacestat Rxbytes: "+ netinterfacestat.getrxbytes ());//The total number of bytes received System.out.println (" Netinterfacestat txbytes: "+ Netinterfacestat.gettxbytes ());//The total number of bytes sent System.out.println ("Netinterfacestat rxerrors:" + Netinterfacestat.getrxerrors ());//The number of error packets received SYSTEM.OUT.PRINTLN ("Netinterfacestat txerrors:" + Netinterfacestat.gettxerrors ());//The number of errors when sending packets System.out.println ("Netinterfacestat rxdropped:" + Netinterfacestat.getrxdropped ());//The number of packets dropped when receiving SYSTEM.OUT.PRINTLN ("Netinterfacestat txdropped:" + Netinterfacestat.gettxdropped ());//The number of packets dropped when sending 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 ());} Some other information for (int i = 0; i < netinterfacelist.length; i++) {String netinterface = netinterfacelist[i];//network Interface Netinterf Aceconfig 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 ());//Nic MAC address System.out.println ("Netinterfaceconfig type:" + netinterfaceconfig.gettype ()); SYSTEM.OUT.PRINTLN ("Netinterfaceconfig Description:" + netinterfaceconfig.getdescription ());// Network card description Information System.out.println ("Netinterfaceconfig Address:" + netinterfaceconfig.getaddress ());// IP address System.out.println ("Netinterfaceconfig destination:" + netinterfaceconfig.getdestination ()); SYSTEM.OUT.PRINTLN ("Netinterfaceconfig broadcast:" + Netinterfaceconfig.getbroadcast ());// Gateway broadcast address SYSTEM.OUT.PRINTLN ("Netinterfaceconfig netmask:" + netinterfaceconfig.getnetmask ());// Subnet Mask System.out.println ("Netinterfaceconfig Flags:" + netinterfaceconfig.getflags ()); System.out.println ("Netinterfaceconfig MTU:" + NETINTERFACECONFIG.GETMTU ()); SYSTEM.OUT.PRINTLN ("Netinterfaceconfig metric:" + netinterfaceconfig.getmetric ());} Sigar.close ();}}
【
0 min Download Sample project】
Get network information with the SIGAR API