1755067 Members
3450 Online
108829 Solutions
New Discussion юеВ

Re: while loop help

 
SOLVED
Go to solution
lawrenzo
Trusted Contributor

while loop help

Hi all,

I am checking a range of IP addresses and would like to manipulate the second octet once the third octet has reached 254.

here is what I have so far:

#!/bin/ksh

IP1=0
IP2=10


while [ $IP1 -lt 255 ]
do

IP1=$((IP1+1))

for NET in 68 200 201 202
do

ping -w 1 -c 1 10.$IP2.$IP1.$NET

if [[ $? -ne 0 ]] ; then

echo "ERROR: IP 10.$IP2.$IP1.$NET cannot be connected" > netfile.error
else
echo "OK: IP 10.$IP2.$IP1.$NET can be connected" > netfile.ok
fi
done
done

once all 10.10.254.x have been ping'd i then need to check all ranges for

10.11.x.x
10.12.x.x

how can I use the syntax to add one more range to the second octet?

Thanks

Chris
hello
5 REPLIES 5
Luk Vandenbussche
Honored Contributor

Re: while loop help

Chris,

-w is not an option for ping under HPUX.
Can you try without this option
James R. Ferguson
Acclaimed Contributor
Solution

Re: while loop help

Hi Chris:

Simply add another (outer) loop. For example:

#!/usr/bin/sh
typeset -i IP1=0

for IP2 in 10 11 12
do
IP1=0
while [ ${IP1} -lt 255 ]
do
IP1=$((IP1+1))
for NET in 68 200 201 202
do
ping -w 1 -c 1 10.${IP2}.${IP1}.${NET}
if [[ $? -ne 0 ]] ; then
echo "ERROR: IP 10.${IP2}.${IP1}.${NET} cannot be connected" \
> netfile.error
else
echo "OK: IP 10.${IP2}.${IP1}.${NET} can be connected" > netfile.ok
fi
done
done
done

...Notice, too, that I changed your variable declaration to a typeset integer for faster arithmetic and surrounded your variables with curly braces --- a good habit to learn to avoid ambiguity in parameter substitution.

Regards!

...JRF...
lawrenzo
Trusted Contributor

Re: while loop help

sorry I should have mentioned that I am running this on an AIX system therefor -w 1 is the ping timeout.

I have actually worked out a solution however could do with a more advanced script:

#!/bin/ksh

IP1=0
IP2=10
>netfile.error
>netfile.ok

while [[ $IP1 -lt 255 && $IP2 -lt 13 ]]
do

if [[ $IP1 -lt 254 ]] ; then

IP1=$IP1

else
IP1=0
IP2=$((IP2+1))

fi

IP1=$((IP1+1))

for NET in 68 200 201 202
do

ping -w 1 -c 1 10.$IP2.$IP1.$NET

if [[ $? -ne 0 ]] ; then

echo "ERROR: IP 10.$IP2.$IP1.$NET cannot be connected" >> netfile.error
else
echo "OK: IP 10.$IP2.$IP1.$NET can be connected" >> netfile.ok
fi
done
done

not use if this is working correctly as it is running through now.

What I would like is a solution that allows me to check all IP's on a range of networks and each octet being incremented:

check

10.1.1.2
10.1.1.3
etc

then check

10.1.2.1
10.1.2.2
10.1.2.3
etc

then check

10.2.1.1
10.2.1.2
10.2.1.3
etc

how can I easily increment each octet?

many thanks in advance
hello
James R. Ferguson
Acclaimed Contributor

Re: while loop help

Hi (again) Chris:

> What I would like is a solution that allows me to check all IP's on a range of networks and each octet being incremented:

Again, nested loops to increment each octet is all that you need. Simply be sure to re-initialize the low-order octets everytime you increment a higher-order one. I showed the addition of a 'for' loop only because it appeared that you wanted only a few ranges. Use multiple 'while' loops.

Regards!

...JRF...
A. Clay Stephenson
Acclaimed Contributor

Re: while loop help

How about a completely different approach that will get them all (at least for a subnet) in one shot? Rather than ping each IP address, ping the broadcast address and see who responds.
If it ain't broke, I can fix that.