0

The below part code prints all the host IP Address that lies in the Subnet, I want to modify the code so that it prints only the starting address and the last address of this list

How do I use an array here to print the first and last value?

import ipaddress
print('enter subnet')  # in CIDR Format
x = input()
IP= ipaddress.ip_network(x, strict = False)
for 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 the list i was able to print the first and last values

however this takes quite longer to compute whenever i give a large
subnet

like 192.0.0.0/8 takes longer to print the first and last value, 

for: IPV6 address calculations it hangs forever, 
for: example: the  IPV6 address is 2001:db8::1/96 

this list will have 4294967294 elements since this IPV6 subnet has
these many IP address and it hangs forever to print the first and
last element of the list
Rahul
  • 31
  • 4

3 Answers3

2

list[0] and list[-1] gets you the first and 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 for getting first and last IP without generating the whole IP range

import ipaddress
def hosts(IPTYPE):
    """Custom function derived from IPv6Network/IPv4Network.hosts to get only first and 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])
Himaprasoon
  • 2,609
  • 3
  • 25
  • 46
  • however this takes quite longer to compute whenever i give a large subnet, like 192.0.0.0/8 takes longer to print the first and last value, and for IPV6 address calculations it hangs forever, for example if i enter the IPV6 address as 2001:db8::1/96 it has 4294967294 elements and it hangs to print the first and last element of the list – Rahul Apr 07 '17 at 19:17
  • I have updated my answer to include a custom method derived from ipaddress module which gives only the first and last IP instantaneously. – Himaprasoon Apr 07 '17 at 20:34
0

replace for y in IP.hosts(): by y = list(IP.hosts()) and then you can do

print y[0]
print y[-1]

You should read the doc of hosts() function "Returns an iterator over the usable hosts in the network"

Axel
  • 41
  • 1
  • 5
  • this takes quite longer to compute whenever i give a large subnet, like 192.0.0.0/8 takes longer to print the first and last value, and for IPV6 address calculations it hangs forever, for example if i enter the IPV6 address as 2001:db8::1/96 it has 4294967294 elements and it hangs to print the first and last element of the list – Rahul Apr 07 '17 at 19:22
0

You don't use an array here. Use a list!

first_item = list[0]
last_item = list[-1]
kame
  • 20,848
  • 33
  • 104
  • 159