python - How do I print the first and last value? -
the below part code prints host ip address lies in subnet, want modify code prints starting address , last address of list
how use array here print first , last value?
import ipaddress print('enter subnet') # in cidr format x = input() ip= ipaddress.ip_network(x, strict = false) y in ip.hosts(): print(y)
current output
enter subnet 192.0.0.0/29 192.0.0.1 192.0.0.2 192.0.0.3 192.0.0.4 192.0.0.5 192.0.0.6
desired output
hostmin: 192.0.0.1 hostmax: 192.0.0.6
=========================================
update:
after using list able print first , last values
however takes quite longer compute whenever give large subnet 192.0.0.0/8 takes longer print first , last value, for: ipv6 address calculations hangs forever, for: example: ipv6 address 2001:db8::1/96 list have 4294967294 elements since ipv6 subnet has these many ip address , hangs forever print first , last element of list
list[0]
, list[-1]
gets first , last element respectively
import ipaddress print('enter subnet') # in cidr format x = input() ip= ipaddress.ip_network(x, strict = false) k = list(ip.hosts()) print("hostmin: ",k[0]) print("hostmax: ",k[-1])
updated answer getting first , last ip without generating whole ip range
import ipaddress def hosts(iptype): """custom function derived ipv6network/ipv4network.hosts first , last host """ network = int(iptype.network_address) broadcast = int(iptype.broadcast_address) return iptype._address_class(network+1),iptype._address_class(broadcast) print('enter subnet') # in cidr format x = input() ip= ipaddress.ip_network(x, strict = false) m = hosts(ip) print("hostmin: ",m[0]) print("hostmax: ",m[1])
Comments
Post a Comment