|
@ -74,12 +74,23 @@ public class SigarUtil {
|
|
|
* @throws UnknownHostException
|
|
|
*/
|
|
|
|
|
|
public static String getHostName(){
|
|
|
try {
|
|
|
NetInfo netInfo = sigar.getNetInfo();
|
|
|
String hostName = netInfo.getHostName();
|
|
|
return hostName;
|
|
|
} catch (SigarException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
return "";
|
|
|
}
|
|
|
|
|
|
public static String getHost(){
|
|
|
try {
|
|
|
InetAddress addr = InetAddress.getLocalHost();
|
|
|
String ip = addr.getHostAddress();
|
|
|
NetInterfaceConfig netInterfaceConfig = sigar.getNetInterfaceConfig(null);
|
|
|
String ip = netInterfaceConfig.getAddress();
|
|
|
return ip;
|
|
|
} catch (UnknownHostException e) {
|
|
|
} catch (SigarException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
return "unknowHost";
|
|
@ -304,26 +315,12 @@ public class SigarUtil {
|
|
|
Map<String, Object> netMap = new HashMap<>();
|
|
|
NetInterfaceConfig netConfig = null;
|
|
|
try {
|
|
|
netConfig = sigar.getNetInterfaceConfig();
|
|
|
netConfig = sigar.getNetInterfaceConfig(null);
|
|
|
NetInterfaceStat netIfStat = sigar.getNetInterfaceStat(netConfig.getName());
|
|
|
|
|
|
// 取到当前机器的IP地址
|
|
|
String address = null;
|
|
|
try {
|
|
|
address = InetAddress.getLocalHost().getHostAddress();
|
|
|
InetAddress inet = InetAddress.getLocalHost();
|
|
|
// 没有出现异常而正常当取到的IP时,如果取到的不是网卡循回地址时就返回
|
|
|
// 否则再通过Sigar工具包中的方法来获取
|
|
|
if (NetFlags.LOOPBACK_ADDRESS.equals(address)) {
|
|
|
address = netConfig.getAddress();
|
|
|
}
|
|
|
} catch (UnknownHostException e) {
|
|
|
address = netConfig.getAddress();
|
|
|
}
|
|
|
|
|
|
Map<String, Object> bps = populate(netConfig.getName());//bps
|
|
|
netMap.put("name", netConfig.getName());
|
|
|
netMap.put("address", address);
|
|
|
netMap.put("address", netConfig.getAddress());
|
|
|
netMap.put("mac", netConfig.getHwaddr());
|
|
|
netMap.put("type", netConfig.getType());
|
|
|
netMap.put("broadcast", netConfig.getBroadcast());
|