-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathazure_ip_check.py
61 lines (53 loc) · 1.34 KB
/
azure_ip_check.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
import ipaddress
import json
import sys
import getopt
def check_network(json_file,ip):
fich_ips = open(json_file,'r')
Azure_ip = json.loads(fich_ips.read())
check = ip
result = ['']
for i in Azure_ip['values']:
for j in i['properties']['addressPrefixes']:
if ipaddress.ip_address(check) in ipaddress.ip_network(j):
result.append('IP: ' + check + ' -> NET: ' + j + ', ID: ' + i['name'] + ', REGION: ' + i['properties']['region'])
break
return(result)
def usage():
print("Command usage:")
print()
print("ip_find.py -f <inputfile> -i <ip_address>")
def main():
inputfile = ""
ipaddr = ""
results = ""
try:
opts, args = getopt.getopt(sys.argv[1:], "hf:i:", ["file=", "ipaddress="])
except getopt.GetoptError as err:
print(err) # will print something like "option -a not recognized"
usage()
sys.exit(2)
output = None
verbose = False
for o, a in opts:
if o in ("-f", "--file"):
inputfile = a
elif o in ("-h", "--help"):
usage()
sys.exit()
elif o in ("-i", "--ipaddress"):
ipaddr = a
else:
assert False, "unhandled option"
exit
if inputfile != "" and ipaddr != "":
results=check_network(inputfile,ipaddr)
else:
usage()
if len(results) == 1:
print("Not found")
else:
for i in results:
print(i)
if __name__== "__main__":
main()