Solved

ping all IPs from it own range (AIX)

Posted on 2011-09-20
7
512 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 68

Expert Comment

by:woolmilkporc
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Very nice WMP!!

you rock man!!!

Thanks.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
AIX 5.3 group password policy changes 7 22
sed -- search an replace multiple pattern 11 53
NTP Server in VMware 5 115
MarkLogic 1 48
Let's say you need to move the data of a file system from one partition to another. This generally involves dismounting the file system, backing it up to tapes, and restoring it to a new partition. You may also copy the file system from one place to…
In tuning file systems on the Solaris Operating System, changing some parameters of a file system usually destroys the data on it. For instance, changing the cache segment block size in the volume of a T3 requires that you delete the existing volu…
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…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

744 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