Generating and printing a range of ip addresses

S

Stephen Briley

Hi all,

I would like to write a python script that takes input
of 2 ip address, one a start address and the other the
end address and prints a list of all ip address in
between in dotted-decimal format. I've attempted to
use the ipv4 module
(http://pynms.sourceforge.net/ipv4.html) ,but I am
unable to get past this error "AttributeError: 'str'
object has no attribute '_address".

Can any suggest a solution to this problem? Is there a
better way than using the ipv4 module?

Thanks,

Steve
.... startadd = ip.nexthost()
.... print startadd
....
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
File "C:\Python23\lib\ipv4.py", line 250, in __cmp__
return cmp(self._address, other._address)
AttributeError: 'str' object has no attribute '_address'

__________________________________
Do you Yahoo!?
Yahoo! Finance: Get your refund fast by filing online.
http://taxes.yahoo.com/filing.html
 
P

Peter Otten

Stephen said:
Hi all,

I would like to write a python script that takes input
of 2 ip address, one a start address and the other the
end address and prints a list of all ip address in
between in dotted-decimal format. I've attempted to
use the ipv4 module
(http://pynms.sourceforge.net/ipv4.html) ,but I am
unable to get past this error "AttributeError: 'str'
object has no attribute '_address".

Can any suggest a solution to this problem? Is there a
better way than using the ipv4 module?

Thanks,

Steve

I don't known pynms, but it seems the above shoud be

endadd = IPv4('10.0.3.0')

instead.
... startadd = ip.nexthost()
... print startadd
...
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
File "C:\Python23\lib\ipv4.py", line 250, in __cmp__
return cmp(self._address, other._address)
AttributeError: 'str' object has no attribute '_address'

Reading the traceback carefully should give the right clue. A str object
occurs where you would expect an IPv4 instance.

Peter
 
B

Bart Nessux

Stephen said:
Hi all,

I would like to write a python script that takes input
of 2 ip address, one a start address and the other the
end address and prints a list of all ip address in
between in dotted-decimal format. I've attempted to
use the ipv4 module
(http://pynms.sourceforge.net/ipv4.html) ,but I am
unable to get past this error "AttributeError: 'str'
object has no attribute '_address".

Can any suggest a solution to this problem? Is there a
better way than using the ipv4 module?

I did it like this:

def gen_and_write_ips():
output_File = file('ips.txt', 'w')
# Generate and write our 120 IP range to a file
# Produces 128.173.120.50-80
x = 49
while x < 80:
x = x + 1
print>> output_File, "128.173.120.%d" %x

# IP Addresses that are not part of a range
# These are in the 128.173.120.0 network
lone_IP = [121, 140, 248]

# Write the lone IPs into the file
for i in lone_IP:
print>> output_File, "128.173.120.%d" %i

# Generate and write our 122 IP range to a file
# Produces 128.173.122.1-90
x = 0
while x < 90:
x = x + 1
print>> output_File, "128.173.122.%d" %x
output_File.close()

Your needs sound different, but it's something you could start with.
 
P

Paul McGuire

Stephen Briley said:
Hi all,

I would like to write a python script that takes input
of 2 ip address, one a start address and the other the
end address and prints a list of all ip address in
between in dotted-decimal format. I've attempted to
use the ipv4 module
(http://pynms.sourceforge.net/ipv4.html) ,but I am
unable to get past this error "AttributeError: 'str'
object has no attribute '_address".

Can any suggest a solution to this problem? Is there a
better way than using the ipv4 module?

Thanks,

Steve
Here's a generator function that works with two strings containing valid IP
addresses.
(Note: no error checking, such as verifying that endAddr > startAddr,
endAddr < 255.255.255.255, etc.)

def ipAddrRange(startAddr, endAddr):
def incrAddr(addrList):
addrList[3] += 1
for i in (3,2,1):
if addrList == 256:
addrList = 0
addrList[i-1] += 1

def asString(addrList):
return ".".join(map(str,addrList))

startAddrList = map(int,startAddr.split("."))
endAddrList = map(int,endAddr.split("."))

curAddrList = startAddrList[:]
yield asString(curAddrList)
for i in range(4):
while curAddrList < endAddrList:
incrAddr(curAddrList)
yield asString(curAddrList)

for addr in ipAddrRange("10.255.255.250","11.0.0.20"):
print addr
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top