我正在尝试创建一个工具,以便IT应用程序所有者可以检查防火墙接口范围的列表,看看IP是否在防火墙后面,然后无缘无故地打开防火墙票证。这个列表在实际代码中大约有500个in。
如果ip_check返回为True,那么响应是简单而干净的。随着它枚举所有的inter_list,is返回了许多错误的响应。我想要改进我的逻辑,这样用户就会得到是它在防火墙后面还是否它不在防火墙后面。我被困住了,一直在寻找一种更好的方法来做这件事。我计划这是一个flask应用程序。
import ipaddress as ip
Inter_List = ['192.168.1.1/24', '192.168.2.1/24', '192.168.3.1./24']
ip_input = input("Enter IP address:")
print("You entered this IP address:{}".format(ip_input))
for intaddr in Inter_List:
ip_check = ip.IPv4Address(ip_input) in ip.IPv4Network(intaddr, False)
if ip_check == True:
print('IP {} is in {} interface range is behind a firewall'.format(ip_input,intaddr))
else:
print("Not behind a firewall") 发布于 2020-08-12 00:37:48
问题是,对于地址不在其中的每个网络,您都会打印"not each a firewall“。在确定这一点之前,您需要检查所有网络。
import ipaddress as ip
Inter_List = ['192.168.1.1/24', '192.168.2.1/24', '192.168.3.1./24']
ip_input = input("Enter IP address:")
print("You entered this IP address:{}".format(ip_input))
ipaddr = ip.IPv4Address(ip_input)
if any(ipaddr in ip.IPv4Network(intaddr, False) for intaddr in Inter_List):
print('IP {} is in {} interface range is behind a firewall'.format(ip_input,intaddr))
else:
print("Not behind a firewall")发布于 2020-08-12 01:08:29
这里有一种方法:
import ipaddress as ip
Inter_List = ['192.168.1.1/24', '192.168.2.1/24', '192.168.3.1/24']
ip_input = input("Enter IP address:")
def is_behind_firewall(Inter_List, ip_input):
print(f"You entered this IP address:{ip_input}")
ipaddr = ip.ip_network(ip_input, strict=False)
for intaddr in Inter_List:
if ipaddr.overlaps(ip.ip_network(intaddr, strict=False)):
return f'IP {ip_input} is in {intaddr} interface range is behind a firewall'
return "Not behind a firewall"
print(is_behind_firewall(Inter_List, ip_input))https://stackoverflow.com/questions/63362439
复制相似问题