changeset 28:4dbe5bf653bb draft

Added bgp graph scripts
author ivo <ivo@UFO-Net.nl>
date Thu, 10 Jun 2010 00:15:51 +0200
parents 26b849b22eaa
children 159a026a8bb1
files doc/ucis.ano/bgp_graph/bgp_path_list_bird.php doc/ucis.ano/bgp_graph/bgp_path_list_bird.sh doc/ucis.ano/bgp_graph/bgp_path_list_quagga.php doc/ucis.ano/bgp_graph/bgp_path_list_quagga.sh doc/ucis.ano/bgp_graph/bgp_path_list_xml.php doc/ucis.ano/bgp_graph/path_list_to_dot.php doc/ucis.ano/bgp_graph/path_list_to_xml.php doc/ucis.ano/bgp_graph/process_xml.sh
diffstat 8 files changed, 162 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/doc/ucis.ano/bgp_graph/bgp_path_list_bird.php	Thu Jun 10 00:15:51 2010 +0200
@@ -0,0 +1,20 @@
+#!/usr/bin/php
+<?php
+$mynode = 64766;
+
+$fds = NULL;
+$proc = proc_open('birdc', array(0 => array('pipe','r'), 1 => array('pipe','w'), 2 => STDERR), $fds);
+fwrite($fds[0], "show route all\n");
+fclose($fds[0]);
+$paths = array();
+
+while (!feof($fds[1])) {
+	$line = stream_get_line($fds[1], 1024, "\n");
+	if ($line === NULL || $line === FALSE) break;
+	if (!strlen($line) || $line[0] != "\t") continue;
+	if (substr($line, 0, 14) != "\tBGP.as_path: ") continue;
+	$path = substr($line, 14);
+	if (in_array($path, $paths)) continue;
+	$paths[] = $path;
+	print($mynode.' '.$path."\n");
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/doc/ucis.ano/bgp_graph/bgp_path_list_bird.sh	Thu Jun 10 00:15:51 2010 +0200
@@ -0,0 +1,3 @@
+#!/bin/sh
+LOCALNODE=0
+echo "show route all" | birdc | grep -F "BGP.as_path:" | sed "s/^\tBGP.as_path: \([0-9 ]*\)$/$LOCALNODE \1/" | sort -u
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/doc/ucis.ano/bgp_graph/bgp_path_list_quagga.php	Thu Jun 10 00:15:51 2010 +0200
@@ -0,0 +1,16 @@
+#!/usr/bin/php
+<?php
+$sock = fsockopen('localhost', 2605);
+fread($sock, 1024);
+fwrite($sock, "insecure\n");
+fwrite($sock, "show ip bgp paths\n");
+fwrite($sock, "quit\n");
+while (!feof($sock)) {
+	$line = stream_get_line($sock, 1024, "\r\n");
+	if ($line === NULL || $line === FALSE) break;
+	if (!strlen($line) || $line[0] != '[') continue;
+	$pos = strpos($line, ') ');
+	if ($pos === FALSE) continue;
+	if ($pos == strlen($line) - 2) continue;
+	print(substr($line, $pos+2)."\n");
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/doc/ucis.ano/bgp_graph/bgp_path_list_quagga.sh	Thu Jun 10 00:15:51 2010 +0200
@@ -0,0 +1,21 @@
+#!/bin/sh
+
+LOCALNODE=64520
+
+(sleep 0.2;
+ echo "insecure";
+ echo "show ip bgp neigh";
+ echo "quit";
+) |
+nc localhost 2605 |
+grep "^BGP neighbor is" |
+sed "s/^.*remote AS \([0-9]\+\),.*$/$LOCALNODE \1/"
+
+(sleep 0.2;
+ echo "insecure";
+ echo "show ip bgp paths";
+ echo "quit";
+) |
+nc localhost 2605 |
+grep "[0-9].$" |
+sed "s/^\[0x[a-z0-9]\{8\}:[0-9]\+\] ([0-9]\+) \([0-9 ]\+\)\r$/\1/"
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/doc/ucis.ano/bgp_graph/bgp_path_list_xml.php	Thu Jun 10 00:15:51 2010 +0200
@@ -0,0 +1,48 @@
+#!/usr/bin/php
+<?php
+$mynode = 0;
+
+$file = STDIN;
+$paths = array();
+
+while (!feof($file)) {
+	if (seekto($file, '<as-path>') === FALSE) break;
+	seekto($file, '<segment');
+	seekto($file, '>');
+	$endofsection = FALSE;
+	$path = $mynode;
+	while (!feof($file)) {
+		if (seekto($file, '<') === FALSE) break;
+		switch (fread($file, 4)) {
+			case 'asn>': break;
+			case '/seg': $endofsection = TRUE; break;
+			default: die('unknown tag at '.(ftell($file)-4));
+		}
+		if ($endofsection) break;
+		$asn = seekto($file, '</asn>');
+		$path .= ' '.$asn;
+	}
+	if (in_array($path, $paths)) continue;
+	$paths[] = $path;
+	print($path."\n");
+}
+
+function seekto($f, $str) {
+	$part = '';
+	$i = 0;
+	$len = strlen($str);
+	while ($i < $len && !feof($f)) {
+		$c = fgetc($f);
+		if ($c === FALSE) return FALSE;
+		if ($c == $str[$i]) {
+			$i++;
+		} else {
+			if ($i) {
+				$i = 0;
+				$part = '';
+			}
+			$part .= $c;
+		}
+	}
+	return $part;
+} 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/doc/ucis.ano/bgp_graph/path_list_to_dot.php	Thu Jun 10 00:15:51 2010 +0200
@@ -0,0 +1,31 @@
+#!/usr/bin/php
+<?php
+$nodes = array();
+
+$file = STDIN;
+
+while (!feof($file)) {
+	$line = stream_get_line($file, 1024, "\n");
+	if ($line === NULL) break;
+	if (!strlen($line)) continue;
+	$pathnodes = explode(' ', $line);
+	$prevnode = NULL;
+	foreach ($pathnodes as $node) {
+		if ($prevnode && $node) $nodes[$prevnode][$node] = 1;
+		$prevnode = $node;
+	}
+}
+
+foreach ($nodes as $node => $links) {
+	foreach ($links as $link => $dummy) {
+		if (isset($nodes[$node][$link]) && isset($nodes[$link][$node])) unset($nodes[$link][$node]);
+	}
+}
+
+print("graph BGP_nodes {\n");
+foreach ($nodes as $node => $links) {
+	foreach ($links as $link => $dummy) {
+		print("\t".$node.' -- '.$link.";\n");
+	}
+}
+print('}');
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/doc/ucis.ano/bgp_graph/path_list_to_xml.php	Thu Jun 10 00:15:51 2010 +0200
@@ -0,0 +1,17 @@
+#!/usr/bin/php
+<?php
+$nodes = array();
+
+$file = STDIN;
+
+while (!feof($file)) {
+	$line = stream_get_line($file, 1024, "\n");
+	if ($line === NULL) break;
+	if (!strlen($line)) continue;
+	$pathnodes = explode(' ', $line);
+	$prevnode = NULL;
+	foreach ($pathnodes as $node) {
+		if ($node && $prevnode) print('<link from="'.$node.'" to="'.$prevnode.'" total="1" />'."\n");
+		$prevnode = $node;
+	}
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/doc/ucis.ano/bgp_graph/process_xml.sh	Thu Jun 10 00:15:51 2010 +0200
@@ -0,0 +1,6 @@
+#!/bin/sh
+for file in ./xml/*
+do
+	echo "Processing $file"
+	./bgp_path_list_xml.php < "$file" > data/`basename "$file"`
+done