python 2.7 - dynamic range in slice or array -
i have python script generates ip subnet info netaddr.
from netaddr import * ipnetwork = raw_input("enter ip address: ") ip = ipnetwork(ipnetwork) print "network id:", ip.network print "network broadcast:", ip.broadcast print "subnet mask:", ip.netmask print "prefix length:", ip.prefixlen print "wildcard mask:", ip.hostmask print "network size:", ip.size print "host range is:", ip[1], "-", ip[-2] ip in ipnetwork(ipnetwork).iter_hosts(): print '%s' % ip this works. having trouble "reserve" first , last 3 addresses. here's example of current output:
enter ip address: 192.168.100.17/28 network id: 192.168.100.16 network broadcast: 192.168.100.31 subnet mask: 255.255.255.240 prefix length: 28 wildcard mask: 0.0.0.15 network size: 16 host range is: 192.168.100.17 - 192.168.100.30 192.168.100.17 192.168.100.18 192.168.100.19 192.168.100.20 192.168.100.21 192.168.100.22 192.168.100.23 192.168.100.24 192.168.100.25 192.168.100.26 192.168.100.27 192.168.100.28 192.168.100.29 192.168.100.30 what have result
192.168.100.17 reserved 192.168.100.18 reserved 192.168.100.19 reserved 192.168.100.20 192.168.100.21 192.168.100.22 192.168.100.23 192.168.100.24 192.168.100.25 192.168.100.26 192.168.100.27 192.168.100.28 sw2-vlan 100 192.168.100.29 sw1-vlan 100 192.168.100.30 hsrp vlan 100 i thought slicing work , does, until choose different subnet length. can't find out how dynamically slice address 4 (the first non-reserved) last address minus three. have not been able find solution (slice, array, index) dynamic depending on subnet size minus first , last 3 addresses. pointers appreciated.
i'm confused question. if you're looking manage ipnetwork(ipnetwork).iter_hosts() you'll want just, sake of simplicity, list comprehend them.
def handle(i, data): # each ip, or 'data', can returned # new value fill list or return # objects themselves. if < 3: return str(data) + " reverse" elif > (total - 3): return str(data) else: return str(data) + " sw1-vlan 100" ips_to_manage = [handle(i, addr) i, addr in \ enumerate(ipnetwork(ipnetwork).iter_hosts())] ip in ips_to_manage: print '%s' % ip you'll have find out total yourself. len(ipnetwork(ipnetwork).iter_hosts()) shoud trick. way, can handle them please.
this printing them however, if want else, in return objects themselves, you're more welcome to.
Comments
Post a Comment