On Fri, Feb 16, 2001 at 03:32:10PM +0000 or thereabouts, Padraig Brady wrote:
> How about after each counterx=$((counterx+1)):
> if [ $counterx -gt 255 ] then break;
> ?
>> Also should the 2nd last line be counter1 and
> not counter 4?
>> Padraig.
>> John P . Looney wrote:
>> > Here's a little puzzler. Seeing as "seq" only works in decimals, it's a
> > little tricky.
> >
2 problems to deal with here:
> > Basically, if someone gives me a pair of IP's, like
> >
> > 10.55.2.9
> > and
> > 10.10.29.2
> >
> > I want something like a "for $ip in $ips" type loop. I'm thinking at the
> > moment, something like:
> >
> > IP_start=10.10.2.9
> > IP_end=10.10.6.0
1. You need to wrap from .9 to .0 via .255
2. IP=10.10.3.2 will never get scanned using IP_start=9
> >
> > for i in `seq 1 4`
> > do
> > eval "IP_start_$i=`echo $IP_start | cut -f$i -d. `"
> > eval "IP_end_$i=`echo $IP_end | cut -f$i -d. `"
# Fix for ##--1--## below
if [ $IP_start_$i -gt $IP_end_$i ]; then
IP_end_$i=`expr $IP_end_$i + 255`
> > done
> >
> > counter1=$IP_start_1
> > while [ $counter1 -le $IP_end_1 ] ; do
> > counter2=$IP_start_2
> > while [ $counter2 -le $IP_end_2 ] ; do
> > counter3=$IP_start_3
> > while [ $counter3 -le $IP_end_3 ] ; do
> > counter4=$IP_start_4
> > while [ $counter4 -le $IP_end_4 ] ; do
##--1--##
# This will fall over for the example above since $IP_start_4 is always
# greater than $IP_end_4
# The fix starts above at assignment time and continues with Padraig's
if [ $counterx -gt 255 ] then break;
# a fix for ##--2--## is a bit harder. I'd rewrite the loop bigtime for
# this. Fix attempt below.
> > echo "$counter1.$counter2.$counter3.$counter4"
> > counter4=$((counter4+1))
> > done
> > counter3=$((counter3+1))
> > done
> > counter2=$((counter2+1))
> > done
> > counter4=$((counter4+1))
> > done
> >
> > But, how do I do the "base 256" thing ?
##--2--##
# let's look at counter3 and counter4
# first, we need to take IP_start and IP_end out of the comparisons. They
# can be replaced with $start4 and $end4 etc.
# if counter3=IP-start_3 then we start with counter4=IP-start_4, otherwise we
# need to start with counter4=0. if counter3=ip_end_3 then we finish with
# counter4=IP_end_4, otherwise we finish with counter4=255. Now, its
# easy...
while [ $counter3 -le $end3 ] ; do
# if counter3 is at the beginning of its range, then counter4 need start
# only from the IP_start_4 point, otherwise it must start from zero
if [ $counter3 -eq $IP_start_3 ]; then
start4=$IP_start_4
else
start4=0
fi
# similarly, if counter3 is at the beginning of its range, then counter4 may
# end at the IP_end_4 point, otherwise it must go up to 255.
if [ $counter3 -eq $IP_end_3 ]; then
# because IP_end_4 could be greater than 255 (see ##--1--## above), we need
# to take account of that when choosing the end point. The same may be the
# case for IP_end_3 so the condition above may need work also. I'm kinda
# thinking the IP_end_$i + 255 thing above may not be necessary using this
# kind of loop but I'll leave that as an exercise for the reader (or I can't
# be bothered figuring it out!-)
if [ $IP_end_4 -gt 255 ];then
end4=`expr $IP_end_4 - 255`
else
end4=$IP_end_4
fi
else
end4=255
fi
# I'm not sure if "start4" is required at all or if you could just use
# "counter4" in the initialisation above
counter4=$start4
while [ $counter4 -le $end4 ] ; do
counter4=$((counter4+1))
done
counter3=$((counter3+1))
done
If we remove the "IP_end_$i + 255" from the script, the loop simplifies to
while [ $counter3 -le $end3 ] ; do
if [ $counter3 -eq $IP_start_3 ]; then
counter4=$IP_start_4
else
counter4=0
fi
if [ $counter3 -eq $IP_end_3 ]; then
end4=$IP_end_4
else
end4=255
fi
while [ $counter4 -le $end4 ] ; do
counter4=$((counter4+1))
done
counter3=$((counter3+1))
done
That's yer lot...
--
Conor Daly
Met Eireann, Glasnevin Hill, Dublin 9, Ireland
Ph +353 1 8064217 Fax +353 1 8064275
------------------------------------
11:19am up 2:41, 6 users, load average: 1.14, 1.11, 1.09
Maintained by the ILUG website team. The aim of Linux.ie is to
support and help commercial and private users of Linux in Ireland. You can
display ILUG news in your own webpages, read backend
information to find out how. Networking services kindly provided by HEAnet, server kindly donated by
Dell. Linux is a trademark of Linus Torvalds,
used with permission. No penguins were harmed in the production or maintenance
of this highly praised website. Looking for the
Indian Linux Users' Group? Try here. If you've read all this and aren't a lawyer: you should be!