123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511 |
- <?php
- //-
- // Copyright (c) 2011-2016 Joe Clarke <jclarke@cisco.com>
- // All rights reserved.
- // Redistribution and use in source and binary forms, with or without
- // modification, are permitted provided that the following conditions
- // are met:
- // 1. Redistributions of source code must retain the above copyright
- // notice, this list of conditions and the following disclaimer.
- // 2. Redistributions in binary form must reproduce the above copyright
- // notice, this list of conditions and the following disclaimer in the
- // documentation and/or other materials provided with the distribution.
- // THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
- // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- // ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
- // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- // OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- // SUCH DAMAGE.
- require_once 'Log.php';
- function cleanup()
- {
- global $logger;
- $logger->close();
- }
- function print_header($title)
- {
- ?>
- <!DOCTYPE html>
- <html>
- <head>
- <title>
- <?= $title ?>
- </title>
- <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
- <link rel="stylesheet" href="<?= get_base($_SERVER['SCRIPT_NAME']) ?>/mini.css" type="text/css">
- <link rel="stylesheet" type="text/css" href="<?= get_base($_SERVER['SCRIPT_NAME']) ?>/jquery.dataTables.min.css">
- <script language="javascript" src="<?= get_base($_SERVER['SCRIPT_NAME']) ?>/jquery-1.12.3.js"></script>
- <script language="javascript" src="<?= get_base($_SERVER['SCRIPT_NAME']) ?>/jquery.dataTables.min.js"></script>
- <script language="javascript" src="<?= get_base($_SERVER['SCRIPT_NAME']) ?>/jquery.color-animation.js"></script>
- <script language="JavaScript" src="<?= get_base($_SERVER['SCRIPT_NAME']) ?>/functions.js"></script>
- <style type="text/css">
- .style1 {
- font-family: "Trebuchet MS"
- }
- </style>
- <script language="javascript">
- <!--
- function MM_reloadPage(init) {
- if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
- document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
- else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
- }
- MM_reloadPage(true);
- //-->
- </script>
- </head>
- <?php
- }
- function get_row_color($row)
- {
- return ($row % 2 != 0) ? 'bgcolor="#f0f0f0"' : '';
- }
- function get_base($script)
- {
- $base = dirname($script);
- if ($base == '/') {
- $base = '';
- }
- return $base;
- }
- function generate_port_profile($start_port, $end_port, $vlan, $pid)
- {
- global $VLANS, $PORT_TYPES;
- $zpid = $pid;
- /*if (isset($PID_MAP[$pid])) {
- $zpid = $PID_MAP[$pid];
- }*/
- $pprefix = '';
- if (isset($PORT_TYPES[$zpid])) {
- $pprefix = $PORT_TYPES[$zpid];
- }
- $profile = "interface range {$pprefix}$start_port - $end_port\n";
- if (file_exists(PORT_TMPL_DIR . '/ports.tmpl')) {
- $profile .= file_get_contents(PORT_TMPL_DIR . '/ports.tmpl');
- }
- if (file_exists(PORT_TMPL_DIR . '/' . $zpid . '/ports.tmpl')) {
- $profile .= file_get_contents(PORT_TMPL_DIR . '/' . $zpid . '/ports.tmpl');
- }
- if ($vlan == 'DYNAMIC') {
- if (file_exists(PORT_TMPL_DIR . '/DYNAMIC/ports.tmpl')) {
- $profile .= file_get_contents(PORT_TMPL_DIR . '/DYNAMIC/ports.tmpl');
- }
- if (file_exists(PORT_TMPL_DIR . '/DYNAMIC/' . $zpid . '/ports.tmpl')) {
- $profile .= file_get_contents(PORT_TMPL_DIR . '/DYNAMIC/' . $zpid . '/ports.tmpl');
- }
- } elseif ($vlan == 'TRUNK' || $vlan == 'UPLINK TRUNK') {
- if ($vlan == 'UPLINK TRUNK') {
- $profile .= " description STATIC: Uplink Trunk Port\n";
- } else {
- $profile .= " description STATIC: Trunk Port\n";
- }
- if (file_exists(PORT_TMPL_DIR . '/TRUNK/ports.tmpl')) {
- $profile .= file_get_contents(PORT_TMPL_DIR . '/TRUNK/ports.tmpl');
- }
- if (file_exists(PORT_TMPL_DIR . '/TRUNK/' . $zpid . '/ports.tmpl')) {
- $profile .= file_get_contents(PORT_TMPL_DIR . '/TRUNK/' . $zpid . '/ports.tmpl');
- }
- if ($vlan == 'UPLINK TRUNK') {
- if (file_exists(PORT_TMPL_DIR . '/TRUNK/uplink/ports.tmpl')) {
- $profile .= file_get_contents(PORT_TMPL_DIR . '/TRUNK/uplink/ports.tmpl');
- }
- if (file_exists(PORT_TMPL_DIR . '/TRUNK/uplink/' . $zpid . '/ports.tmpl')) {
- $profile .= file_get_contents(PORT_TMPL_DIR . '/TRUNK/uplink/' . $zpid . '/ports.tmpl');
- }
- }
- } else {
- $vi = $VLANS[$vlan];
- $profile .= " description STATIC: $vlan Access Port\n";
- $profile .= " switchport access vlan $vi\n";
- if (file_exists(PORT_TMPL_DIR . '/ACCESS/ports.tmpl')) {
- $profile .= file_get_contents(PORT_TMPL_DIR . '/ACCESS/ports.tmpl');
- }
- if (file_exists(PORT_TMPL_DIR . '/ACCESS/' . $zpid . '/ports.tmpl')) {
- $profile .= file_get_contents(PORT_TMPL_DIR . '/ACCESS/' . $zpid . '/ports.tmpl');
- }
- if (file_exists(PORT_TMPL_DIR . '/ACCESS/' . $vi . '/ports.tmpl')) {
- $profile .= file_get_contents(PORT_TMPL_DIR . '/ACCESS/' . $vi . '/ports.tmpl');
- }
- if (file_exists(PORT_TMPL_DIR . '/ACCESS/' . $vi . '/' . $zpid . '/ports.tmpl')) {
- $profile .= file_get_contents(PORT_TMPL_DIR . '/ACCESS/' . $vi . '/' . $zpid . '/ports.tmpl');
- }
- }
- return $profile;
- }
- function call_hook($hook, $args)
- {
- global $HOOKS;
- $hook_parts = explode(':', $hook);
- $hook_name = '$HOOKS';
- foreach ($hook_parts as $hp) {
- $hook_name .= "['$hp']";
- }
- @eval('$hook_val = ' . $hook_name . ';');
- if (!isset($hook_val) || $hook_val == '') {
- return;
- }
- $argstr = $hook . ' ';
- foreach ($args as $arg) {
- $argstr .= escapeshellarg($arg) . ' ';
- }
- $argstr = trim($argstr);
- @exec("$hook_val $argstr > /dev/null 2>&1 &");
- }
- function get_PI_REST($url, $p = false, $decode = true, $timeout = 300)
- {
- $cookie = false;
- $error = '';
- if (DEBUG >= 5) {
- echo "<h3>calling $url with params:</h3>";
- var_dump($p);
- }
- $i = 0;
- $res = false;
- while ($i < REST_RETRIES) {
- $res = fetch_url($url, $cookie, $error, $p, $timeout);
- if ($res) {
- break;
- }
- sleep(3);
- ++$i;
- }
- if ($res) {
- if ($decode) {
- $json = json_decode($res, true);
- return $json;
- } else {
- return $res;
- }
- }
- return false;
- }
- function apicGetTicket($host, $user, $pass, &$error)
- {
- $cookie = false;
- $url = 'https://' . $host . '/api/v1/ticket';
- $p = array();
- $p['post'] = json_encode(array('username' => $user, 'password' => $pass));
- $p['header']['Content-Type'] = 'application/json';
- $i = 0;
- $res = false;
- while ($i < REST_RETRIES) {
- $res = fetch_url($url, $cookie, $error, $p);
- if ($res) {
- break;
- }
- sleep(REST_RETRY_INTERVAL);
- ++$i;
- }
- if ($res) {
- $json = json_decode($res, true);
- return $json['response']['serviceTicket'];
- }
- return false;
- }
- function apicAddDevice($host, $ticket, $projid, $device, $sn, $pid, &$msg, $config = null, $image = null)
- {
- $cookie = false;
- $error = '';
- $url = 'https://' . $host . '/api/v1/pnp-project/' . $projid . '/device';
- $p = array();
- $params = array();
- $params['hostName'] = $device;
- $params['serialNumber'] = $sn;
- $params['platformId'] = $pid;
- if ($config) {
- $params['configId'] = $config;
- }
- if ($image) {
- $params['imageId'] = $image;
- }
- $p['post'] = json_encode($params);
- $p['header']['Content-Type'] = 'application/json';
- $p['header']['X-Auth-Token'] = $ticket;
- $i = 0;
- $res = false;
- while ($i < REST_RETRIES) {
- $res = fetch_url($url, $cookie, $error, $p);
- if ($res) {
- break;
- }
- sleep(REST_RETRY_INTERVAL);
- ++$i;
- }
- if ($res) {
- $json = json_decode($res, true);
- return apicGetTaskResult($host, $ticket, $json['response']['taskId'], $msg);
- }
- $msg = $error;
- return false;
- }
- function apicGetTaskResult($host, $ticket, $task, &$msg, $timeout = 60)
- {
- $cookie = false;
- $error = '';
- $url = 'https://' . $host . '/api/v1/task/' . $task;
- $p = array();
- $p['header']['X-Auth-Token'] = $ticket;
- $done = false;
- $j = 0;
- while ($j < $timeout) {
- $i = 0;
- $res = false;
- while ($i < REST_RETRIES) {
- $res = fetch_url($url, $cookie, $error, $p);
- if ($res) {
- break;
- }
- sleep(REST_RETRY_INTERVAL);
- ++$i;
- }
- if ($res) {
- $json = json_decode($res, true);
- if (isset($json['response']['isError']) && $json['response']['isError'] === true) {
- $msg = $json['response']['failureReason'];
- return false;
- }
- if (isset($json['response']['endTime']) && $json['response']['endTime'] >= $json['response']['startTime']) {
- return true;
- }
- // This allows the timeout to be specified in seconds.
- sleep(1);
- ++$j;
- }
- }
- if ($error != '') {
- $msg = $error;
- } else {
- $msg = 'Failed to get result from APIC-EM';
- }
- return false;
- }
- function apicGetProject($host, $ticket, $proj, &$error)
- {
- $cookie = false;
- $url = 'https://' . $host . '/api/v1/pnp-project/count';
- $p = array();
- $p['header']['X-Auth-Token'] = $ticket;
- $i = 0;
- $res = false;
- while ($i < REST_RETRIES) {
- $res = fetch_url($url, $cookie, $error, $p);
- if ($res) {
- break;
- }
- sleep(REST_RETRY_INTERVAL);
- ++$i;
- }
- if ($res) {
- $json = json_decode($res, true);
- $count = $json['response'];
- $url = 'https://' . $host . '/api/v1/pnp-project';
- $offset = 1;
- $limit = 500;
- do {
- $p['get']['offset'] = $offset;
- $p['get']['limit'] = $limit;
- $i = 0;
- $res = false;
- while ($i < REST_RETRIES) {
- $res = fetch_url($url, $cookie, $error, $p);
- if ($res) {
- break;
- }
- sleep(REST_RETRY_INTERVAL);
- ++$i;
- }
- if ($res) {
- $json = json_decode($res, true);
- foreach ($json['response'] as $s) {
- if ($s['siteName'] == $proj) {
- return $s;
- }
- }
- }
- $offset += $limit;
- } while ($offset + $limit < $count);
- }
- return false;
- }
- function apicUpdateProject($host, $ticket, $projid, $proj, $ip, &$msg, $path = '/')
- {
- $cookie = false;
- $error = '';
- $url = 'https://' . $host . '/api/v1/pnp-project';
- $p = array();
- $params = array();
- $params['id'] = $projid;
- $params['siteName'] = $proj;
- $params['tftpServer'] = $ip;
- $params['tftpPath'] = $path;
- $p['put'] = json_encode($params);
- $p['header']['Content-Type'] = 'application/json';
- $p['header']['X-Auth-Token'] = $ticket;
- $i = 0;
- $res = false;
- while ($i < REST_RETRIES) {
- $res = fetch_url($url, $cookie, $error, $p);
- if ($res) {
- break;
- }
- sleep(REST_RETRY_INTERVAL);
- ++$i;
- }
- if ($res) {
- $json = json_decode($res, true);
- return apicGetTaskResult($host, $ticket, $json['response']['taskId'], $msg);
- }
- $msg = $error;
- return false;
- }
- function fetch_url($url, &$cookie, &$error, $p = false, $timeout = REST_TIMEOUT)
- {
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
- curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- if (DEBUG > 5) {
- curl_setopt($ch, CURLOPT_HEADER, true);
- curl_setopt($ch, CURLINFO_HEADER_OUT, true);
- curl_setopt($ch, CURLOPT_VERBOSE, true);
- } else {
- curl_setopt($ch, CURLOPT_HEADER, false);
- }
- if (isset($p['get'])) {
- $url .= '?' . http_build_query($p['get']);
- }
- if (isset($p['post'])) {
- curl_setopt($ch, CURLOPT_POST, count($p['post']));
- curl_setopt($ch, CURLOPT_POSTFIELDS, $p['post']);
- }
- if (isset($p['put'])) {
- curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
- $p['header']['Content-Length'] = strlen($p['put']);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $p['put']);
- }
- if (isset($p['header'])) {
- foreach ($p['header'] as $name => $value) {
- $header[] = $name . ': ' . $value;
- }
- curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
- }
- if (isset($p['cookie'])) {
- curl_setopt($ch, CURLOPT_COOKIE, $p['cookie']);
- }
- if (isset($p['authentication'])) {
- curl_setopt($ch, CURLOPT_USERPWD, $p['authentication']['user'] . ':' . $p['authentication']['password']);
- }
- curl_setopt($ch, CURLOPT_URL, $url);
- $response = curl_exec($ch);
- $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
- $error = 'cURL error num: ' . curl_errno($ch) . ' => ' . curl_error($ch);
- if (DEBUG >= 5) {
- echo '<h3>Request:</h3>';
- var_dump(curl_getinfo($ch));
- echo '<h3>Response:</h3>';
- var_dump($response);
- echo curl_error($ch) . "\n";
- }
- if (empty($response)) {
- if (DEBUG > 0) {
- echo "WARNING: empty response from URL<br/>\n";
- curl_close($ch);
- return false;
- }
- } else {
- if (empty($httpcode)) {
- if (DEBUG > 0) {
- echo "WARNING: no HTTP code was returned\n$response\n";
- curl_close($ch);
- return false;
- }
- } elseif ($httpcode < 200 || $httpcode > 299) {
- if (DEBUG > 0) {
- echo "ERROR: the server returned http code: $httpcode\n$response\n";
- }
- if (DEBUG > 1) {
- echo '<h3>Request:</h3>';
- var_dump(curl_getinfo($ch));
- echo '<h3>Response:</h3>';
- var_dump($response);
- }
- curl_close($ch);
- $cookie = false;
- $error = $httpcode;
- return false;
- } else {
- curl_close($ch);
- return $response;
- }
- }
- }
- ?>
|