From 2a9c494477ed1d88f97b8b8db98a841c74159b1f Mon Sep 17 00:00:00 2001
From: ivo <ivo@UFO-Net.nl>
Date: Thu, 10 Jun 2010 00:15:51 +0200
Subject: Added bgp graph scripts

---
 doc/ucis.ano/bgp_graph/bgp_path_list_bird.php   | 20 +++++++++++
 doc/ucis.ano/bgp_graph/bgp_path_list_bird.sh    |  3 ++
 doc/ucis.ano/bgp_graph/bgp_path_list_quagga.php | 16 +++++++++
 doc/ucis.ano/bgp_graph/bgp_path_list_quagga.sh  | 21 +++++++++++
 doc/ucis.ano/bgp_graph/bgp_path_list_xml.php    | 48 +++++++++++++++++++++++++
 doc/ucis.ano/bgp_graph/path_list_to_dot.php     | 31 ++++++++++++++++
 doc/ucis.ano/bgp_graph/path_list_to_xml.php     | 17 +++++++++
 doc/ucis.ano/bgp_graph/process_xml.sh           |  6 ++++
 8 files changed, 162 insertions(+)
 create mode 100755 doc/ucis.ano/bgp_graph/bgp_path_list_bird.php
 create mode 100755 doc/ucis.ano/bgp_graph/bgp_path_list_bird.sh
 create mode 100755 doc/ucis.ano/bgp_graph/bgp_path_list_quagga.php
 create mode 100755 doc/ucis.ano/bgp_graph/bgp_path_list_quagga.sh
 create mode 100755 doc/ucis.ano/bgp_graph/bgp_path_list_xml.php
 create mode 100755 doc/ucis.ano/bgp_graph/path_list_to_dot.php
 create mode 100755 doc/ucis.ano/bgp_graph/path_list_to_xml.php
 create mode 100755 doc/ucis.ano/bgp_graph/process_xml.sh

diff --git a/doc/ucis.ano/bgp_graph/bgp_path_list_bird.php b/doc/ucis.ano/bgp_graph/bgp_path_list_bird.php
new file mode 100755
index 0000000..6ef71c7
--- /dev/null
+++ b/doc/ucis.ano/bgp_graph/bgp_path_list_bird.php
@@ -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");
+}
diff --git a/doc/ucis.ano/bgp_graph/bgp_path_list_bird.sh b/doc/ucis.ano/bgp_graph/bgp_path_list_bird.sh
new file mode 100755
index 0000000..747f017
--- /dev/null
+++ b/doc/ucis.ano/bgp_graph/bgp_path_list_bird.sh
@@ -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
diff --git a/doc/ucis.ano/bgp_graph/bgp_path_list_quagga.php b/doc/ucis.ano/bgp_graph/bgp_path_list_quagga.php
new file mode 100755
index 0000000..240dcf3
--- /dev/null
+++ b/doc/ucis.ano/bgp_graph/bgp_path_list_quagga.php
@@ -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");
+}
diff --git a/doc/ucis.ano/bgp_graph/bgp_path_list_quagga.sh b/doc/ucis.ano/bgp_graph/bgp_path_list_quagga.sh
new file mode 100755
index 0000000..97d750f
--- /dev/null
+++ b/doc/ucis.ano/bgp_graph/bgp_path_list_quagga.sh
@@ -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/"
diff --git a/doc/ucis.ano/bgp_graph/bgp_path_list_xml.php b/doc/ucis.ano/bgp_graph/bgp_path_list_xml.php
new file mode 100755
index 0000000..96e2ab6
--- /dev/null
+++ b/doc/ucis.ano/bgp_graph/bgp_path_list_xml.php
@@ -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;
+} 
diff --git a/doc/ucis.ano/bgp_graph/path_list_to_dot.php b/doc/ucis.ano/bgp_graph/path_list_to_dot.php
new file mode 100755
index 0000000..d198331
--- /dev/null
+++ b/doc/ucis.ano/bgp_graph/path_list_to_dot.php
@@ -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('}');
diff --git a/doc/ucis.ano/bgp_graph/path_list_to_xml.php b/doc/ucis.ano/bgp_graph/path_list_to_xml.php
new file mode 100755
index 0000000..d9df7f6
--- /dev/null
+++ b/doc/ucis.ano/bgp_graph/path_list_to_xml.php
@@ -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;
+	}
+}
diff --git a/doc/ucis.ano/bgp_graph/process_xml.sh b/doc/ucis.ano/bgp_graph/process_xml.sh
new file mode 100755
index 0000000..dec83dd
--- /dev/null
+++ b/doc/ucis.ano/bgp_graph/process_xml.sh
@@ -0,0 +1,6 @@
+#!/bin/sh
+for file in ./xml/*
+do
+	echo "Processing $file"
+	./bgp_path_list_xml.php < "$file" > data/`basename "$file"`
+done
-- 
cgit v1.2.3