Generating and printing a range of ip addresses

Bart Nessux bart_nessux at hotmail.com
Sun Feb 15 04:43:08 CET 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?

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.



More information about the Python-list mailing list