-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathip_utils.py
More file actions
executable file
·31 lines (25 loc) · 1.06 KB
/
Copy pathip_utils.py
File metadata and controls
executable file
·31 lines (25 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
from netaddr import IPAddress, IPNetwork, IPRange, IPSet
class IpUtils (object):
"""Generic Utility for IPv4/IPv6 networks/addresses handling"""
def __init__(self):
pass
def ips_in_range(self, start_ip, end_ip):
""" Retruns all the IPs between two IPv4/v6 addresses; input Ips can be either in format 10.10.10.10/32 or just
10.10.10.10 """
try:
ips = IPRange (IPNetwork (start_ip), IPNetwork (end_ip))
except:
ips=[start_ip,end_ip]
print("Invalid", ips)
return map (str, ips)
def compare_ips(self, ip1, ip2):
"""Compare Two IPv6(Compressed or Uncompressed format) or IPv4 returns True if Logically same"""
if IPNetwork (ip1) == IPNetwork (ip2):
return True
return False
def match_in_set(self, ipaddress, myset):
""" It returns the matched Equavalent Ip address from the list of various notations of same IP"""
for ip in myset:
if self.compare_ips (ipaddress, ip):
return ip
return None