#!/usr/bin/perl -w # # ifconfig-parse.pl - parse output from ifconfig and print # in format suitable for eval in shell # # Developed by Lubommir Host "rajo" # Copyright (c) 2007 Platon SDG # Licensed under terms of GNU General Public License. # All rights reserved. # # Changelog: # 2007-08-29 - created # # $Platon: $ #use Data::Dumper; my $iface_count = 0; my $iface; my (%ip6, %scope6, %bcast, %mask, %hwaddr, %ipcount); while (my $line = ) { chomp $line; if ($line =~ m/^([a-z0-9:]+)\s+.*?([a-z0-9:]+)\s*$/i) { # Linux interface $iface = $1; my $iface_hwaddr = $2; $iface = [ $iface =~ m/^([a-z0-9]+)/i ]->[0]; # convert "eth0:0" --> "eth0" $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]; } } 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; printf "interfaces=\"%s\"; export interfaces;\n", join(" ", keys %ip);