Generating and printing a range of ip addresses

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Stephen Briley

    Generating and printing a range of ip addresses

    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
    [color=blue][color=green][color=darkred]
    >>> from ipv4 import *
    >>> ip = IPv4('10.0.0.1' )
    >>> startadd = ip.nexthost()
    >>> endadd = ('10.0.3.0')
    >>> print startadd[/color][/color][/color]
    10.0.0.2[color=blue][color=green][color=darkred]
    >>> print endadd[/color][/color][/color]
    10.0.3.0
    [color=blue][color=green][color=darkred]
    >>> while startadd != endadd:[/color][/color][/color]
    .... startadd = ip.nexthost()
    .... print startadd
    ....
    Traceback (most recent call last):
    File "<interacti ve input>", line 1, in ?
    File "C:\Python23\li b\ipv4.py", line 250, in __cmp__
    return cmp(self._addre ss, other._address)
    AttributeError: 'str' object has no attribute '_address'

    _______________ _______________ ____
    Do you Yahoo!?
    Yahoo! Finance: Get your refund fast by filing online.
    The latest news and headlines from Yahoo! News. Get breaking news stories and in-depth coverage with videos and photos.


  • Peter Otten

    #2
    Re: Generating and printing a range of ip addresses

    Stephen Briley wrote:
    [color=blue]
    > 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
    >[color=green][color=darkred]
    >>>> from ipv4 import *
    >>>> ip = IPv4('10.0.0.1' )
    >>>> startadd = ip.nexthost()
    >>>> endadd = ('10.0.3.0')[/color][/color][/color]

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

    endadd = IPv4('10.0.3.0' )

    instead.
    [color=blue][color=green][color=darkred]
    >>>> print startadd[/color][/color]
    > 10.0.0.2[color=green][color=darkred]
    >>>> print endadd[/color][/color]
    > 10.0.3.0
    >[color=green][color=darkred]
    >>>> while startadd != endadd:[/color][/color]
    > ... startadd = ip.nexthost()
    > ... print startadd
    > ...
    > Traceback (most recent call last):
    > File "<interacti ve input>", line 1, in ?
    > File "C:\Python23\li b\ipv4.py", line 250, in __cmp__
    > return cmp(self._addre ss, other._address)
    > AttributeError: 'str' object has no attribute '_address'[/color]

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

    Peter

    Comment

    • Bart Nessux

      #3
      Re: Generating and printing a range of ip addresses

      Stephen Briley wrote:
      [color=blue]
      > 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?[/color]

      I did it like this:

      def gen_and_write_i ps():
      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.clo se()

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

      Comment

      • Paul McGuire

        #4
        Re: Generating and printing a range of ip addresses

        "Stephen Briley" <sdb1031@yahoo. com> wrote in message
        news:mailman.26 .1076744175.313 98.python-list@python.org ...[color=blue]
        > 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
        >[/color]
        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(sta rtAddr, endAddr):
        def incrAddr(addrLi st):
        addrList[3] += 1
        for i in (3,2,1):
        if addrList[i] == 256:
        addrList[i] = 0
        addrList[i-1] += 1

        def asString(addrLi st):
        return ".".join(map(st r,addrList))

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

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

        for addr in ipAddrRange("10 .255.255.250"," 11.0.0.20"):
        print addr


        Comment

        Working...