[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: how do I access interface details from inside perl ?
> Hi !
>
> I know that this is a little bit off-topic but the perl folks couldn't
> answer this. I'd like to get information about incoming and outgoing
> packets or bytes, like e.g. netstat or ifconfig shows. But I don't want
> to call an external program.
Hi,
Remember that Perl is not an operating system - rather, Perl relies on the
OS to do certain things. One of them is getting this kind of information.
Perl does not have a network interface attached - It uses the network
facility your OS gives it. You can trick Perl into believing it is reading
from the network interface when it is reading from somewhere completely
different.
You *need* to call an external program. Yes, in Linux you can read from
/proc/something - but that is also technically calling an external program
to generate the data (only the program lives inside the kernel). In OBSD,
the filesystem does not represent anything but files... So you must ask a
system program.
I use this to get the bytes recieved and sent by my network interface:
===========================================================
#!/usr/bin/perl -wT
use strict;
my (@data,$interface,@line,$ethIn,$ethOut);
$interface = 'xl0';
$ENV{PATH}='/usr/bin';
@data = `/usr/bin/netstat -bin`;
# Format of data is:
# One line of legend (ignored)
# Each following line begins with the interface name, its MTU,
# the network (or <DEFANGED_link>), the address (either physical for links
# or IP for network addresses), input bytes and output bytes
while (@line = split(/\s+/,shift(@data))) {
last if ($line[0] eq $interface);
}
$ethIn = $line[4];
$ethOut = $line[5];
print "$ethIn\n$ethOut\n\n";
===========================================================
it calls netstat. This is the only way I'm aware of.
--
Gunnar Wolf - gwolf@campus.iztacala.unam.mx - (+52-55)5623-1118
PGP key 1024D/8BB527AF 2001-10-23
Fingerprint: 0C79 D2D1 2C4E 9CE4 5973 F800 D80E F35A 8BB5 27AF