I'm sure this question has been asked a million times already. I have read through some others and am struggling to find an answer.
I am querying the RIPE api en masse, using the following curl command in Debian 9:
file="servers-to-ripe.txt"
while IFS= read -r line
do
# Hostnames -> corresponding IPs
dig=$(./ip_extrapolate2 $line| grep -v $resolving_server)
curl --silent "https://stat.ripe.net/data/address-space-usage/data.json?resource="$dig"&data=asn_name" >> servers.json
done <"$file"
This gives me some JSON output, pertaining to the ownership of said servers. I initially used the jq CLI parser, to no avail.
Thus leading me to write it in Python instead. Here are the first two objects from the list:
{
"status": "ok",
"server_id": "app002",
"status_code": 200,
"version": "0.4",
"cached": false,
"see_also": [],
"time": "2020-01-18T02:44:39.610258",
"messages": [
[
"info",
"IP address (185.230.125.107) has been changed to the closest encompassing prefix/range (185.230.125.0/24) found in RIPE DB"
]
],
"data_call_status": "supported - connecting to ursa",
"process_time": 216,
"build_version": "2020.1.13.174",
"query_id": "20200118024439-c225c628-6317-430d-8244-64f805701675",
"data": {
"assignments": [],
"query_time": "2020-01-16T00:00:00",
"ip_stats": [
{
"status": "LIR Free",
"ips": 256
}
],
"resource": "185.230.125.0/24",
"allocations": [
{
"allocation": "185.230.124.0/22",
"status": "ALLOCATED PA",
"asn_name": "RO-M247EUROPE-OCT-20171108",
"assignments": 0
}
]
}
}{
"status": "ok",
"server_id": "app018",
"status_code": 200,
"version": "0.4",
"cached": false,
"see_also": [],
"time": "2020-01-18T02:44:40.104775",
"messages": [
[
"info",
"IP address (45.9.249.67) has been changed to the closest encompassing prefix/range (45.9.249.0/24) found in RIPE DB"
]
],
"data_call_status": "supported - connecting to ursa",
"process_time": 180,
"build_version": "2020.1.13.174",
"query_id": "20200118024439-33ce2ee1-33a2-42c2-8d9e-acbc92996fe5",
"data": {
"assignments": [
{
"status": "ASSIGNED PA",
"parent_allocation": "45.9.248.0/22",
"address_range": "45.9.249.0/24",
"asn_name": "M247-Dubai"
}
],
"query_time": "2020-01-16T00:00:00",
"ip_stats": [
{
"status": "ASSIGNED PA",
"ips": 256
}
],
"resource": "45.9.249.0/24",
"allocations": [
{
"allocation": "45.9.248.0/22",
"status": "ALLOCATED PA",
"asn_name": "RO-M247-APR1901-20190423",
"assignments": 1
}
]
}
}{
I am trying to pull ONLY the asn_name and the IP-range.
I have tinkered with Python (2.7)'s inbuilt json parser. Here's what I've tried:
#!/usr/bin/python
import json
input_file = open ('servers.json')
json_array = json.load(input_file)
servers = []
for item in json_array:
server_asn_name = {"asn":None, "resource":None}
server_asn_name['asn'] = item['asn_name']
server_asn_name['resource'] = item["resource"]
servers.append(server_asn_name)
print(server_asn_name)
There's a few others, but that's probably the closest I've gotten so far. Any advice would be much appreciated :)