diff options
author | Joachim Tingvold <joachim@tingvold.com> | 2015-04-01 03:49:53 +0200 |
---|---|---|
committer | root <root@einstein.tg15.gathering.org> | 2015-04-01 03:49:53 +0200 |
commit | 41b1eca7a62d450bc0d2357027a6be6acd51a7a6 (patch) | |
tree | f7f5ceb504cc46e990d04d2da36f82dff7ff566b | |
parent | ee87dca5e68e0f8521163b5603717742b248e6b1 (diff) |
Changes...
-rw-r--r-- | fap/README.md | 27 | ||||
-rw-r--r-- | fap/database/create_tables.sql | 17 | ||||
-rw-r--r-- | fap/httpd/OLD - DEPRECATED/# ex2200.template (renamed from fap/httpd/ex2200.template) | 0 | ||||
-rw-r--r-- | fap/httpd/OLD - DEPRECATED/# server_http.py (renamed from fap/httpd/server_http.py) | 0 | ||||
-rw-r--r-- | fap/httpd/OLD - DEPRECATED/# terminal.log (renamed from fap/httpd/terminal.log) | 0 | ||||
-rw-r--r-- | fap/httpd/apache_base_config | 15 | ||||
-rw-r--r-- | fap/tools/create_queries/create_queries.php | 62 | ||||
-rw-r--r-- | fap/tools/tools_includes/ipcalc_functions.php | 134 |
8 files changed, 243 insertions, 12 deletions
diff --git a/fap/README.md b/fap/README.md index f41e879..f07a968 100644 --- a/fap/README.md +++ b/fap/README.md @@ -9,24 +9,27 @@ Licensed under the GNU GPL, version 2. See the included COPYING file. ## Usage -Launch the python scripts for fap from tgmanage directory. +### Installation/configuration +* Install apache2, postgresql, php, php-cli, python3, python-psycopg2 +* Configure Postgresql with user + db name "fap", and password to your liking +* Create DB tables from database/create_tables.sql +* Configure Apache, necessary config in fap/httpd/apache_base_config +* Enable mod_rewrite in Apache - "a2enmod rewrite" +* Start/restart Apache -### HTTPD - j@lappie:~/git/tgmanage$ sudo python3 fap/httpd/server_http.py - -Example: <a href="httpd/terminal.log">httpd/terminal.log</a> - - -### DHCPD - j@lappie:~/git/tgmanage$ sudo python3 fap/dhcpd/server_dhcp.py - -Example: <a href="dhcpd/terminal.log">dhcpd/terminal.log</a> +### FAP workflow +* planning.cpp generates switches.txt and patchlist.txt +* "php -f fap/tools/create_queries/create_queries.php" generates SQL queries +* Manually insert queries from create_queries.php into DB +* "php -f fap/tools/generate_distro_config_ae_event-options/generate.php" generates distroconfig into generated_configs/<distro> +* Copy distro config from generated_configs/* to HTTPD (fap/httpd/files/), and load them from the distros (load merge <ip>/files/<distro>.conf in configure mode) +* Start FAP DHCPD (sudo python3 fap/dhcpd/server_dhcp.py) # TODO * DONE: Support for IPv6 management -* Process multiple HTTP request simultaneously +* DONE: Process multiple HTTP request simultaneously * Support for only pushing JunOS image to switch - no config (for backup switches) * Try/catch on whole ethernet frame in DHCPD * Timestamps on each line in log both from DHCPD and HTTPD diff --git a/fap/database/create_tables.sql b/fap/database/create_tables.sql new file mode 100644 index 0000000..b4aab3f --- /dev/null +++ b/fap/database/create_tables.sql @@ -0,0 +1,17 @@ +CREATE TABLE switches ( + id serial, + hostname character varying(20), + distro_name character varying(100), + distro_phy_port character varying(100), + mgmt_v4_addr character varying(15), + mgmt_v4_cidr smallint, + mgmt_v4_gw character varying(15), + mgmt_v6_cidr smallint, + mgmt_v6_addr character varying(35), + mgmt_v6_gw character varying(35), + mgmt_vlan smallint, + last_config_fetch integer, + current_mac character varying(17), + model character varying(20), + traffic_vlan integer +); diff --git a/fap/httpd/ex2200.template b/fap/httpd/OLD - DEPRECATED/# ex2200.template index 3adcdf9..3adcdf9 100644 --- a/fap/httpd/ex2200.template +++ b/fap/httpd/OLD - DEPRECATED/# ex2200.template diff --git a/fap/httpd/server_http.py b/fap/httpd/OLD - DEPRECATED/# server_http.py index a9ae74c..a9ae74c 100644 --- a/fap/httpd/server_http.py +++ b/fap/httpd/OLD - DEPRECATED/# server_http.py diff --git a/fap/httpd/terminal.log b/fap/httpd/OLD - DEPRECATED/# terminal.log index bedb829..bedb829 100644 --- a/fap/httpd/terminal.log +++ b/fap/httpd/OLD - DEPRECATED/# terminal.log diff --git a/fap/httpd/apache_base_config b/fap/httpd/apache_base_config new file mode 100644 index 0000000..dc17635 --- /dev/null +++ b/fap/httpd/apache_base_config @@ -0,0 +1,15 @@ +root@copernicus:/home/lol# cat /etc/apache2/sites-enabled/000-default.conf | grep -v "#" +<VirtualHost *:80> + ServerAdmin webmaster@localhost + DocumentRoot /srv/fap/httpd/httpd_root/ + ErrorLog ${APACHE_LOG_DIR}/error.log + CustomLog ${APACHE_LOG_DIR}/access.log combined +</VirtualHost> + +root@copernicus:/home/lol# cat /etc/apache2/apache2.conf +[...] +<Directory /srv/fap/httpd/httpd_root/> + Options Indexes FollowSymLinks + AllowOverride All + Require all granted +</Directory> diff --git a/fap/tools/create_queries/create_queries.php b/fap/tools/create_queries/create_queries.php new file mode 100644 index 0000000..d579370 --- /dev/null +++ b/fap/tools/create_queries/create_queries.php @@ -0,0 +1,62 @@ +<?php + /* + Used for generating SQL queries for FAP + Jonas Lindstad for TG15 + GPL V2 + */ + + chdir(__DIR__); # sets the executing directory to be the path of this script - necessary for CLI require() usage + header("Content-Type: text/plain"); + + /* + To get IPv4/IPv6 calculation functions + */ + require '../tools_includes/ipcalc_functions.php'; + + /* + Fetch data sources + + $patchlist = file('../../../patchlist.txt'); + $switches = file('../../../switches.txt'); + */ + $patchlist = file('../../../patchlist_extras.txt'); + $switches = file('../../../switches_extras.txt'); + + $dataset = array(); + + foreach($patchlist as $lines){ + $pieces = explode(' ', trim($lines)); + $dataset[$pieces[0]] = array( + 'hostname' => $pieces[0], + 'distro_name' => 'rs1.' . $pieces[1], # prefix with "rs." so we get "rs1.distro0" syntax + 'distro_phy_port' => $pieces[2] + ); + } + + /* + Assign to logical and usable names in array + */ + foreach($switches as $lines){ + $pieces = explode(' ', trim($lines)); + $dataset[$pieces[0]]['mgmt_v4_addr'] = explode('/', $pieces[3])[0]; + $dataset[$pieces[0]]['mgmt_v4_cidr'] = explode('/', $pieces[3])[1]; + $dataset[$pieces[0]]['mgmt_v4_gw'] = net_to_gw($pieces[3]); + $dataset[$pieces[0]]['mgmt_v6_addr'] = explode('/', $pieces[4])[0]; + $dataset[$pieces[0]]['mgmt_v6_cidr'] = explode('/', $pieces[4])[1]; + $dataset[$pieces[0]]['mgmt_v6_gw'] = net_to_gw($pieces[4]); + $dataset[$pieces[0]]['traffic_vlan'] = $pieces[5]; + $dataset[$pieces[0]]['mgmt_vlan'] = '666'; + } + + /* + Prints the query rows + */ + $sql_query = ''; + foreach($dataset as $k => $v){ + $columns = implode(', ', array_keys($v)); + $values = "'" . implode("', '", array_values($v)) . "'"; + $sql_query .= 'INSERT INTO switches (' . $columns . ') VALUES (' . $values . ');' . "\n"; + } + + echo $sql_query; +?> diff --git a/fap/tools/tools_includes/ipcalc_functions.php b/fap/tools/tools_includes/ipcalc_functions.php new file mode 100644 index 0000000..e848ef1 --- /dev/null +++ b/fap/tools/tools_includes/ipcalc_functions.php @@ -0,0 +1,134 @@ +<?php + /* + Mainly copied from djamps's work - https://github.com/djamps/php-ipv6-calculator/blob/master/ipcalc.php + Edited by Jonas Lindstad for The Gathering 2015 + Licensed under GPL + */ + + + + // Convert array of short unsigned integers to binary + function _packBytes($array) { + foreach ( $array as $byte ) { + $chars .= pack('C',$byte); + } + return $chars; + } + + + // Convert binary to array of short integers + function _unpackBytes($string) { + return unpack('C*',$string); + } + + + // Add array of short unsigned integers + function _addBytes($array1,$array2) { + $result = array(); + $carry = 0; + foreach ( array_reverse($array1,true) as $value1 ) { + $value2 = array_pop($array2); + if ( empty($result) ) { $value2++; } + $newValue = $value1 + $value2 + $carry; + if ( $newValue > 255 ) { + $newValue = $newValue - 256; + $carry = 1; + } else { + $carry = 0; + } + array_unshift($result,$newValue); + } + return $result; + } + + + /* Useful Functions */ + function _cdr2Bin ($cdrin,$len=4){ + if ( $len > 4 || $cdrin > 32 ) { // Are we ipv6? + return str_pad(str_pad("", $cdrin, "1"), 128, "0"); + } else { + return str_pad(str_pad("", $cdrin, "1"), 32, "0"); + } + } + + + function _bin2Cdr ($binin){ + return strlen(rtrim($binin,"0")); + } + + + function _cdr2Char ($cdrin,$len=4){ + $hex = _bin2Hex(_cdr2Bin($cdrin,$len)); + return _hex2Char($hex); + } + + + function _char2Cdr ($char){ + $bin = _hex2Bin(_char2Hex($char)); + return _bin2Cdr($bin); + } + + + function _hex2Char($hex){ + return pack('H*',$hex); + } + + + function _char2Hex($char){ + $hex = unpack('H*',$char); + return array_pop($hex); + } + + + function _hex2Bin($hex){ + $bin=''; + for($i=0;$i<strlen($hex);$i++) + $bin.=str_pad(decbin(hexdec($hex{$i})),4,'0',STR_PAD_LEFT); + return $bin; + } + + + function _bin2Hex($bin){ + $hex=''; + for($i=strlen($bin)-4;$i>=0;$i-=4) + $hex.=dechex(bindec(substr($bin,$i,4))); + return strrev($hex); + } + + /* + Converts a v4/v6 subnet to the first usable IP + */ + function net_to_gw($net){ + $maxSubNets = '2048'; // Stop memory leak from invalid input or large ranges + $charHost = inet_pton(strtok($net, '/')); + $charMask = _cdr2Char(strtok('/'),strlen($charHost)); + $charHostMask = substr(_cdr2Char(127),-strlen($charHost)); + $charNet = $charHost & $charMask; // Supernet network address + $charHostMin = $charNet | ~$charHostMask; + return inet_ntop($charHostMin); + } + + + + + + + + +/* + + $maxSubNets = '2048'; // Stop memory leak from invalid input or large ranges + $superNet = '2a02:ed02:180a::13/64'; + if (ereg('/',$superNet)){ //if cidr type mask + $charHost = inet_pton(strtok($superNet, '/')); + $charMask = _cdr2Char(strtok('/'),strlen($charHost)); + } + + $charHostMask = substr(_cdr2Char(127),-strlen($charHost)); + $charNet = $charHost & $charMask; // Supernet network address + $charHostMin = $charNet | ~$charHostMask; + echo 'Første brukbare adresse i ' . $superNet . ': '; + echo inet_ntop($charHostMin); +*/ + +?> |