Platon Technologies
not logged in Login Registration
EnglishSlovak
open source software development celebrating 10 years of open source development! Thursday, March 28, 2024

File: [Platon] / scripts / shell / firewall / ifconfig-parse.sh (download)

Revision 1.3, Fri Mar 4 23:54:05 2005 UTC (19 years ago) by rajo

Changes since 1.2: +65 -2 lines

Parse also route info (gateway, etc.)

#!/bin/sh

#
# ifconfig-parse.sh - parse output from ifconfig and print
#                     in format suitable for eval in shell
#
# FreeBSD and Linux are supported
#
# Developed by Lubommir Host 'rajo' <rajo AT platon.sk>
# Copyright (c) 2003 Platon SDG
# Licensed under terms of GNU General Public License.
# All rights reserved.
#

# $Platon: scripts/shell/firewall/ifconfig-parse.sh,v 1.2 2003/08/08 09:56:53 rajo Exp $

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
export PATH

# Parse output from ifconfig:
ifconfig | \
    awk 'BEGIN { interfaces=""; }
        /^[a-zA-Z0-9]+[ \t]+/ { # Linux
            iface=$1;
            interfaces = sprintf("%s %s", interfaces, iface);
            printf "\nIFACE_%s=\"%s\";    export IFACE_%s;\n", iface, iface, iface;
            printf "HWaddr_%s=\"%s\";    export HWaddr_%s;\n", iface, $5, iface;
        }
        /^[ \t]+inet addr:/ { # Linux
            split($0, fields, "[ \t:]+");
            printf "IP_%s=\"%s\";    export IP_%s;\n", iface, fields[4], iface;
            printf "Bcast_%s=\"%s\";    export Bcast_%s;\n", iface, fields[6], iface;
            printf "Mask_%s=\"%s\";    export Mask_%s;\n", iface, fields[8],  iface;
        }
        /^[a-zA-Z0-9]+:/ { # FreeBSD
            iface = $1;
            sub(":", "", iface);
            interfaces = sprintf("%s %s", interfaces, iface);
            printf "\nIFACE_%s=\"%s\";    export IFACE_%s;\n", iface, iface, iface;
        }
        /^[ \t]+inet [0-9]+/ { # FreeBSD
            printf "IP_%s=\"%s\";    export IP_%s;\n", iface, $2, iface;
            printf "Bcast_%s=\"%s\";    export Bcast_%s;\n", iface, $6, iface;
            printf "Mask_%s=\"%s\";    export Mask_%s;\n", iface, $4,  iface;
        }
        /^[ \t]+ether/ { # FreeBSD
            printf "HWaddr_%s=\"%s\";    export HWaddr_%s;\n", iface, $2, iface;
        }
        END { printf "\ninterfaces=\"%s\";     export interfaces;\n", interfaces; }
    '

#
# parse routing info on Linux (FreeBSD not supported yet)
#
# only "Gateway Destination Flags MTU Metric Window IRTT" columns are extracted from /proc/net/route:
if [ "x`uname -s`" = "xLinux" ]; then

    perl -e '
    $\ = "\n";
    open(FILE, "/proc/net/route") or die "Cannot open /proc/net/route: $!";
    my @columns = split(/\s+/, <FILE>);
    while (my $line = <FILE>) {
        my $iface;
        my @vals = split(/\s+/, $line);
        foreach my $key (@columns) {
            $iface->{$key} = shift @vals;
        }
        
        foreach my $key (qw( Gateway Destination )) {
            print "${key}_$iface->{Iface}=",
                qw("), hex2ip($iface->{$key}), qw("),
                "; export ${key}_$iface->{Iface};";
        }
        foreach my $key (qw( Flags MTU Metric Window IRTT )) {
            print "${key}_$iface->{Iface}=",
                qw("), $iface->{$key}, qw("),
                "; export ${key}_$iface->{Iface};";
        }
    }
    close(FILE);


    sub hex2ip
    { # {{{
        my ($str) = @_;
        my @block;

        my $hex = uc($str);

        while (length($hex)) {
            my $x = ord(substr($hex, 0, 1));
            my $y = ord(substr($hex, 1, 1));

            $x = $x > 64 ? $x - 55 : $x - 48;
            $y = $y > 64 ? $y - 55 : $y - 48;

            push @block, 16 * $x + $y;
            $hex = substr($hex, 2);
        }

        return join(".", reverse @block);

    } # }}}
    '

fi

# vim: ft=sh fdm=marker fdl=0 fdc=3


Platon Group <platon@platon.org> http://platon.org/
Copyright © 2002-2006 Platon Group
Site powered by Metafox CMS
Go to Top