sigar/bindings/dotnet/examples/Ifconfig.cs

86 lines
2.5 KiB
C#
Raw Normal View History

2004-09-18 06:52:18 +08:00
using System;
using Hyperic.Sigar;
public class Ifconfig {
private static void println(String line) {
System.Console.WriteLine(line);
}
private static void output(Sigar sigar, String name) {
NetInterfaceConfig ifconfig =
sigar.NetInterfaceConfig(name);
2005-12-07 01:08:14 +08:00
long flags = ifconfig.Flags;
2004-09-18 06:52:18 +08:00
String hwaddr = "";
if (!Sigar.NULL_HWADDR.Equals(ifconfig.Hwaddr)) {
hwaddr = " HWaddr " + ifconfig.Hwaddr;
}
println(ifconfig.Name + "\t" +
2005-12-07 01:01:54 +08:00
"Link encap:" + ifconfig.Type +
2004-09-18 06:52:18 +08:00
hwaddr);
String ptp = "";
if ((flags & Sigar.IFF_POINTOPOINT) > 0) {
ptp = " P-t-P:" + ifconfig.Destination;
}
String bcast = "";
if ((flags & Sigar.IFF_BROADCAST) > 0) {
bcast = " Bcast:" + ifconfig.Broadcast;
}
println("\t" +
"inet addr:" + ifconfig.Address +
ptp + //unlikely
bcast +
" Mask:" + ifconfig.Netmask);
println("\t" +
ifconfig.FlagsString() +
" MTU:" + ifconfig.Mtu +
" Metric:" + ifconfig.Metric);
try {
NetInterfaceStat ifstat =
sigar.NetInterfaceStat(name);
println("\t" +
"RX packets:" + ifstat.RxPackets +
" errors:" + ifstat.RxErrors +
" dropped:" + ifstat.RxDropped +
" overruns:" + ifstat.RxOverruns +
" frame:" + ifstat.RxFrame);
println("\t" +
"TX packets:" + ifstat.TxPackets +
" errors:" + ifstat.TxErrors +
" dropped:" + ifstat.TxDropped +
" overruns:" + ifstat.TxOverruns +
" carrier:" + ifstat.TxCarrier);
println("\t" + "collisions:" +
ifstat.TxCollisions);
2005-12-07 01:08:14 +08:00
long rxBytes = ifstat.RxBytes;
long txBytes = ifstat.TxBytes;
2004-09-18 06:52:18 +08:00
println("\t" +
"RX bytes:" + rxBytes +
" (" + Sigar.FormatSize(rxBytes) + ")" +
" " +
"TX bytes:" + txBytes +
" (" + Sigar.FormatSize(txBytes) + ")");
2004-09-19 01:49:18 +08:00
} catch (SigarException) { }
2004-09-18 06:52:18 +08:00
println("");
}
public static void Main() {
Sigar sigar = new Sigar();
foreach (String name in sigar.NetInterfaceList()) {
output(sigar, name);
}
}
}