以下部分代碼打印位於子網中的所有主機IP地址,我想修改代碼以便僅打印此列表的起始地址和最後一個地址如何打印第一個和最後一個值?
如何在此處使用數組打印第一個和最後一個值?
import ipaddress
print('enter subnet') # in CIDR Format
x = input()
IP= ipaddress.ip_network(x, strict = False)
for y in IP.hosts():
print(y)
電流輸出
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
希望的輸出
HostMin: 192.0.0.1
HostMax: 192.0.0.6
========================= ================
UPDATE:
使用清單後,我能夠打印的第一和最後一個值
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
http://stackoverflow.com/questions/930397/getting-the-last-element-of-a-list-in-python –