-
Notifications
You must be signed in to change notification settings - Fork 6
/
whatazuresvc
executable file
·54 lines (50 loc) · 2.07 KB
/
whatazuresvc
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
#!/usr/bin/env python3
# use azure public ip tag data to correlate an address to a service
import ipaddress
import json
import sys
import os
import urllib.request
def ip_in_cidr(ip, cidr):
return ipaddress.ip_address(ip) in ipaddress.ip_network(cidr, strict=False)
def find_service_for_ip(ip, data):
for value in data['values']:
for address_prefix in value['properties']['addressPrefixes']:
if ip_in_cidr(ip, address_prefix):
return {
"name": value['name'],
"region": value['properties']['region'],
"systemService": value['properties'].get('systemService', 'Not specified'),
"addressPrefix": address_prefix
}
return None
def download_json():
url = "https://download.microsoft.com/download/7/1/D/71D86715-5596-4529-9B13-DA13A5DE5B63/ServiceTags_Public_20231030.json"
destination = "/tmp/ServiceTags_Public_20230925.json"
try:
urllib.request.urlretrieve(url, destination)
except Exception as e:
print(f"error downloading ServiceTags_Public JSON: {e}")
print("you may need to download new from https://www.microsoft.com/en-us/download/details.aspx?id=56519")
sys.exit(1)
return destination
if __name__ == "__main__":
if len(sys.argv) != 2:
print("usage: whatazuresvc <ip>")
sys.exit(1)
json_path = "/tmp/ServiceTags_Public_20230925.json"
if not os.path.exists(json_path):
print("servicedata datafile not found. downloading...")
json_path = download_json()
with open(json_path, 'r') as file:
data = json.load(file)
ip_address = sys.argv[1]
result = find_service_for_ip(ip_address, data)
if result:
print(f"ip: {ip_address}")
print(f"name: {result['name']}")
print(f"region: {result['region']}")
print(f"system service: {result['systemService'] if result['systemService'] else 'Not specified'}")
print(f"address prefix: {result['addressPrefix']}")
else:
print(f"no service found for IP: {ip_address}")