Solved

ping all IPs from it own range (AIX)

Posted on 2011-09-20
7
519 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
  • 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
cant install flashplugins for firefox in freebsd 8 9 44
can't umount nfs share after server goes offline... 4 74
OpenLDAP Proxy to Active Directy 6 211
MarkLogic 1 69
Hello fellow BSD lovers, I've created a patch process for patching openjdk6 for BSD (FreeBSD specifically), although I tried to keep all BSD versions in mind when creating my patch. Welcome to OpenJDK6 on BSD First let me start with a little …
Installing FreeBSD… FreeBSD is a darling of an operating system. The stability and usability make it a clear choice for servers and desktops (for the cunning). Savvy?  The Ports collection makes available every popular FOSS application and packag…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
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…

910 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now