tags:

views:

71

answers:

3

I'd like to make a very simple text (.txt) file. This python program needs to make a list of multiple ranges of IPs in a subnet, each taking up one line.

Example:

10.10.27.1
10.10.27.5
10.10.27.6
10.10.27.26
10.10.27.27
10.10.27.28
10.10.27.29
10.10.27.51
10.10.27.52
10.10.27.53
10.10.27.54

The subnet mask will essentially always be a /24, so providing mask input is not necessary. The program can even default to only supporting a standard class C.

Also, I'd like to support common ranges for devices that we use. A prompt for say, "Printers?" will include .26 - .30. "Servers?" will include .5 - .7. "DHCP?" prompt always will include .51 - .100 of the subnet. "Abnormal?" will include .100 - .254.

Subnet?  10.10.27.1
Servers?  Y
Printers?  Y
DHCP?  Y
Abnormal?  N

Output being:

10.10.27.1
10.10.27.5
10.10.27.6
10.10.27.7
10.10.27.26
10.10.27.27
10.10.27.28
10.10.27.29
10.10.27.30
10.10.27.51 (all the way to .100)

What is the best way to code this?

A: 

I would keep the script simple, just output all addresses as needed :)

def yesno(question):
    output = input(question).lower()

    if output == 'y':
        return True
    elif output == 'n':
        return False
    else:
        print '%r is not a valid response, only "y" and "n" are allowed.' % output
        return yesno(question)

addresses = []

subnet = input('Subnet? ')
# Remove the last digit and replace it with a %d for formatting later
subnet, address = subnet.rsplit('.', 1)
subnet += '%d'
addresses.append(int(address))

if yesno('Servers? '):
    addresses += range(5, 8)

if yesno('Printers? '):
    addresses += range(26, 31)

if yesno('DHCP? '):
    addresses += range(51, 101)

if yesno('Abnormal? '):
    addresses += range(100, 255)

for address in addresses:
    print subnet % address
WoLpH
+1  A: 

It looks like a few for loops are all you need:

network = '10.10.27'

for host in xrange(100, 255):
   print("{network}.{host}".format(**locals()))
Wayne Werner
very cute use of **locals()
Martlark
Thanks, I wish I could claim original credit, but I first saw it somewhere else on SO. Sure beats making your own dict (though I've never done speed tests to compare)
Wayne Werner
A: 

Here is a quick little script I threw together...

import socket
import sys


ranges = {'servers':(5, 8), 'printers':(26, 31), 'dhcp':(51, 101), 'abnormal':(101, 256)}

subnet = raw_input("Subnet? ")

try:
    socket.inet_aton(subnet)
except socket.error:
    print "Not a valid subnet"
    sys.exit(1)

ip_parts = subnet.split(".")
if len(ip_parts) < 3:
    print "Need at least three octets"
    sys.exit(1)
ip_parts = ip_parts[:3]
ip = ".".join(ip_parts)

include_groups = []
last_octets = []

servers = raw_input("Servers? ")
printers = raw_input("Printers? ")
dhcp = raw_input("DHCP? ")
abnormal = raw_input("Abnormal? ")

if servers == "Y":
    include_groups.append('servers')
if printers == "Y":
    include_groups.append('printers')
if dhcp == "Y":
    include_groups.append('dhcp')
if abnormal == "Y":
    include_groups.append('abnormal')

for group in include_groups:
    last_octets.extend([x for x in xrange(ranges[group][0], ranges[group][1])])

print "%s.1" %(ip)
for octet in last_octets:
    print "%s.%s" %(ip, octet)
sberry2A
Thanks guys! I am going to try both of these out and see what works best. I will throw together / modify as I see fit, and post back. You've been a great help.
MisterITGuy