forked from device42/nix_bsd_mac_inventory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util_ip_operations.py
83 lines (74 loc) · 2.61 KB
/
util_ip_operations.py
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import socket
import sys
import netaddr
class IPOperations:
def __init__(self, targets):
self.ipscope = None
self.targets = targets
def sort_ip(self):
ip_addresses = []
if ',' in self.targets:
self.ipscope = self.targets.split(',')
else:
self.ipscope = [self.targets]
for scope in self.ipscope:
# cannot mix CIDR and iprange notation
if '/' in scope and '-' in scope:
msg = '[!] Mailformed target IP %s ' % scope
print msg
sys.exit()
# CIDR
if '/' in scope:
try:
mask = int(scope.split('/')[1])
except:
msg = '[!] Illegal CIDR mask.'
print msg
sys.exit()
if mask not in range(1, 32):
msg = '[!] Illegal CIDR mask.'
print msg
sys.exit()
valid = self.check_valid_ip(scope.split('/')[0])
if valid:
for ip in netaddr.IPNetwork(scope).iter_hosts():
ip_addresses.append(str(ip))
else:
msg = '[!] Mailformed target IP.'
print msg
sys.exit()
# iprange
elif '-' in scope:
iprange = scope.split('-')
if len(iprange) != 2:
msg = '[!] Mailformed target IP.'
print msg
sys.exit()
else:
for ip in iprange:
valid = self.check_valid_ip(ip)
if not valid:
msg = '[!] Mailformed target IP.'
print msg
sys.exit()
start, end = iprange
ip_list = list(netaddr.iter_iprange(start, end))
for ip in ip_list:
ip_addresses.append(str(ip))
# SINGLE IP
else:
valid = self.check_valid_ip(scope)
if not valid:
msg = '[!] Mailformed target IP.'
print msg
sys.exit()
else:
ip_addresses.append(scope)
return ip_addresses
@staticmethod
def check_valid_ip(ip):
try:
socket.inet_aton(ip)
return 1
except:
return 0