bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Subnet address


From: Dave Rutherford
Subject: Re: Subnet address
Date: Sat, 21 Jul 2007 10:39:25 -0400

On 7/21/07, Archimerged Ark Submedes <address@hidden> wrote:
On 7/20/07, Mike Frysinger <address@hidden> did not read the question.

Neither did you.  ;-)  Asked for was a solution using ifconfig and bash;
you added grep and tr.  Yes, that's entirely reasonable on your part,
but it's not necessary.

The answer is:

 /sbin/ifconfig eth0 |
grep 'inet addr' | tr .: '  ' |
 (read inet addr a b c d Bcast e f g h Mask i j k l;
 echo $(($a & $i)).$(($b & $j )).$(( $c & $k )).$(( $d & $l )) )

Here's an ugly way to do it:

NETADDR=`/sbin/ifconfig |
        while read w d z z; do
            if [ "$w" = "inet" ]; then
                d=${d#addr:}; z=${z#Mask:};
                a=${d%%.*}; w=${z%%.*}; d=${d#*.}; z=${z#*.};
                b=${d%%.*}; x=${z%%.*}; d=${d#*.}; z=${z#*.};
                c=${d%%.*}; y=${z%%.*}; d=${d#*.}; z=${z#*.};
                echo $((a&w)).$((b&x)).$((c&y)).$((d&z));
                break;
            fi;
        done`

And here's a better way, inspired by your use of 'tr':

NETADDR=`/sbin/ifconfig |
        while read w x y y; do
            if [ "$w" = "inet" ]; then
                set -- ${x//./ }; a=${1#addr:}; b=$2; c=$3; d=$4;
                set -- ${y//./ }; w=${1#Mask:}; x=$2; y=$3; z=$4;
                echo $((a&w)).$((b&x)).$((c&y)).$((d&z));
                break;
            fi;
        done`

Dave




reply via email to

[Prev in Thread] Current Thread [Next in Thread]