Generating and printing a range of ip addresses

Peter Otten __peter__ at web.de
Sat Feb 14 04:24:35 EST 2004


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



More information about the Python-list mailing list