jinyedge's note
{http://www.jinyedge.pe.kr}
Hi, this is jinyedge. I'm a software developer.
I hope you can find some useful information
in my homepage.
jinyedge at gmail.com
Since 2001.12.05
|
|
| Subj: Perl, get_ifnames, get_ifconfig_rh, get_default_gw in Freebsd. |
|
|
Mtime: 2009-10-28 01:20:56 |
|
|
#!/usr/bin/perl
use Data::Dumper;
#------------------------------------------------------
sub get_ifnames{
my @lines = `/sbin/ifconfig`;
my @ifnames = ();
for my $line(@lines){
$line =~ /^plip0|^lo0/ and last;
if($line =~ /^\S/){
$ifname = $line;
$ifname =~ s/:.*//g;
$ifname =~ s/\s+//g;
$ifname or next;
push @ifnames, $ifname;
}
}
return \@ifnames;
}
#------------------------------------------------------
sub get_ifconfig_rh{
my $ifname = shift;
my @lines = `/sbin/ifconfig $ifname`;
my $rh = {};
$rh->{ifname} = $ifname;
for my $line(@lines){
if($line =~ /inet/){
$line =~ s/^\s+//;
my @arr = split /\s+/, $line;
# Get ip.
shift @arr;
my $ip = shift @arr;
$rh->{ip} = $ip;
# Get subnet.
shift @arr;
my $netmask = shift @arr;
$netmask =~ /(..)(..)(..)(..)(..)/g;
$netmask = sprintf "%s.%s.%s.%s.", hex($2), hex($3), hex($4), hex($5);
$rh->{netmask} = $netmask;
}
}
return $rh;
}
#------------------------------------------------------
sub get_default_gw{
my @lines = `/usr/bin/netstat -rn`;
for my $line(@lines){
if($line =~ /default/){
my @arr = split /\s+/, $line;
return $arr[1];
}
}
}
#------------------------------------------------------
# main.
my $ra = get_ifnames();
for my $ifname(@$ra){
my $rh = get_ifconfig_rh($ifname);
print Dumper($rh);
}
print get_default_gw(), "\n";
|
|
|
|
|