add getNetInterfaceConfig() util to get default ethernet config
This commit is contained in:
parent
a0b3363afd
commit
fb9d624b0c
|
@ -663,6 +663,46 @@ public class Sigar implements SigarProxy {
|
|||
return NetInterfaceConfig.fetch(this, name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get default network interface configuration info.
|
||||
* Iterates getNetInterfaceList(), returning the first
|
||||
* available ethernet interface.
|
||||
* @exception SigarException on failure.
|
||||
*/
|
||||
public NetInterfaceConfig getNetInterfaceConfig()
|
||||
throws SigarException {
|
||||
|
||||
String[] interfaces = getNetInterfaceList();
|
||||
|
||||
for (int i=0; i<interfaces.length; i++) {
|
||||
String name = interfaces[i];
|
||||
NetInterfaceConfig ifconfig;
|
||||
|
||||
try {
|
||||
ifconfig = getNetInterfaceConfig(name);
|
||||
} catch (SigarException e) {
|
||||
continue;
|
||||
}
|
||||
|
||||
long flags = ifconfig.getFlags();
|
||||
if ((flags & NetFlags.IFF_UP) <= 0) {
|
||||
continue;
|
||||
}
|
||||
if ((flags & NetFlags.IFF_POINTOPOINT) > 0) {
|
||||
continue;
|
||||
}
|
||||
if ((flags & NetFlags.IFF_LOOPBACK) > 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
return ifconfig;
|
||||
}
|
||||
|
||||
String msg =
|
||||
"No ethernet interface available";
|
||||
throw new SigarException(msg);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get network interface stats.
|
||||
* @exception SigarException on failure.
|
||||
|
|
|
@ -118,6 +118,9 @@ public interface SigarProxy {
|
|||
public NetInterfaceConfig getNetInterfaceConfig(String name)
|
||||
throws SigarException;
|
||||
|
||||
public NetInterfaceConfig getNetInterfaceConfig()
|
||||
throws SigarException;
|
||||
|
||||
public NetInterfaceStat getNetInterfaceStat(String name)
|
||||
throws SigarException;
|
||||
|
||||
|
|
|
@ -77,6 +77,8 @@ public class TestNetIf extends SigarTestCase {
|
|||
getNetIflist(sigar, false);
|
||||
getNetIflist(sigar, false);
|
||||
getNetIflist(sigar, true);
|
||||
traceln("Default IP=" +
|
||||
sigar.getNetInterfaceConfig().getAddress());
|
||||
|
||||
//XXX somehow manage to trigger a segfault using the
|
||||
//1.4.1_02-b06 jdk on linux, no problem with 1.4.2_02-b03
|
||||
|
|
Loading…
Reference in New Issue