-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathipcheck.py
More file actions
173 lines (147 loc) · 5.37 KB
/
ipcheck.py
File metadata and controls
173 lines (147 loc) · 5.37 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
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/usr/bin/python3
# Copyright 2019-2021 Wes Moskal-Fitzpatrick
# Licensed under Apache Version 2.0
import ipaddress
import csv
import argparse
from argparse import RawTextHelpFormatter
from cidrize import cidrize
import sys
parser = argparse.ArgumentParser(description='IP Subnet Checker',formatter_class=RawTextHelpFormatter)
parser.add_argument('-s', '--subnet', dest='subnet', type=str, required=False, help='A subnet to check against.\n\n', metavar='<subnet>')
parser.add_argument('-S', '--subnets_file', dest='subnets_file', type=str, required=False, help='CSV file containing a list of subnets to check against.\n\nFormat - IPAddress, Name\n\n', metavar='<filename>')
parser.add_argument('-i', '--ip_address', dest='ip_address', type=str, required=False, help='IP address to check in subnet.\n\n', metavar='<ip address>')
parser.add_argument('-I', '--ip_file', dest='ip_file', type=str, required=False, help='CSV file containing IP addresses to check in subnet.\n\nFormat - Subnet, Name\n\n', metavar='<filename>')
parser.add_argument('-o', '--output_file', dest='output_file', type=str, required=False, help='Specify a CSV output_file file.\n\n', metavar='<filename>')
args = parser.parse_args()
subnet = args.subnet
subnets_file = args.subnets_file
ip_addr = args.ip_address
ip_file = args.ip_file
output_file = args.output_file
subnets_list = []
if subnets_file:
with open(subnets_file) as file:
subnets_list = list(csv.reader(file))
if not ip_addr and not ip_file:
parser.print_help()
sys.exit(1)
def range_to_ips(iprange):
list_of_ips = []
if not iprange:
return list_of_ips
try:
ipaddr = ipaddress.ip_address(iprange)
list_of_ips.append(ipaddr)
return list_of_ips
except ValueError:
pass
try:
ipaddrs = ipaddress.ip_network(iprange)
list_of_ips.extend(ipaddrs)
return list_of_ips
except ValueError:
pass
try:
ipaddrs = ipaddress.ip_network(iprange, strict=False)
print('Address %s is not valid CIDR syntax, using recommended CIDR: %s' % (iprange, ipaddrs))
list_of_ips.extend(ipaddrs)
return list_of_ips
except ValueError:
pass
try:
cidrip = cidrize(iprange)
for cidr in cidrip:
for ipaddr in ipaddress.ip_network(cidr):
list_of_ips.append(ipaddr)
except Exception:
print('Address %s is not valid CIDR syntax, cannot process!' % (iprange))
return list_of_ips
def validate_subnet(subnet):
if not subnet:
return None
try:
ip = ipaddress.ip_address(subnet)
return ipaddress.ip_network(ip)
except ValueError:
pass
try:
return ipaddress.ip_network(subnet)
except ValueError:
pass
try:
subbed = ipaddress.ip_network(subnet, strict=False)
print('Address %s is not valid CIDR syntax, using recommended CIDR: %s' % (subnet, subbed))
return subbed
except ValueError:
pass
try:
cidrip = cidrize(subnet)
return ipaddress.ip_network(cidrip)
except Exception:
print('Subnet %s is not valid CIDR syntax, cannot process!' % (subnet))
def output(ip, ip_name, valid_subnet, subnet, sub_name):
if not valid_subnet:
return mapped
in_net = ip in valid_subnet
values = [str(ip), ip_name, str(valid_subnet), sub_name, in_net, ip_addr, subnet]
status = "found in" if in_net else "NOT in"
msg = ("IP Address", values[0], status, values[2], "( Inputted Address:", values[5], "Inputted Subnet:", values[6], ")")
if not output_file:
print(' '.join(str(s) for s in msg))
mapped.append(values)
return mapped
def checkIPs(ip_addr, ip_name=None):
ip_list = range_to_ips(ip_addr)
for ip in ip_list:
if subnet:
validated = validate_subnet(subnet)
output(ip, ip_name, validated, subnet, None)
elif subnets_file:
with open(subnets_file) as file:
reader = csv.reader(file)
for line in reader:
subnet_ip = line[0]
sub_name = line[1]
validated = validate_subnet(subnet_ip)
output(ip, ip_name, validated, subnet_ip, sub_name)
mapped.sort(key=lambda k: ipaddress.ip_address(k[0]))
return mapped
def main():
mapped = []
if ip_addr:
mapped = checkIPs(ip_addr, mapped)
mapped.insert(0, [
"IP Address",
"IP Name",
"Subnet",
"Subnet Name",
"In Subnet",
"Inputted Address",
"Inputted Subnet",
])
elif ip_file:
with open(ip_file) as file:
reader = csv.reader(file)
count = 0
for line in reader:
ip_item = line[0]
ip_name = line[1]
mapped = checkIPs(ip_item, mapped, ip_name)
mapped.insert(0, [
"IP Address",
"IP Name",
"Subnet",
"Subnet Name",
"In Subnet",
"Inputted Address",
"Inputted Subnet",
])
print(len(mapped) - 1, "IP addresses processed.\n")
if output_file:
with open(output_file, "w", newline="") as file:
writer = csv.writer(file)
writer.writerows(mapped)
print("Results saved to", output_file, "\n")
if __name__ == "__main__":
main()