From 5882915d14712a2c079b5f24e270f9d7c2493dbf Mon Sep 17 00:00:00 2001 From: Doug MacEachern Date: Fri, 17 Sep 2004 20:21:56 +0000 Subject: [PATCH] ifconfig example --- bindings/perl/examples/ifconfig.pl | 75 ++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100755 bindings/perl/examples/ifconfig.pl diff --git a/bindings/perl/examples/ifconfig.pl b/bindings/perl/examples/ifconfig.pl new file mode 100755 index 00000000..bd94362b --- /dev/null +++ b/bindings/perl/examples/ifconfig.pl @@ -0,0 +1,75 @@ +#!/usr/bin/perl + +use strict; +use Sigar; + +my $sigar = new Sigar; + +my $iflist = $sigar->net_interface_list; + +for my $ifname (@$iflist) { + my $ifconfig = $sigar->net_interface_config($ifname); + my $flags = $ifconfig->flags; + + my $encap = ($flags & Sigar::IFF_LOOPBACK) ? + "Local Loopback" : "Ethernet"; + + my $hwaddr = $ifconfig->hwaddr; + if ($hwaddr eq Sigar::NULL_HWADDR) { + $hwaddr = ""; + } + else { + $hwaddr = " HWaddr " . $hwaddr; + } + + print $ifname . "\t" . "Link encap:" . $encap . $hwaddr . "\n"; + + my $ptp = ""; + if ($flags & Sigar::IFF_POINTOPOINT) { + $ptp = " P-t-P:" . $ifconfig->destination; + } + + my $bcast = ""; + if ($flags & Sigar::IFF_BROADCAST) { + $bcast = " Bcast:" . $ifconfig->broadcast; + } + + print "\t" . "inet addr:" . $ifconfig->address . + $ptp . $bcast . " Mask:" . $ifconfig->netmask . "\n"; + + print "\t" . + Sigar::net_interface_flags_string($flags) . + " MTU:" . $ifconfig->mtu . + " Metric:" . $ifconfig->metric . "\n"; + + my $ifstat; + eval { + $ifstat = $sigar->net_interface_stat($ifname); + } or next; + + print "\t" . + "RX packets:" . $ifstat->rx_packets . + " errors:" . $ifstat->rx_errors . + " dropped:" . $ifstat->rx_dropped . + " overruns:" . $ifstat->rx_overruns . + " frame:" . $ifstat->rx_frame . "\n"; + + print "\t" . + "TX packets:" . $ifstat->tx_packets . + " errors:" . $ifstat->tx_errors . + " dropped:" . $ifstat->tx_dropped . + " overruns:" . $ifstat->tx_overruns . + " carrier:" . $ifstat->tx_carrier . "\n"; + + print "\t" . "collisions:" . $ifstat->tx_collisions . "\n"; + + my $rx_bytes = $ifstat->rx_bytes; + my $tx_bytes = $ifstat->tx_bytes; + + print "\t" . + "RX bytes:" . $rx_bytes . + " (" . Sigar::format_size($rx_bytes) . ")" . + " " . + "TX bytes:" . $tx_bytes . + " (" . Sigar::format_size($tx_bytes) . ")" . "\n\n"; +}