[MD5 password]\n"; exit(); } $router = $argv[1]; $peer_as = $argv[2]; if ($argc == 4) $md5_password = $argv[3]; # include configuration from config.txt: include("./config.txt"); # # find all the internet exchanges where the AS is present # # description for netixlan in PeeringDB REST API: # https://www.peeringdb.com/apidocs/#!/netixlan/Network_Ix_Lan_list # $n = 0; $t = peeringdb("netixlan?asn=$peer_as"); for ($i = 0; $i < $t[num]; $i++) { $ix_name = preg_replace("/[^a-z0-9]/", "", strtolower($t[$i][name])); # skip if neither IPv4 nor IPv6 if (!($t[$i][ipaddr4] == "" && $t[$i][ipaddr6] == "")) { # if "test" keyword was provided, display peer information if ($test) { if (strpos($ixes, ":" . $ix_name . ":") === false) echo "$peer_as is present at $ix_name but we aren't\n"; else echo "$peer_as and us are both present at $ix_name!\n"; } } # store peer information if (!(strpos($ixes, ":" . $ix_name . ":") === false)) { $peer_ixname[$n] = $ix_name; $peer_ixid[$n] = $t[$i][ix_id]; $peer_ipaddr4[$n] = $t[$i][ipaddr4]; $peer_ipaddr6[$n] = $t[$i][ipaddr6]; $n++; } } # # start of configuration # $routertype = ""; for ($i = 0; $i < $num_rtrs; $i++) if ($rtr_name[$i] == $router) $routertype = $rtr_type[$i]; if ($routertype == "") { echo "Unknown router $router\n"; exit; } genconfig("begin", $router, "", $routertype, $local_as, $peer_as, "", 0, ""); for ($i = 0; $i < $num_rtrs; $i++) for ($j = 0; $j < $n; $j++) if ($rtr_ixid[$i] == $peer_ixid[$j] && $rtr_name[$i] == $router) #if ($rtr_ixid[$i] == $peer_ixid[$j]) { # # neighbor configurations, first for IPv4 peer address, then IPv6 # genconfig("middle", $router, $peer_ixname[$j], $rtr_type[$i], $local_as, $peer_as, $peer_ipaddr4[$j], 4, $md5_password); genconfig("middle", $router, $peer_ixname[$j], $rtr_type[$i], $local_as, $peer_as, $peer_ipaddr6[$j], 6, $md5_password); } # # end of configuration # genconfig("end", $router, "", $routertype, $local_as, $peer_as, "", 0, ""); # # generate router configuration for a router / peer combination # function genconfig($part, $rtr_name, $ix_name, $rtr_type, $local_as, $peer_as, $ipaddr, $ip_version, $md5_password) { if ($ipaddr != "" || $part != "middle") switch($rtr_type) { case "quagga" : case "brocade" : case "cisco" : genconfig_cisco($part, $rtr_name, $ix_name, $rtr_type, $local_as, $peer_as, $ipaddr, $ip_version, $md5_password); break; case "juniper" : echo "Juniper configurations not supported at this time.\n"; break; default : echo "Unsupported router type $rtr_type.\n"; break; } } # # generate router configuration for a Cisco router (or Quagga or Brocade) # function genconfig_cisco($part, $rtr_name, $ix_name, $rtr_type, $local_as, $peer_as, $ipaddr, $ip_version, $md5_password) { if ($part == "begin") { printf("\n"); printf("conf t\n"); printf("!\n"); printf("! configuration for %s on %s for peer AS%s\n", $rtr_name, date("Y-m-d"), $peer_as); printf("!\n"); if ($rtr_type != "brocade") printf("router bgp %d\n", $local_as); else printf("router bgp\n local-as %d\n", $local_as); } if ($part == "middle") { printf("!\n"); printf(" neighbor %s remote-as %s\n", $ipaddr, $peer_as); # printf(" neighbor %s description ...\n", $ipaddr); if ($md5_password != "") printf(" neighbor %s password %s\n", $ipaddr, $md5_password); if ($ip_version == 4) printf(" neighbor %s peer-group %s-peers-ipv4\n", $ipaddr, $ix_name); else if ($ip_version == 6) { printf(" address-family ipv6 unicast\n"); printf(" neighbor %s peer-group %s-peers-ipv6\n", $ipaddr, $ix_name); printf(" exit-address-family\n"); } else echo "UNKNOWN IP VERSION!"; } if ($part == "end") { printf("!\n"); printf("end\n"); printf("\n"); } } # # 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; }