check_as_path/check_as_path.py

30 lines
1.2 KiB
Python
Raw Normal View History

2023-03-04 22:02:15 +01:00
import requests
import argparse
import sys
def check_as_path(prefix, as_to_check):
url = f'https://stat.ripe.net/data/looking-glass/data.json?resource={prefix}'
response = requests.get(url)
if response.ok:
data = response.json()
for rrc in data['data']['rrcs']:
for peer in rrc['peers']:
as_path = peer['as_path']
if as_to_check in as_path:
print(f"CRITICAL: AS {as_to_check} found in AS path for prefix {prefix} at RRC {rrc['rrc']}")
return 2 # return critical
print(f"OK: AS {as_to_check} not found in AS path for prefix {prefix}")
return 0 # return OK
else:
print(f"UNKNOWN: Error fetching data from {url}")
return 3 # return unknown
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Check if a certain AS is in the AS path for any peers for a given prefix')
parser.add_argument('--prefix', required=True, help='IPv4 prefix to check')
parser.add_argument('--asn', required=True, help='AS number to check for')
args = parser.parse_args()
exit_code = check_as_path(args.prefix, args.asn)
sys.exit(exit_code)