[Tutor] print IP address range to stdout
MK
lopoff at gmx.net
Tue Dec 22 13:56:40 CET 2009
Ok. That was very helpful. As i dont know how to do it i googled
and found this one:
http://anonymouse.org/cgi-bin/anon-www.cgi/http://snipplr.com/view/14807/convert-ip-to-int-and-int-to-ip/
But frankly i dont understand it. The program works now like it should
but i want to understand the code i use. So any help would be great.
First function the ip is splitted as i did it. Alright.
The use 256 as it is the maximum for any digit. ok.
But what is that ** and exp meaning ????
----------------------------------------------------------
def ip_to_int(dotted_ip):
exp = 3
intip = 0
for quad in dotted_ip.split('.'):
intip = intip + (int(quad) * (256 ** exp))
exp = exp - 1
return(intip)
---------------------------------------------------
def int_to_ip(intip):
octet = ''
for exp in [3,2,1,0]:
octet = octet + str(intip / (256 ** exp)) + "."
intip = intip % ( 256 ** exp)
return (octet.rstrip("."))
Am Dienstag, den 22.12.2009, 06:32 -0500 schrieb Dave Angel:
>
> MK wrote:
> > Hi there,
> >
> > i have some logical problem. I dont get it done to write my for loops in
> > that way that the ip address range which is given as arguments are
> > correct processed. Meaning that only the ips are printed which the
> > user defines as argument. I tried to make an if statement to stop
> > at the end_adress but it didnt work as it will stop at the end range
> > every time.
> >
> > Here is my program so far:
> >
> > ---------------------------------------------------------
> > sai = start_adress.split(".")
> > eai = end_adress.split(".")
> >
> > # Prüfen auf gültige IP
> > if eai < sai:
> > help_here()
> >
> > #print sai,eai
> >
> > sa1=int(sai[0])
> > sa2=int(sai[1])
> > sa3=int(sai[2])
> > sa4=int(sai[3])
> > ea1=int(eai[0])
> > ea2=int(eai[1])
> > ea3=int(eai[2])
> > ea4=int(eai[3])
> >
> > #print sa1,sa2,sa3,sa4
> > #print ea1,ea2,ea3,ea4
> >
> > e1=ea1+1 # muß um 1 erhöht werden da sonst nur bis ea1-1
> > e2=ea2+1
> > e3=ea3+1
> > e4=ea4+1
> >
> > ip=""
> > for i in range(sa4,255):
> > ip=sai[0]+"."+sai[1]+"."+sai[2]+"."+str(i)
> > print ip
> > print "-------4--------"
> >
> > for i in range(sa3+1,255):
> > for i2 in range(1,255):
> > ip=sai[0]+"."+sai[1]+"."+str(i)+"."+str(i2)
> > print ip
> > print "-------3--------"
> >
> > sa3=sa3+1
> > for i in range(sa2+1,e2):
> > for i2 in range(1,255):
> > for i3 in range(1,255):
> > ip=sai[0]+"."+str(i)+"."+str(i2)+"."+str(i3)
> > print ip
> > print "-------2--------"
> >
> > for i in range(sa1+1,e1):
> > for i2 in range(1,255):
> > for i3 in range(1,255):
> > for i4 in range(1,255):
> > ip=str(i)+"."+str(i2)+"."+str(i3)+"."+str(i4)
> > print ip
> > print "-------1--------"
> >
> > ---------------------------------------------------------
> >
> > The start_adress and end_adress are the ip-range.
> >
> > For example:
> > printdomains.py -s 192.168.178.0 -e 193.170.180.4
> >
> > This should make all ips and stop at the end_adress.
> >
> > Maybe you can help.
> >
> > Thank you.
> >
> > Mac
> >
> >
> >
> Trying to write nested loops as you have done is going to be very
> difficult, as the start and end conditions for each nested loop depends
> on the state of the outer loop.
>
> There are several ways you could accomplish the desired loop, but the
> easiest would probably be to write two functions. The first converts
> from the four integers in the ip address into a single, larger one. And
> the other function converts back. Then the main loop is simply a
> non-nested loop.
>
> def to_integer(ip_string):
> #convert the ip string into a single 32-bit integer
>
> def to_string(ip_int):
> #convert the integer back into a string of four values, with
> periods between
>
> sai = to_integer(start_address)
> eai = to_integer(end_address)
> for ip in xrange(sai, eai):
> result = to_string(ip)
> print result
>
> If you need help writing the two functions, I'm sure many people here
> could help. But try it for yourself. Note that the int you're looking
> for in the first function will be gotten by multiplying the various
> parts of the IP address by different powers of 256.
>
> And note that whatever valid IP address you plug into the first
> function, if you then apply the second function you should get back the
> string you started with. So it should be easy to test while you're
> working on it.
>
> DaveA
More information about the Tutor
mailing list