Solved

ping all IPs from it own range (AIX)

Posted on 2011-09-20
7
545 Views
Last Modified: 2013-11-17
OK,

I need a script which take the range of IPs  from the IP and mask on the server it's running and ping, with -c 1 option, theses IPs  to see which responds or not. I would like to exclude the network and braodcast address. I just want to see which IPs are used or not.

NOTE: There's no nmap installed on our AIX boxes. (With nmap -sP uou can do this job)

Thanks..

0
Comment
Question by:sminfo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 4

Expert Comment

by:MarioAlcaide
ID: 36565721
Hi, you could do this:

subnet=$(echo $begin | sed 's/\.[0-4]*$//')

start=$(echo $begin | sed 's/.*\.//')
stop=$(echo $end | sed 's/.*\.//')

while [[ $start -le $stop ]]
do
  ping -c 1 $subnet.$start
  ((start++))
done
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 36565978
Hi,

I don't think we must reinvent the wheel here (at least not as a whole).

So I'll attach two scripts.

The first one is a liittle perl script from (anonymous) internet which would extract
the network address and broadcast address from a given ip/mask combo.

The second is a ksh script I made to ping the IP range between the values found
by the above script, omitting the net and broadcast addresses.

The perl script is called as /usr/local/bin/iprange.pl in the ksh script. Change as desired.

Please note that the ksh script uses the "seq" utility from GNU coreutils
 (http://www.perzl.org/aix/index.php?n=Main.Coreutils)
We could go without it, but this would blow up the ksh script a lot.
Note further the "echo" statement in front of ping. It's for you to test.

Run the script like this:

scriptname ipaddr mask
e.g.
pingrange 192.168.1.1 255.255.255.0

Have fun!

wmp
#!/usr/bin/perl
my $ipaddr=$ARGV[0];
my $nmask=$ARGV[1];
my @addrarr=split(/\./,$ipaddr);
my ( $ipaddress ) = unpack( "N", pack( "C4",@addrarr ) );
my @maskarr=split(/\./,$nmask);
my ( $netmask ) = unpack( "N", pack( "C4",@maskarr ) );
my $netadd = ( $ipaddress & $netmask );
my @netarr=unpack( "C4", pack( "N",$netadd ) );
my $netaddress=join(".",@netarr);

print "Network address : $netaddress \n";

my $bcast = ( $ipaddress & $netmask ) + ( ~ $netmask );
my @bcastarr=unpack( "C4", pack( "N",$bcast ) ) ;
my $broadcast=join(".",@bcastarr);

print "Broadcast address: $broadcast\n";

Open in new window

#!/bin/ksh
/usr/local/bin/iprange.pl $1 $2 | while read line
 do
  echo $line | grep -q "Network" && NET=$(echo $line |awk '{print $NF}')
  echo $line | grep "Network" && set -A beginarray $(echo $line | awk -F":|\\\." '{print $2, $3, $4, $5}')
  echo $line | grep -q "Broadcast" && BRO=$(echo $line |awk '{print $NF}')
  echo $line | grep "Broadcast" && set -A endarray $(echo $line | awk -F":|\\\." '{print $2, $3, $4, $5}')
 done
for s1 in $(seq ${beginarray[0]} ${endarray[0]})
 do
   for s2 in $(seq ${beginarray[1]} ${endarray[1]})
    do
      for s3 in $(seq ${beginarray[2]} ${endarray[2]})
       do
         for s4 in $(seq ${beginarray[3]} ${endarray[3]})
          do
            ADDR=$s1"."$s2"."$s3"."$s4
            [[ $ADDR != $NET && $ADDR != $BRO ]] && echo ping -c1 $ADDR
          done
       done
    done
 done

Open in new window

0
 

Author Comment

by:sminfo
ID: 36566071
Mario,

it didn't work for me:
(aix):[root] /tmp -> ./findip
0821-062 ping: host name . NOT FOUND
./findip[11]: start++: 0403-009 The specified number is not valid for this command.

wmp,

I though there's was a simpler way... I want to run this script inside our IVMs to see the "not used" IPs for our VLANs segments. This IVM does not have coreutils installed. I tried to make the script using pure ksh but I couldn't, mainly because of time . But well, as you said above, it seems I want to reinvent the wheel :-). Come on, show the people your ksh skills.. :-) [joke]
But I suppose it's not easy to do.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 68

Expert Comment

by:woolmilkporc
ID: 36566131
To extract the first ip/mask combo of an IVM:

lstcpip -stored | grep -E "address|Mask" | head -2 | awk '{printf $NF " "} END {print}'

Working on the script ...
0
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 500 total points
ID: 36566740
Here you go:
#!/bin/ksh
/usr/local/bin/iprange.pl $1 $2 | while read line
 do
  echo $line | grep -q "Network" && NET=$(echo $line |awk '{print $NF}')
  echo $line | grep "Network" && set -A beginarray $(echo $line | awk -F":|\\\." '{print $2, $3, $4, $5}')
  echo $line | grep -q "Broadcast" && BRO=$(echo $line |awk '{print $NF}')
  echo $line | grep "Broadcast" && set -A endarray $(echo $line | awk -F":|\\\." '{print $2, $3, $4, $5}')
 done
s1=${beginarray[0]}; s2=${beginarray[1]}; s3=${beginarray[2]}; s4=${beginarray[3]}
while [[ $s1 -le ${endarray[0]} ]]
 do
   while [[ $s2 -le ${endarray[1]} ]]
    do
      while [[ $s3 -le ${endarray[2]} ]]
       do
         while [[ $s4 -le ${endarray[3]} ]]
          do
            ADDR=$s1"."$s2"."$s3"."$s4
            [[ $ADDR != $NET && $ADDR != $BRO ]] && echo ping -c1 $ADDR
            ((s4+=1))
          done
          ((s3+=1))
          s4=${beginarray[3]}
       done
       ((s2+=1))
       s3=${beginarray[2]}
    done
    ((s1+=1))
    s2=${beginarray[1]}
 done

Open in new window

0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 36566804
Finally, here's an example how to get the first ip/mask combo with native AIX (OEM):

ifconfig -a |grep inet | head -1 |awk '{printf "%s %d.%d.%d.%d\n", $2, "0x" substr($4,3,2), "0x" substr($4,5,2), "0x" substr($4,7,2), "0x" substr($4,9,2)}'
0
 

Author Closing Comment

by:sminfo
ID: 36566847
Very nice WMP!!

you rock man!!!

Thanks.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question