By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,050 Members | 1,009 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,050 IT Pros & Developers. It's quick & easy.

Generating and printing a range of ip addresses

P: n/a
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
from ipv4 import *
ip = IPv4('10.0.0.1')
startadd = ip.nexthost()
endadd = ('10.0.3.0')
print startadd 10.0.0.2 print endadd 10.0.3.0
while startadd != endadd:

.... 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

Jul 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Stephen Briley wrote:
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
from ipv4 import *
ip = IPv4('10.0.0.1')
startadd = ip.nexthost()
endadd = ('10.0.3.0')
I don't known pynms, but it seems the above shoud be

endadd = IPv4('10.0.3.0')

instead.
print startadd 10.0.0.2 print endadd 10.0.3.0
while startadd != endadd:

... 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
Jul 18 '05 #2

P: n/a
Stephen Briley wrote:
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.
Jul 18 '05 #3

P: n/a
"Stephen Briley" <sd*****@yahoo.com> wrote in message
news:ma*************************************@pytho n.org...
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[i] == 256:
addrList[i] = 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[i] < endAddrList[i]:
incrAddr(curAddrList)
yield asString(curAddrList)

for addr in ipAddrRange("10.255.255.250","11.0.0.20"):
print addr
Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.