123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- <?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.
- include_once 'db.inc.php';
- include_once 'swreg/swreg.inc.php';
- include_once 'swreg_creds.inc.php';
- require_once 'Log.php';
- require_once 'functions.php';
- $dsn = "$db_driver:host=$db_host;dbname=$db_name";
- $options = [
- PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
- PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
- PDO::ATTR_EMULATE_PREPARES => false,
- ];
- if (EXPORT_PRIME === false) {
- exit(0);
- }
- try {
- $dbh = new PDO($dsn, $db_user, $db_pass, $options);
- } catch (PDOException $e) {
- die($e->getMessage());
- }
- $logger = Log::singleton('file', LOGFILE, TOOL_NAME . ' : Exporter');
- if ($logger === false) {
- die("Failed to open logfile.\n");
- }
- $mask = Log::MAX(LOG_LEVEL);
- $logger->setMask($mask);
- $base = get_base($_SERVER['SCRIPT_NAME']);
- $sql = 'SELECT * FROM DEVICE_MAP WHERE checked_out=1';
- $res = null;
- try {
- $res = $dbh->query($sql);
- } catch (PDOException $e) {
- echo "<p><font color=\"red\">Error querying for physical switches: {$e->getMessage()}</font></p>\r\n";
- exit(1);
- }
- $p['authentication']['user'] = PRIME_API_USER;
- $p['authentication']['password'] = PRIME_API_PASS;
- $url = PRIME_URL . '/webacs/api/v1/op/devices/bulkImport.json';
- $d['devicesImport']['devices']['device'] = array();
- $dns_devices = array();
- //$csv_file = array("ip_address,snmp_version,snmp_community,snmp_retries,snmp_timeout,protocol,cli_username,cli_password,cli_enable_password,cli_timeout,snmpv3_user_name,snmpv3_auth_type,snmpv3_auth_password,snmpv3_privacy_type,snmpv3_privacy_password,http_server,http_port,http_config_username,http_config_password,http_monitor_username,http_monitor_password,udf:Serial Number");
- while ($row = $res->fetch()) {
- $sql = "SELECT ip_address FROM SWITCHES WHERE name = '{$row['assigned_switch']}'";
- $sw_res = null;
- try {
- $sw_res = $dbh->query($sql);
- } catch (PDOException $e) {
- echo "<p><font color=\"red\">Error querying for IP address for {$row['assigned_switch']}: {$e->getMessage()}</font></p>\r\n";
- continue;
- }
- $sw_row = $sw_res->fetch();
- $device['ipAddress'] = $sw_row['ip_address'];
- $device['credentialProfileName'] = PRIME_CRED_PROFILE;
- $device['udfs']['udf'] = array();
- $dns_device['ip'] = $sw_row['ip_address'];
- $dns_device['name'] = $row['assigned_switch'];
- $sn['name'] = 'Serial Number';
- $sn['value'] = $row['serial_number'];
- array_push($device['udfs']['udf'], $sn);
- array_push($d['devicesImport']['devices']['device'], $device);
- array_push($dns_devices, $dns_device);
- unset($device);
- }
- $json = json_encode($d);
- $p['put'] = $json;
- $p['header']['Content-Type'] = 'application/json';
- $res = get_PI_REST($url, $p);
- $errors = array();
- $dns_pass = DNS_PASS;
- $dns_user = DNS_USER;
- $dns_server = DNS_SERVER;
- $dns_zone = DNS_ZONE;
- $ztp_user = ZTP_USER;
- if (!$res) {
- echo "<font color=\"red\">ERROR: Failed to export to PI</font>\n";
- } else {
- foreach ($dns_devices as $device) {
- $out = `/usr/local/bin/sudo -u $ztp_user /usr/bin/ssh -2 root@$dns_server /opt/nwreg2/local/usrbin/nrcmd -N $dns_user -P $dns_pass zone $dns_zone showHost {$device['name']}`;
- if (preg_match('/302 Not Found/', $out)) {
- $out = `/usr/local/bin/sudo -u $ztp_user /usr/bin/ssh -2 root@$dns_server /opt/nwreg2/local/usrbin/nrcmd -N $dns_user -P $dns_pass zone $dns_zone addHost {$device['name']} {$device['ip']}`;
- if (!preg_match('/100 Ok/', $out)) {
- array_push($errors, "Error adding entry for {$device['name']}: '$out'");
- }
- } else {
- preg_match('/([^:]+): addr=([^;]+)/', $out, $matches);
- $h = $matches[1];
- $a = $matches[2];
- if ($a != $device['ip']) {
- $out = `/usr/local/bin/sudo -u $ztp_user /usr/bin/ssh -2 root@$dns_server /opt/nwreg2/local/usrbin/nrcmd -N $dns_user -P $dns_pass zone $dns_zone removeHost {$device['name']}`;
- if (!preg_match('/100 Ok/', $out)) {
- array_push($errors, "Failed to remove host {$device['name']}: '$out'");
- continue;
- }
- $out = `/usr/local/bin/sudo -u $ztp_user /usr/bin/ssh -2 root@$dns_server /opt/nwreg2/local/usrbin/nrcmd -N $dns_user -P $dns_pass zone $dns_zone addHost {$device['name']} {$device['ip']}`;
- if (!preg_match('/100 Ok/', $out)) {
- array_push($errors, "Error adding entry for {$device['name']}: '$out'");
- }
- }
- }
- }
- cleanup();
- if (count($errors) > 0) {
- echo "<font color=\"red\">ERROR: Failed to add entries to DNS:<br/></font>\n";
- echo implode('<br/>', $errors);
- } else {
- ?>
- <script language="Javascript">
- window.close();
- </script>
- <?php
- }
- }
- ?>
|