D. Strout
Resident IPv6 Proponent
Since bgp.he.net has no posted terms of service disallowing it, I wrote a script that, given an ASN, scrapes the site to find number of IPs and list of prefixes with descriptions.
<?php
$asn = trim(ltrim($argv[1], "AS"));
if (!is_numeric($asn)) exit("Invalid ASN!\n");
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "http://bgp.he.net/AS$asn");
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36"); //Scraping with cURL or wget UAs causes 403. UA spoofing here.
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 2); //Don't take too long
curl_setopt($curl, CURLOPT_TIMEOUT, 60);
$asPage = curl_exec($curl);
if (strpos($asPage, "did not return any results")) exit("AS not found!\n"); //No results!
$ips = explode("IPs Originated (v4): ", $asPage)[1];
$ips = str_replace(",", "", explode("<", $ips)[0]); //Parse # of IPs
$prefixes = array();
$prefixHTML = array_splice(explode("/net/", $asPage), 1); //See source of any ASN page to understand this
foreach ($prefixHTML as $prefix) {
$prefixDesc = explode("<td>", $prefix)[1]; //Get description first
$prefixDesc = trim(explode("<div", $prefixDesc)[0]);
$prefix = explode('"', $prefix)[0]; //Since prefix is easier
$prefixes[] = "$prefix - $prefixDesc";
}
if ($ips > 0) echo "AS$asn has $ips IPs. The full list of prefixes has been saved to the file `prefixes`.\n"; //Output
else exit("No IPs in this ASN.\n"); //Amazing how many empty ASNs there are out there. See for instance AS43297.
file_put_contents("prefixes", implode("\n", $prefixes)); //Output full list to file since it's often pretty long.
?>
A little messy, and obviously any use is at your own risk. Careful with that output - it's up to you to make sure it doesn't wipe out anything important. This is written to be run from the terminal. Make sure php5-cli (v 5.4+) and php5-curl are installed, then run with:
<?php
$asn = trim(ltrim($argv[1], "AS"));
if (!is_numeric($asn)) exit("Invalid ASN!\n");
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "http://bgp.he.net/AS$asn");
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36"); //Scraping with cURL or wget UAs causes 403. UA spoofing here.
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 2); //Don't take too long
curl_setopt($curl, CURLOPT_TIMEOUT, 60);
$asPage = curl_exec($curl);
if (strpos($asPage, "did not return any results")) exit("AS not found!\n"); //No results!
$ips = explode("IPs Originated (v4): ", $asPage)[1];
$ips = str_replace(",", "", explode("<", $ips)[0]); //Parse # of IPs
$prefixes = array();
$prefixHTML = array_splice(explode("/net/", $asPage), 1); //See source of any ASN page to understand this
foreach ($prefixHTML as $prefix) {
$prefixDesc = explode("<td>", $prefix)[1]; //Get description first
$prefixDesc = trim(explode("<div", $prefixDesc)[0]);
$prefix = explode('"', $prefix)[0]; //Since prefix is easier
$prefixes[] = "$prefix - $prefixDesc";
}
if ($ips > 0) echo "AS$asn has $ips IPs. The full list of prefixes has been saved to the file `prefixes`.\n"; //Output
else exit("No IPs in this ASN.\n"); //Amazing how many empty ASNs there are out there. See for instance AS43297.
file_put_contents("prefixes", implode("\n", $prefixes)); //Output full list to file since it's often pretty long.
?>
A little messy, and obviously any use is at your own risk. Careful with that output - it's up to you to make sure it doesn't wipe out anything important. This is written to be run from the terminal. Make sure php5-cli (v 5.4+) and php5-curl are installed, then run with:
Code:
./bgp.php 1234
#...OR...
./bgp.php AS1234
Last edited by a moderator: