Súbor: [Platon] / scripts / shell / firewall / ifconfig-parse.pl (stiahnutie)
Revízia 2.2, Mon Oct 12 22:41:24 2015 UTC (9 years, 2 months ago) by rajo
Zmeny od 2.1: +20 -5 [lines]
Fix: parse new ifconfig format introduced in Debian version 1.60+git20150829.73ce of package net-tools
|
#!/usr/bin/perl -w
use strict;
use warnings;
#
# ifconfig-parse.pl - parse output from ifconfig and print
# in format suitable for eval in shell
#
# Developed by Lubommir Host "rajo" <rajo AT platon.sk>
# Copyright (c) 2007 Platon SDG
# Licensed under terms of GNU General Public License.
# All rights reserved.
#
# Changelog:
# 2007-08-29 - created
#
# $Platon: scripts/shell/firewall/ifconfig-parse.pl,v 2.1 2007-08-29 14:43:55 rajo Exp $
#use Data::Dumper;
my $iface_count = 0;
my $iface;
my (%ip, %ifname, %ip6, %scope6, %bcast, %mask, %hwaddr, %ipcount);
while (my $line = <STDIN>) {
chomp $line;
if ($line =~ m/^([a-z0-9:]+)\s+.*?([a-z0-9:]+)\s*$/i) { # Linux interface
$iface = $1;
my $iface_hwaddr = $2;
my $x_iface = $iface;
$iface =~ s/:$//g;
$iface =~ s/:/_/g; # convert "eth0:0" --> "eth0_0"
$x_iface = [ $x_iface =~ m/^([a-z0-9]+)/i ]->[0]; # convert "eth0:0" --> "eth0"
$ifname{$iface} = $x_iface;
$ipcount{$iface}++;
$hwaddr{$iface} = $iface_hwaddr;
$iface_count++;
}
elsif ($line =~ m/^[ \t]+inet addr:/) { # Linux IP address
die unless defined $iface;
my @fields = split(/[\s:]+/, $line);
push @{$ip{$iface}}, $fields[3];
$bcast{$iface} = $fields[5] || ""; # invalid for loopback interface lo, but we don t need this
$mask{$iface} = $fields[7] || $fields[5]; # for loopback interface lo
}
elsif($line =~ m/^[ \t]+inet6 addr:/) { # Linux IPv6 address
die unless defined $iface;
my @fields = split(/\s+/, $line);
push @{$ip6{$iface}}, $fields[3];
$scope6{$iface} = [ $fields[4] =~ m/Scope:(.*)$/i ]->[0];
}
elsif ($line =~ m/^[ \t]+inet\s/) { # Linux IP address
die unless defined $iface;
my @fields = split(/[\s:]+/, $line);
push @{$ip{$iface}}, $fields[2];
$bcast{$iface} = (defined($fields[5]) and $fields[5] eq "broadcast") ? $fields[6] : "";
$mask{$iface} = $fields[4];
}
}
map { printf "IP_%s=\"%s\"; export IP_%s;\n", $_, join(" ", @{$ip{$_}}), $_; } keys %ip;
map { printf "IFACE_6_%s=\"%s\"; export IFACE_6_%s;\n", $_, join(" ", @{$ip6{$_}}), $_; } keys %ip6;
map { printf "SCOPE_6_%s=\"%s\"; export SCOPE_6_%s;\n", $_, $scope6{$_}, $_; } keys %scope6;
map { printf "Bcast_%s=\"%s\"; export Bcast_%s;\n", $_, $bcast{$_}, $_; } keys %bcast;
map { printf "Mask_%s=\"%s\"; export Mask_%s;\n", $_, $mask{$_}, $_; } keys %mask;
map { printf "HWaddr_%s=\"%s\"; export HWaddr_%s;\n", $_, $hwaddr{$_}, $_; } keys %hwaddr;
map { printf "IPcount_%s=\"%s\"; export IPcount_%s;\n", $_, $ipcount{$_}, $_; } keys %ipcount;
map { printf "IFname_%s=\"%s\"; export IFname_%s;\n", $_, $ifname{$_}, $_; } keys %ifname;
printf "interfaces=\"%s\"; export interfaces;\n", join(" ", sort keys %ip);
Platon Group <platon@platon.sk> http://platon.sk/
|