query an api to determine if an autonomous system uses rpki in python

To determine if an Autonomous System (AS) uses Resource Public Key Infrastructure (RPKI) validation, you can query the RPKI validation state of a BGP Prefix by using the IRR REST API provided by RADB (https://www.radb.net/rpki/irr_api_doc).

Here's an example Python code using Requests library to query the API:

main.py
import requests

def check_rpki_validation(asn, prefix):
    url = f"https://api.radb.net/api/rpki/prefix/{asn}/{prefix}"
    response = requests.get(url)
    data = response.json()
    return data["validated"]
211 chars
8 lines

In this code, the check_rpki_validation function takes in two parameters - the Autonomous System number (asn) and the BGP Prefix (prefix). The function constructs the API URL by formatting the asn and prefix parameters into the URL string. The API returns a JSON response and the function extracts the validated field from the response data.

The validated field will have a boolean value - True if the BGP Prefix is validated by RPKI or False if it's not validated by RPKI.

You can call this function with the AS number and BGP prefix you want to query:

main.py
print(check_rpki_validation(64512, "192.0.2.0/24"))
52 chars
2 lines

This will output True or False indicating if the BGP Prefix is validated by RPKI.

gistlibby LogSnag