You are on page 1of 3

<?

#
# This script takes as input an AS number and then queries the PeeringDB API
# to find and display all the internet exchanges where this AS is present.
# Then, it obtains a list of all ASes that are also present at these internet
# exchange locations and presents that list, which are potential peers for
# the original AS.
#
# Run the script from the command line with an AS number as the argument.
# If the keyword csv is provided as a second argument, the output will be
# in comma separated value format which can easily be read into a spreadsheet
# or database.
#
# This script is released as open source by Noction, http://www.noction.com/
#

if ($argc != 2 && $argc != 3)


{
echo "Usage: php peerlist.php <ASN> [csv]\n";
exit();
}

$asn = $argv[1];
$csv = $argc == 3 && $argv[2] == "csv";

$gbps = 0;
if (!$csv)
{
echo "\nAS$asn has a presence at:\n\n";
echo "Exchange Mbps RS IPv4 address IPv6 address\n";
}

#
# find all the internet exchanges where our AS is present
#
# description for netixlan in PeeringDB REST API:
# https://www.peeringdb.com/apidocs/#!/netixlan/Network_Ix_Lan_list
#

$t = peeringdb("netixlan?asn=$asn");
for ($i = 0; $i < $t[num]; $i++)
{
$gbps += $t[$i][speed] / 1000.0;
# store IX lan IDs and IX names for later
$ix[$t[$i][ixlan_id]]++;
$ixname[$t[$i][ixlan_id]] = $t[$i][name];
if (!$csv)
printf("%-22.22s %7s %1s %-16s %s\n", $t[$i][name], $t[$i][speed],
$t[$i][is_rs_peer], $t[$i][ipaddr4], $t[$i][ipaddr6]);
}

if (!$csv)
{
echo "\nTotal available bandwidth: $gbps Gbps at $t[num] internet exchange
locations.\n";
echo "\nPotential peers:\n\n";
}
#
# now find all availalbe potential peers at each internet exchange
#
# description for ixlan in PeeringDB REST API:
# https://www.peeringdb.com/apidocs/#!/ixlan/Ix_Lan_list
#

if ($csv)

printf("ix,name,website,asn,info_traffic,info_ratio,info_scope,policy_url,policy_ge
neral,peeringdb_url\n");

foreach($ix as $ixid=>$count)
{
$u = peeringdb("net?ixlan_id=" . $ixid);
for ($j = 0; $j < $u[num]; $j++)
{
if ($u[$j][asn] != $asn)
{
if ($csv)
printf("\"%s\",\"%s\",\"%s\",
%s,\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\"\n", $ixname[$ixid], $u[$j][name],
$u[$j][website], $u[$j][asn], $u[$j][info_traffic], $u[$j][info_ratio], $u[$j]
[info_scope], $u[$j][policy_url], $u[$j][policy_general],
"https://www.peeringdb.com/net/" . $u[$j][id]);
else
printf("%s, %s, %s, %s\n", $ixname[$ixid], $u[$j][name], $u[$j][asn],
$u[$j][policy_general]);
}
$asindex[$u[$j][asn]] = $u[$j][id];
$ixnum[$u[$j][asn]]++;
$ixes[$u[$j][asn]] .= $ixname[$ixid] . " ";
}
}

#
# request information from PeeringDB and return the results as an array
#

function peeringdb($query)
{
$channel = curl_init("https://peeringdb.com/api/$query");
curl_setopt($channel, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($channel);
curl_close($channel);

$j = json_decode($data, true);

$n = 0;
foreach ($j[data] as $num=>$info)
{
foreach ($info as $key=>$val)
{
$t[$n][$key] = $val;
}
$n++;
}

$t[num] = $n;
return $t;
}