Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Create a range of IP Addresses 4

Status
Not open for further replies.

viadisky

Technical User
Jun 19, 2003
110
GB
Hi,

I need to ping a range of IP Addresses. I have this three data to use:

Minimum IP Address: 172.24.2.1
Maximum IP Address: 172.24.3.254
Total No. of Hosts: 510

Basically, I just need to create a script that can generate a list of IP Addresses starting from 172.24.2.1 up to 172.24.3.254

I have a very basic idea of using the "Total No. of Hosts" as the initial value of the counter and then decrementing it everytime my script loops.

Hope somebody can provide some help.

Many thanks :)
 
I am sure there are several ways to do this. Personally I would create a loop for 1 to 254 and then ping address 172.24.2.<loopcounter> and 172.24.3.<loopcounter> . If you wanted to do it in IP Address order then do 172.24.2.<loopcounter> first, reset the loop counter and then do 172.24.3.<loopcounter> .

I hope that helps to get you started.

Mike
 
A starting point:
Code:
#!/bin/ksh
for i in 2 3
do j=0
  while [ $j -le 254 ]
  do ping 172.24.$i.$j
     ((j+=1))
  done
done

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Mike042, thanks a lot for your suggestions, because I'm not that good with scripting yet, I just modified PHV's script.

PHV, I lessened the range of IP Addresses so that it would be quicker for me to test your script. I ran the ping command manually in our server and here are the output:

$ ping 172.21.149.100
172.21.149.100 is alive

$ ping 172.21.149.101
172.21.149.101 is alive

$ ping 172.21.149.103
172.21.149.103 is alive

$ ping 172.21.149.104
no answer from 172.21.149.104

$ ping 172.21.149.105
172.21.149.105 is alive


I did some modifications in your script just to ping 172.21.149.100 up to 172.21.149.105 only.

#!/bin/ksh
for i in 149
do j=100
while [ $j -le 105 ]
do ping 172.21.$i.$j
((j+=1))
done
done

When I ran it, it gave me this error (these lines are repeating, it only stopped when I did manual stop).

$ sh script
172.21.149.100 is alive
script: j+=1: not found
172.21.149.100 is alive
script: j+=1: not found
172.21.149.100 is alive
script: j+=1: not found


It looks like the script is continuously pinging the first IP Address and can't move on to the second one because it is having some problems incrementing variable "j" which represents the last octet of the IP Address.

Did I miss anything?

Thank you.
Maria :)
 
Don't run it with the command "[tt]sh script[/tt]". Do the following...
Code:
chmod +x script
./script
Hope this helps.
 
Or use j=$((1+j))

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Hi PHV,

Either of the lines you suggested are correct: ((j+=1)) or j=$((1+j)) after doing what SamBones suggested, that is to run the script this way:

chmod +x script

then run it using ---> ./script

Because I am novice in Unix scripting, I always use "sh" or "sh -x".I didn't use " #!/bin/ksh " to majority of the simple scripts I created and I just normally run them using sh. The scripts worked but quite limited :-(

Hi Stefanwagner,

I would like to add that -W option in ping but it isn't working. Here is the error:

$ ./script
ping: illegal option -- W
usage: ping host [timeout]
usage: ping -s [-l | U] [adLnRrv] [-A addr_family] [-c traffic_class]
[-g gateway [-g gateway ...]] [-F flow_label] [-I interval]
[-i interface] [-P tos] [-p port] [-t ttl] host [data_size] [npackets]

Many thanks guys!
Maria :)





 
By running a script with "sh script", you are running it with the "Bourne shell". That shell doesn't know a whole lot about arithmetic (doesn't know arithmetic at all).

So, for quick testing of a new script - either run it with "ksh script" or specify the #!/bin/ksh and chmod +x it.

Only option to do arithmetic in Bourne shell is to invoke a command that does understand the sums:

j=`expr $j + 1` # increment j
k=`expr $i \* $j` # multiplication - note you need to hide the * from the shell...

or

j=`echo $j + 1|bc`
k=`echo $i \* $j|bc`
k=`echo "$i * $j"|bc`

all rather tedious - so move up to Korn!


HTH,

p5wizard
 
Hi p5wizard,

The explanation you gave is very helpful for a beginner like me ... worth converting all my small scripts to run in Korn.

Cheers!
Maria :)
 
Hi

Would not be easier to put [tt]nmap[/tt] to do a ping scan on the address range ? I find it more stable then [tt]ping[/tt].
Code:
echo -n "Total number of hosts : "
nmap -sP 172.24.2,3.1-254 | grep -c "appears to be up"

Feherke.
 
In which flavor of *nix does exists such nmap command ?
 
Hi

PHV said:
In which flavor of *nix does exists such nmap command ?
No idea properly. I just asked. But you could have it :
insecure.org/nmap/ said:
Most operating systems are supported, including Linux, Microsoft Windows, FreeBSD, OpenBSD, Solaris, IRIX, Mac OS X, HP-UX, NetBSD, Sun OS, Amiga, and more.

Feherke.
 
Hi PHV,

I was just thinking, if the range of networks I have is really big, how can I increment variable "i" in this loop ... I did try to modify your loop but it keep incrementing variable "j" but can't increase "i" ....

This is the original loop you provided:

#!/bin/ksh
for i in 2 3
do j=0
while [ $j -le 254 ]
do ping 172.24.$i.$j
((j+=1))
done
done

In your loop, 2 and 3 (in line 2) are listed ... how can I increment it in the loop using a counter?

Hope my question isn't very confusing.

Cheers!
 
either use two nested while loops like so:

i=2
while [ i -le 8 ]
do
j=0
while [ $j -le 254 ]
do
echo "172.24.$i.$j"
j=`expr $j + 1`
done
i=`expr $i + 1`
done

or expand your for i in list like so:

for i in 2 3 4 5 6 7 8
do
j=0
while [ $j -le 254 ]
do
echo "172.24.$i.$j"
j=`expr $j + 1`
done
done

HTH,

p5wizard
 
Hi p5wizard,

Thanks for your suggestion, it worked!!!

Cheers,
Maria :)
 
typo error (which you probably found)

i=2
while [ $i -le 8 ]
do
j=0
while [ $j -le 254 ]
do
echo "172.24.$i.$j"
j=`expr $j + 1`
done
i=`expr $i + 1`
done



HTH,

p5wizard
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top