public static function downloadConfig($DEVICE) { $TIME = time(); $DESTINATION = "/config/{$DEVICE['id']}.{$TIME}.config"; $TFTPFILE = "/tftpboot/config/{$DEVICE['id']}.{$TIME}.config"; $DEVICE['name'] = preg_replace("/\\//", '-', $DEVICE['name']); // Replace slashes in device names with hyphens! $CONFIGREPO = BASEDIR . "/config/{$DEVICE['name']}"; // TRY SNMP TFTP config grab FIRST because its FAST(er) than CLI! $Config = new CiscoConfig($DEVICE['ip'], SNMP_RW); $Config->WriteNetwork($DESTINATION, 'tftp', TFTPIP); if ($Config->Error) { // If we hit an error, fall back to CLI! special_output(' SNMP error, trying Telnet/SSH:'); $INFOBJECT = Information::retrieve($DEVICE['id']); $INFOBJECT->scan(); $SHOW_RUN = $INFOBJECT->data['run']; $DELIMITER = $INFOBJECT->data['pattern']; unset($INFOBJECT); // Save some memory } else { $SHOW_RUN = file_get_contents($TFTPFILE); } $RUNLEN = strlen($SHOW_RUN); if ($RUNLEN < 800) { // If we cant get the config at all, or length is less than 800 bytes, fuckit and die. special_output(" config is {$RUNLEN} bytes, Could not get config!\n"); return 0; } else { special_output(" Got {$RUNLEN} bytes!"); } $LINES_IN = preg_split('/\\r\\n|\\r|\\n/', $SHOW_RUN); $LINES_OUT = []; foreach ($LINES_IN as $LINE) { // Ignore a bunch of unimportant often-changing lines that clutter up the config repository if (preg_match('/.*show run.*/', $LINE, $REG) || preg_match('/.*show startup.*/', $LINE, $REG) || preg_match('/^Building configur.*/', $LINE, $REG) || preg_match('/^ntp clock-period.*/', $LINE, $REG) || preg_match('/^Current configuration.*/', $LINE, $REG) || preg_match('/.*NVRAM config last up.*/', $LINE, $REG) || preg_match('/.*uncompressed size*/', $LINE, $REG) || preg_match('/^!Time.*/', $LINE, $REG)) { continue; } // If we have UTC and its NOT the configuration last changed line, ignore it. if (preg_match('/.* UTC$/', $LINE, $REG) && !preg_match('/^.*onfig.*/', $LINE, $REG)) { continue; } // If we have CST and its NOT the configuration last changed line, ignore it. if (preg_match('/.* CST$/', $LINE, $REG) && !preg_match('/^.*onfig.*/', $LINE, $REG)) { continue; } // If we have CDT and its NOT the configuration last changed line, ignore it. if (preg_match('/. *CDT$/', $LINE, $REG) && !preg_match('/^.*onfig.*/', $LINE, $REG)) { continue; } // If we find a control code like ^C replace it with ascii ^C $LINE = str_replace(chr(3), '^C', $LINE); // If we find ": end", break out of this function, end of command output detected if (preg_match('/^: end$/', $LINE, $REG)) { break; } // If we find the prompt, break out of this function, end of command output detected if (isset($DELIMITER) && preg_match($DELIMITER, $LINE, $REG)) { break; } $LINE = rtrim($LINE); // Trim whitespace off the right end! array_push($LINES_OUT, $LINE); } // REMOVE blank lines from the leading part of the array and REINDEX the array while ($LINES_OUT[0] == '' && count($LINES_OUT) > 2) { array_shift($LINES_OUT); } // REMOVE blank lines from the end of the array and REINDEX the array while (end($LINES_OUT) == '' && count($LINES_OUT) > 2) { array_pop($LINES_OUT); } // Ensure there is one blank line at EOF. Subversion bitches about this for some reason. array_push($LINES_OUT, ''); $SHOW_RUN = implode("\n", $LINES_OUT); $FH = fopen($CONFIGREPO, 'w'); fwrite($FH, $SHOW_RUN); fclose($FH); return 1; }