export.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. //-
  3. // Copyright (c) 2011-2016 Joe Clarke <jclarke@cisco.com>
  4. // All rights reserved.
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions
  7. // are met:
  8. // 1. Redistributions of source code must retain the above copyright
  9. // notice, this list of conditions and the following disclaimer.
  10. // 2. Redistributions in binary form must reproduce the above copyright
  11. // notice, this list of conditions and the following disclaimer in the
  12. // documentation and/or other materials provided with the distribution.
  13. // THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  14. // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  16. // ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  17. // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18. // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  19. // OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  20. // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  21. // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  22. // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  23. // SUCH DAMAGE.
  24. include_once 'db.inc.php';
  25. include_once 'swreg/swreg.inc.php';
  26. include_once 'swreg_creds.inc.php';
  27. require_once 'Log.php';
  28. require_once 'functions.php';
  29. $dsn = "$db_driver:host=$db_host;dbname=$db_name";
  30. $options = [
  31. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  32. PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
  33. PDO::ATTR_EMULATE_PREPARES => false,
  34. ];
  35. if (EXPORT_PRIME === false) {
  36. exit(0);
  37. }
  38. try {
  39. $dbh = new PDO($dsn, $db_user, $db_pass, $options);
  40. } catch (PDOException $e) {
  41. die($e->getMessage());
  42. }
  43. $logger = Log::singleton('file', LOGFILE, TOOL_NAME . ' : Exporter');
  44. if ($logger === false) {
  45. die("Failed to open logfile.\n");
  46. }
  47. $mask = Log::MAX(LOG_LEVEL);
  48. $logger->setMask($mask);
  49. $base = get_base($_SERVER['SCRIPT_NAME']);
  50. $sql = 'SELECT * FROM DEVICE_MAP WHERE checked_out=1';
  51. $res = null;
  52. try {
  53. $res = $dbh->query($sql);
  54. } catch (PDOException $e) {
  55. echo "<p><font color=\"red\">Error querying for physical switches: {$e->getMessage()}</font></p>\r\n";
  56. exit(1);
  57. }
  58. $p['authentication']['user'] = PRIME_API_USER;
  59. $p['authentication']['password'] = PRIME_API_PASS;
  60. $url = PRIME_URL . '/webacs/api/v1/op/devices/bulkImport.json';
  61. $d['devicesImport']['devices']['device'] = array();
  62. $dns_devices = array();
  63. //$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");
  64. while ($row = $res->fetch()) {
  65. $sql = "SELECT ip_address FROM SWITCHES WHERE name = '{$row['assigned_switch']}'";
  66. $sw_res = null;
  67. try {
  68. $sw_res = $dbh->query($sql);
  69. } catch (PDOException $e) {
  70. echo "<p><font color=\"red\">Error querying for IP address for {$row['assigned_switch']}: {$e->getMessage()}</font></p>\r\n";
  71. continue;
  72. }
  73. $sw_row = $sw_res->fetch();
  74. $device['ipAddress'] = $sw_row['ip_address'];
  75. $device['credentialProfileName'] = PRIME_CRED_PROFILE;
  76. $device['udfs']['udf'] = array();
  77. $dns_device['ip'] = $sw_row['ip_address'];
  78. $dns_device['name'] = $row['assigned_switch'];
  79. $sn['name'] = 'Serial Number';
  80. $sn['value'] = $row['serial_number'];
  81. array_push($device['udfs']['udf'], $sn);
  82. array_push($d['devicesImport']['devices']['device'], $device);
  83. array_push($dns_devices, $dns_device);
  84. unset($device);
  85. }
  86. $json = json_encode($d);
  87. $p['put'] = $json;
  88. $p['header']['Content-Type'] = 'application/json';
  89. $res = get_PI_REST($url, $p);
  90. $errors = array();
  91. $dns_pass = DNS_PASS;
  92. $dns_user = DNS_USER;
  93. $dns_server = DNS_SERVER;
  94. $dns_zone = DNS_ZONE;
  95. $ztp_user = ZTP_USER;
  96. if (!$res) {
  97. echo "<font color=\"red\">ERROR: Failed to export to PI</font>\n";
  98. } else {
  99. foreach ($dns_devices as $device) {
  100. $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']}`;
  101. if (preg_match('/302 Not Found/', $out)) {
  102. $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']}`;
  103. if (!preg_match('/100 Ok/', $out)) {
  104. array_push($errors, "Error adding entry for {$device['name']}: '$out'");
  105. }
  106. } else {
  107. preg_match('/([^:]+): addr=([^;]+)/', $out, $matches);
  108. $h = $matches[1];
  109. $a = $matches[2];
  110. if ($a != $device['ip']) {
  111. $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']}`;
  112. if (!preg_match('/100 Ok/', $out)) {
  113. array_push($errors, "Failed to remove host {$device['name']}: '$out'");
  114. continue;
  115. }
  116. $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']}`;
  117. if (!preg_match('/100 Ok/', $out)) {
  118. array_push($errors, "Error adding entry for {$device['name']}: '$out'");
  119. }
  120. }
  121. }
  122. }
  123. cleanup();
  124. if (count($errors) > 0) {
  125. echo "<font color=\"red\">ERROR: Failed to add entries to DNS:<br/></font>\n";
  126. echo implode('<br/>', $errors);
  127. } else {
  128. ?>
  129. <script language="Javascript">
  130. window.close();
  131. </script>
  132. <?php
  133. }
  134. }
  135. ?>