anshul0905:
私は、IPが特定のネットワークに属しているかどうかを確認するために、いくつかのIPアドレスやネットワークを反復したいです。
これは私がこれまでに書かれたものです。
import netaddr, ipaddress
from netaddr import *
IP_found = []
IP_miss = []
dca = ['172.17.34.2', '172.17.33.1', '172.17.35.1', '172.17.36.2']
ip_net = [IPNetwork('172.17.34.0/27'), IPNetwork('172.17.35.0/27')]
for element in ip_net:
temp = ipaddress.ip_network(element)
for ip in dca:
if ipaddress.ip_address(ip) in (temp):
IP_found.append(ip)
break
else:
IP_miss.append(ip)
print(len(IP_found))
print(len(IP_miss))
print(IP_found)
print(IP_miss)
これは私の予想出力されます。
IP_found -> ['172.17.34.2', '172.17.35.1']
IP_miss -> ['172.17.33.1', '172.17.36.2']
私は以下の出力を得ました:
['172.17.34.2', '172.17.35.1']
['172.17.34.2', '172.17.33.1']
ガレス・マ:
import netaddr,ipaddress
from netaddr import *
IP_found = []
IP_miss = []
dca = ['172.17.34.2', '172.17.33.1', '172.17.35.1', '172.17.36.2']
ip_net = [IPNetwork('172.17.34.0/27'), IPNetwork('172.17.35.0/27')]
for ip in dca: # Loops through the ip
if any(ip in ip_subnet for ip_subnet in ip_net): # Loops through subnet
IP_found.append(ip)
else:
IP_miss.append(ip)
print(len(IP_found))
print(len(IP_miss))
print(IP_found)
print(IP_miss)
代わりにこれを試してみてください。