Solved

ping all IPs from it own range (AIX)

Posted on 2011-09-20
7
535 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

Technology Partners: 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

Suggested Solutions

I have been running these systems for a few years now and I am just very happy with them.   I just wanted to share the manual that I have created for upgrades and other things.  Oooh yes! FreeBSD makes me happy (as a server), no maintenance and I al…
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

749 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