473,324 Members | 2,254 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

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
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
3 5530
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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Xah Lee | last post by:
# -*- coding: utf-8 -*- # Python # David Eppstein of the Geometry Junkyard fame gave this elegant # version for returing all possible pairs from a range of n numbers. def combo2(n): return...
10
by: Mario | last post by:
Hello all, I'm trying hard to make possible to print some simple text from python to the default printer using wxPython, after days of internet searches I found this page:...
7
by: eric.gagnon | last post by:
In a program randomly generating 10 000 000 alphanumeric codes of 16 characters in length (Ex.: "ZAZAZAZAZAZAZ156"), what would be an efficient way to ensure that I do not generate duplicates? ...
6
by: Murray Elliot | last post by:
Just wondering if anyone has any ideas on how to solve a particular problem. A client wants to print barcode labels from their (web/php based application). The barcode labels are very small, so I'm...
12
by: Alex Clark | last post by:
Greetings, (.NET 2.0, WinXP Pro/Server 2003, IE6 with latest service packs). I've decided to take advantage of the layout characteristics of HTML documents to simplify my printing tasks, but...
57
by: Robert Seacord | last post by:
i am trying to print the address of a function without getting a compiler warning (i am compiling with gcc with alot of flags). if i try this: printf("%p", f); i get: warning: format %p...
1
by: kukawalkar | last post by:
Hi I'm facing problem while printing Range of pages using Microsoft Common dialog control on vb6. No of copies prints correctly. The code I used : MainMenu.CMDialog1.flags =...
0
by: John Smith | last post by:
Hello, I have 7 different crystal reports that need to be collated. Since I want to end up with a page of each (which all together make a single report), I created a blank main report and then...
1
by: belinda | last post by:
I have used the ff command but only get the date printout and have no clue whats wrong: 'Printing function ' - opening a Word template with bookmarks ' - read bookmarks list from template and...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.