export.php 5.5 KB

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