depot/ops/nlnog/amazon.py

35 lines
1.4 KiB
Python
Executable file

#!/usr/bin/env nix-shell
#!nix-shell -i python3 -p python3 -p python3.pkgs.requests
import ipaddress
import requests
import socket
IP_RANGES_URL = 'https://ip-ranges.amazonaws.com/ip-ranges.json'
NLNOG_PARTICIPANTS_URL = 'https://ring.nlnog.net/scripts/participants.cgi'
def main():
ip_ranges = requests.get(IP_RANGES_URL).json()
for prefix in ip_ranges['prefixes']:
prefix['network'] = ipaddress.ip_network(prefix['ip_prefix'])
nlnog_participants = requests.get(NLNOG_PARTICIPANTS_URL).json()
nlnog_amazon = nlnog_participants["participants"]["75"]
for machine in sorted(nlnog_amazon['machines']):
addrinfos = socket.getaddrinfo(f'{machine}.ring.nlnog.net', 22, proto=socket.IPPROTO_TCP, family=socket.AF_INET)
for addrinfo in addrinfos:
_, _, _, _, (addr, _) = addrinfo
ip_addr = ipaddress.ip_address(addr)
matched_prefix = None
for prefix in ip_ranges['prefixes']:
if ip_addr in prefix['network']:
if not matched_prefix or matched_prefix['service'] == 'AMAZON':
matched_prefix = prefix
else:
print(machine, ip_addr, 'overlap', prefix, matched_prefix)
print(f"\t'{ip_addr}', // AWS region {matched_prefix['region']} - {machine}.ring.nlnog.net")
if __name__ == '__main__':
main()