Only in routerconfigs: devices.sql
diff -crB routerconfigs-orig/functions.php routerconfigs/functions.php
*** routerconfigs-orig/functions.php	2008-09-07 21:09:13.000000000 +0200
--- routerconfigs/functions.php	2009-05-12 17:15:38.000000000 +0200
***************
*** 83,99 ****
  }
  
  function plugin_routerconfigs_check_config ($data) {
! 	$data = explode("\n", $data);
! 	foreach ($data as $d) {
! 		if (trim($d) == 'end')
! 			return TRUE;
! 	}
  	return FALSE;
  }
  
  function plugin_routerconfigs_download_config ($device) {
  	$info = plugin_routerconfigs_retrieve_account($device['id']);
  	$dir = $device['directory'];
  
  	$backuppath = read_config_option("routerconfigs_backup_path");
  	if (!is_dir($backuppath) || strlen($backuppath) < 2) {
--- 83,97 ----
  }
  
  function plugin_routerconfigs_check_config ($data) {
! 	if (preg_match('/\n[^\w]*end[^\w]*$/',$data))
! 		return TRUE;
  	return FALSE;
  }
  
  function plugin_routerconfigs_download_config ($device) {
  	$info = plugin_routerconfigs_retrieve_account($device['id']);
  	$dir = $device['directory'];
+ 	$ip = $device['ipaddress'];
  
  	$backuppath = read_config_option("routerconfigs_backup_path");
  	if (!is_dir($backuppath) || strlen($backuppath) < 2) {
***************
*** 109,189 ****
  
  	$tftpfilename = $device['hostname'];
  	$filename = $tftpfilename;
- 	$telnet = new PHPTelnet();
  
  	$devicetype = db_fetch_row("SELECT * FROM plugin_routerconfigs_devicetypes WHERE id = " . $device['devicetype']);
  
  	if (empty($devicetype)){
  		$devicetype = array('username' => 'sername:',
  					'password' => 'assword:',
! 					'copytftp' => 'copy run tftp',
  					'version' => 'show version',
  					'forceconfirm' => 0,
  				);
  	}
! 	$result = $telnet->Connect($device['ipaddress'], $info['username'], $info['password'], $devicetype);
! 	$debug = $telnet->debug;
  	db_execute('UPDATE plugin_routerconfigs_devices SET lastattempt = ' . time() . ' WHERE id = ' . $device['id']);
  
  	if ($result == 0) {
! 		$telnet->DoCommand($devicetype['copytftp'], $result);
! 		$debug .= $result;
! 		echo $result;
! 		$telnet->DoCommand($tftpserver, $result);
  		$debug .= $result;
! 		echo $result;
! 		$telnet->DoCommand($filename, $result);
! 		$debug .= $result;
! 		echo $result;
  
! 		if (strpos($result, 'confirm') !== FALSE || strpos($result, 'to tftp:') !== FALSE || $devicetype['forceconfirm']) {
! 			$telnet->DoCommand('y', $result);
  			$debug .= $result;
! 			echo $result;
  		}
  
! 		$x = 0;
! 		while ($x < 30) {
! 			$telnet->GetResponse($result);
  			$debug .= $result;
! 			echo $result;
  
! 			if (strpos($result, 'Error'))
! 				break;
  
! 			$data = '';
! 			if (file_exists("$backuppath/$tftpfilename")) {
! 				clearstatcache();
! 				$file = fopen("$backuppath/$tftpfilename", 'r');
! 				if (filesize("$backuppath/$tftpfilename") > 0) {
! 					$data = fread($file, filesize("$backuppath/$tftpfilename"));
! 				}
! 				fclose($file);
! 				if (plugin_routerconfigs_check_config ($data)) {
! 					break;
! 				}
! 			}
! 			sleep(1);	
  			$x++;
  		}
  
! 		if (!file_exists("$backuppath/$tftpfilename")) {
! 			$telnet->error = 7;
! 			plugin_routerconfigs_save_error ($device['id'], $telnet);
  			plugin_routerconfigs_save_debug($device, $debug);
! 			$telnet->Disconnect();
  			return false;
- 		} else {
- 			@unlink("$backuppath/$tftpfilename");
  		}
! 
! 		if (!plugin_routerconfigs_check_config ($data)) {
! 			$telnet->error = 5;
! 			plugin_routerconfigs_save_error ($device['id'], $telnet);
  			plugin_routerconfigs_save_debug($device, $debug);
! 			$telnet->Disconnect();
  			return false;
  		}
  
  		$data = str_replace ("\n", "\r\n", $data);
  		$data2 = explode("\r\n", $data);
--- 107,215 ----
  
  	$tftpfilename = $device['hostname'];
  	$filename = $tftpfilename;
  
  	$devicetype = db_fetch_row("SELECT * FROM plugin_routerconfigs_devicetypes WHERE id = " . $device['devicetype']);
  
  	if (empty($devicetype)){
  		$devicetype = array('username' => 'sername:',
  					'password' => 'assword:',
! 					'copytftp' => 'copy start tftp',
  					'version' => 'show version',
  					'forceconfirm' => 0,
+ 					'checkendinconfig' => 1
  				);
  	}
! 
! 	$connection = new PHPSsh();
! 	if (($result = $connection->Connect($device['ipaddress'], $info['username'], $info['password'], $devicetype))) {
! 		$connection=NULL;
! 		$connection = new PHPTelnet();
! 		$result = $connection->Connect($device['ipaddress'], $info['username'], $info['password'], $devicetype);
! 		plugin_routerconfigs_log($device['ipaddress']."-> DEBUG: telnet");
! 	}
! 	$debug = $connection->debug;
! 
  	db_execute('UPDATE plugin_routerconfigs_devices SET lastattempt = ' . time() . ' WHERE id = ' . $device['id']);
  
  	if ($result == 0) {
! 		$command=$devicetype['copytftp'];
! 		if(stristr($command, "%SERVER%"))
! 			$command=str_replace("%SERVER%",$tftpserver,$command);
! 		if(stristr($command, "%FILE%"))
! 			$command=str_replace("%FILE%",$filename,$command);
! 		plugin_routerconfigs_log($ip."-> DEBUG: command to execute $command");
! 		$connection->DoCommand($command, $result);
  		$debug .= $result;
! 		plugin_routerconfigs_log($ip."-> DEBUG: copy tftp result=$result");
! 		
! 		//check if there is questions to confirm
! 		//i.e: in ASA it ask for confirmation of the source file
! 		//but this also confirm in case that all command is executed
! 		//in one line
! 		while(preg_match("/[\d\w]\]\?[^\w]*$/",$result)){
! 			$connection->DoCommand("", $result); //Just send an enter to confirm a question 
! 			$debug .= $result;
! 			plugin_routerconfigs_log($ip."-> DEBUG: confirm data result=$result");
! 		}
  
! 		//send tftpserver if necessary
! 		if(stristr($debug,"address") && !stristr($debug,"[$ip]"))
! 		{
! 			$connection->DoCommand($tftpserver, $result);
  			$debug .= $result;
! 			$connection->GetResponse($result);
! 			plugin_routerconfigs_log($ip."-> DEBUG: send serverip result=$debug");
  		}
  
! 		//send filename if necessary
! 		if(stristr($debug,"filename") && !stristr($debug, "[$filename]"))
! 		{
! 			$connection->DoCommand($filename, $result);
! 			plugin_routerconfigs_log($ip."-> DEBUG: send filename result=$result");
  			$debug .= $result;
! 		}
  
! 		if (strpos($result, 'confirm') !== FALSE || strpos($result, 'to tftp:') !== FALSE || $devicetype['forceconfirm']) {
! 			$connection->DoCommand('y', $result);
! 			$debug .= $result;
! 			plugin_routerconfigs_log($ip."-> DEBUG: confirm result=$result");
! 		}
  
! 		$x = 0;
! 		while (!stristr($result,"bytes copied") && !stristr($result,"successfully") && !stristr($result, "error") && $x<30) {
! 			$connection->GetResponse($result);
! 			$debug .= $result;
  			$x++;
  		}
  
! 		$data = '';
! 		if (file_exists("$backuppath/$tftpfilename")) {
! 			clearstatcache();
! 			$file = fopen("$backuppath/$tftpfilename", 'r');
! 			if (filesize("$backuppath/$tftpfilename") > 0) {
! 				$data = fread($file, filesize("$backuppath/$tftpfilename"));
! 			}
! 			fclose($file);
! 			@unlink("$backuppath/$tftpfilename");
! 		}else{
! 			$connection->error = 7;
! 			plugin_routerconfigs_save_error ($device['id'], $connection);
  			plugin_routerconfigs_save_debug($device, $debug);
! 			$connection->Disconnect();
  			return false;
  		}
! 		if (!plugin_routerconfigs_check_config ($data) && $devicetype['checkendinconfig']) {
! 			$connection->error = 5;
! 			plugin_routerconfigs_save_error ($device['id'], $connection);
  			plugin_routerconfigs_save_debug($device, $debug);
! 			plugin_routerconfigs_log($device['ipaddress']."-> DEBUG: checking end in config");
! 			$connection->Disconnect();
  			return false;
  		}
+ 		if ($devicetype['checkendinconfig'])
+ 			plugin_routerconfigs_log($device['ipaddress']."-> DEBUG: config checked");
+ 		else
+ 			plugin_routerconfigs_log($device['ipaddress']."-> DEBUG: config not checked");
  
  		$data = str_replace ("\n", "\r\n", $data);
  		$data2 = explode("\r\n", $data);
***************
*** 220,226 ****
  		if ($lastchange != '' && $lastchange != $device['lastchange']) {
  			db_execute("UPDATE plugin_routerconfigs_devices SET lastchange = $lastchange, username = '$lastuser' WHERE id = " . $device['id']);
  		} else if ($lastchange == '' && $devicetype['version'] != '') {
! 			$telnet->DoCommand($devicetype['version'], $version);
  			$t = time();
  			$debug .= $version;
  			$version = explode("\n", $version);
--- 246,254 ----
  		if ($lastchange != '' && $lastchange != $device['lastchange']) {
  			db_execute("UPDATE plugin_routerconfigs_devices SET lastchange = $lastchange, username = '$lastuser' WHERE id = " . $device['id']);
  		} else if ($lastchange == '' && $devicetype['version'] != '') {
! 			$connection->DoCommand("terminal length 0", $version);
! 			$connection->DoCommand("terminal pager 0", $version);
! 			$connection->DoCommand($devicetype['version'], $version);
  			$t = time();
  			$debug .= $version;
  			$version = explode("\n", $version);
***************
*** 281,288 ****
  				}
  			}
  		}
! 		$telnet->Disconnect();
! 
  		if (strlen($data) > 100) {
  			if (!is_dir("$backuppath/$dir")) {
  				mkdir("$backuppath/$dir", 0777, true);
--- 309,317 ----
  				}
  			}
  		}
! 		$connection->Disconnect();
! 		
! 		plugin_routerconfigs_log($ip."-> DEBUG: datalen".strlen($data));
  		if (strlen($data) > 100) {
  			if (!is_dir("$backuppath/$dir")) {
  				mkdir("$backuppath/$dir", 0777, true);
***************
*** 299,321 ****
  				 $lastchange = 0;
  			db_execute('INSERT INTO plugin_routerconfigs_backups (device, btime, directory, filename, config, lastchange, username) VALUES (' . $device['id'] . ",$t,'$dir','$filename-$date', '$data2', $lastchange, '$lastuser')");
  		} else {
! 			plugin_routerconfigs_save_error ($device['id'], $telnet);
  			plugin_routerconfigs_save_debug($device, $debug);
  			return false;
  		}
  	} else {
! 		plugin_routerconfigs_save_error ($device['id'], $telnet);
  		plugin_routerconfigs_save_debug($device, $debug);
  		return false;
  	}
! 	plugin_routerconfigs_save_error ($device['id'], $telnet);
  	plugin_routerconfigs_save_debug($device, $debug);
  	return true;
  }
  
  function plugin_routerconfigs_save_debug($device, $debug) {
  	$debug = base64_encode($debug);
! 	echo "Saving Debug\n";
  	db_execute("UPDATE plugin_routerconfigs_devices SET debug = '$debug' WHERE id = " . $device['id']);
  }
  
--- 328,352 ----
  				 $lastchange = 0;
  			db_execute('INSERT INTO plugin_routerconfigs_backups (device, btime, directory, filename, config, lastchange, username) VALUES (' . $device['id'] . ",$t,'$dir','$filename-$date', '$data2', $lastchange, '$lastuser')");
  		} else {
! 			plugin_routerconfigs_save_error ($device['id'], $connection);
  			plugin_routerconfigs_save_debug($device, $debug);
  			return false;
  		}
  	} else {
! 		plugin_routerconfigs_save_error ($device['id'], $connection);
  		plugin_routerconfigs_save_debug($device, $debug);
+ 		plugin_routerconfigs_log($ip."-> DEBUG: Connection failed");
  		return false;
  	}
! 	plugin_routerconfigs_save_error ($device['id'], $connection);
  	plugin_routerconfigs_save_debug($device, $debug);
+ 	plugin_routerconfigs_log($device['ipaddress']."-> NOTICE: Backed up");
  	return true;
  }
  
  function plugin_routerconfigs_save_debug($device, $debug) {
  	$debug = base64_encode($debug);
! 	//echo "Saving Debug\n";
  	db_execute("UPDATE plugin_routerconfigs_devices SET debug = '$debug' WHERE id = " . $device['id']);
  }
  
***************
*** 352,357 ****
--- 383,633 ----
  }
  
  /*
+ //Log messages to cacti log or syslog 
+ //This function is the same as thold plugin with a litle changes
+ //to respect cacti log level settings
+ */
+ function plugin_routerconfigs_log($string) {
+ 	global $config;
+ 	$environ = 'ROUTERCONFIGS';
+ 	/* fill in the current date for printing in the log */
+ 	$date = date("m/d/Y h:i:s A");
+ 
+ 	/* determine how to log data */
+ 	$logdestination = read_config_option("log_destination");
+ 	$logfile        = read_config_option("path_cactilog");
+ 
+ 	/* format the message */
+ 	$message = "$date - " . $environ . ": " . $string . "\n";
+ 
+ 	if (substr_count($string,"ERROR:") || substr_count($string,"STATS:"))
+ 		$log_level = 2;
+ 	else if (substr_count($string,"WARNING:") || substr_count($string,"NOTICE:"))
+ 		$log_level = 3;
+ 	else if (substr_count($string,"DEBUG:"))
+ 		$log_level = 5;
+ 
+ 	/* Log to Logfile */
+ 	if ((($logdestination == 1) || ($logdestination == 2)) && read_config_option("log_verbosity") >= $log_level) {
+ 
+ 		$log_type = "note";
+ 
+ 		if ($logfile == "") {
+ 			$logfile = $config["base_path"] . "/log/cacti.log";
+ 		}
+ 
+ 		/* echo the data to the log (append) */
+ 		$fp = @fopen($logfile, "a");
+ 
+ 		if ($fp) {
+ 			@fwrite($fp, $message);
+ 			fclose($fp);
+ 		}
+ 	}
+ 
+ 	/* Log to Syslog/Eventlog */
+ 	/* Syslog is currently Unstable in Win32 */
+ 	if (($logdestination == 2) || ($logdestination == 3)) {
+ 		$string = strip_tags($string);
+ 		$log_type = "";
+ 		if (substr_count($string,"ERROR:"))
+ 			$log_type = "err";
+ 		else if (substr_count($string,"WARNING:"))
+ 			$log_type = "warn";
+ 		else if (substr_count($string,"STATS:"))
+ 			$log_type = "stat";
+ 		else if (substr_count($string,"NOTICE:"))
+ 			$log_type = "note";
+ 
+ 		if (strlen($log_type)) {
+ 			define_syslog_variables();
+ 
+ 			if ($config["cacti_server_os"] == "win32")
+ 				openlog("Cacti", LOG_NDELAY | LOG_PID, LOG_USER);
+ 			else
+ 				openlog("Cacti", LOG_NDELAY | LOG_PID, LOG_SYSLOG);
+ 
+ 			if (($log_type == "err") && (read_config_option("log_perror"))) {
+ 				syslog(LOG_CRIT, $environ . ": " . $string);
+ 			}
+ 
+ 			if (($log_type == "warn") && (read_config_option("log_pwarn"))) {
+ 				syslog(LOG_WARNING, $environ . ": " . $string);
+ 			}
+ 
+ 			if ((($log_type == "stat") || ($log_type == "note")) && (read_config_option("log_pstats"))) {
+ 				syslog(LOG_INFO, $environ . ": " . $string);
+ 			}
+ 
+ 			closelog();
+ 		}
+ 	}
+ }
+ 
+ /*
+ PHPSsh
+ by Abdul Pallares: abdul(at)riereta.net
+ following the PHPTelnet structure 
+ and looking arround on Internet
+ */
+ class PHPSsh {
+ 	var $show_connect_error=1;
+ 	var $timeout=1; //Seconds to avoid buggies connections
+ 	var $connection=NULL; //stores the ssh connection pointer
+ 	var $stream=NULL; //points to the ssh session stream
+ 	var $errorcode=0;
+ 	var $use_usleep=0;	// change to 1 for faster execution
+ 		// don't change to 1 on Windows servers unless you have PHP 5
+ 	var $debug='';
+ 	var $error=0;
+ 	
+ 	/*
+ 	0 = success
+ 	1 = couldn't open network connection
+ 	2 = unknown host
+ 	3 = login failed
+ 	4 = No ssh2 extension 
+ 	5 = Error enabling device
+ 	*/
+ 	function Connect($server, $user, $pass, $devicetype) {
+ 		$this->debug = '';
+ 		$rv=0;
+ 
+ 		if (!function_exists("ssh2_auth_password"))
+ 		{
+ 			plugin_routerconfigs_log($ip."-> DEBUG: PHP doesn't have the ssh2 module installed\nFollow the installation instructions in the official manual at http://www.php.net/manual/en/ssh2.installation.php");
+ 			$rv=4;
+ 		}
+ 
+ 		if (strlen($server)) {
+ 			if (preg_match('/[^0-9.]/',$server)) {
+ 				$ip=gethostbyname($server);
+ 				if ($ip==$server) {
+ 					$ip='';
+ 					$rv=2;
+ 				}
+ 			} else $ip=$server;
+ 		} else $ip='127.0.0.1';
+ 
+ 		if (strlen($ip)) {
+ 			if(!($this->connection = @ssh2_connect($server, 22))){
+ 					$rv=1;
+ 			} else {
+ 				// try to authenticate
+ 				if(!@ssh2_auth_password($this->connection, $user, $pass)) {
+ 					$rv=3;
+ 				} else {
+ 					if($this->stream = @ssh2_shell($this->connection,"xterm")) {
+ 						$this->GetResponse($response);
+ 						plugin_routerconfigs_log($ip."-> DEBUG: okay: logged in...");
+ 					}
+ 
+ 					if (strstr($response,">")) {
+ 						$this->DoCommand("en",$response);
+ 						if (stristr($response,$devicetype["password"])) {
+ 							if($this->DoCommand($pass,$response)){
+ 								plugin_routerconfigs_log($ip."-> DEBUG: Enable login failed");
+ 								$this->Disconnect();
+ 								$rv=5;
+ 							}
+ 							if (strstr($response,"#"))
+ 								plugin_routerconfigs_log($ip."-> DEBUG: Ok we are in enabled mode");
+ 						}
+ 					}
+ 				}
+ 			}
+ 		}
+ 		if ($rv) {
+ 			$error=$this->ConnectError($rv);
+ 			if (strstr($error,"ERROR")) echo $ip."-> ".$error."<br>";
+ 			plugin_routerconfigs_log($ip."-> ".$error);
+ 		}
+ 	
+ 		return $rv; //everything goes well ;)
+ 	}
+ 
+ 	function Disconnect($exit=1) {
+ 		if ($this->stream) {
+ 			if ($exit)
+ 				$this->DoCommand('exit', $junk);
+ 			fclose($this->stream);
+ 			$this->stream=NULL;
+ 		}
+ 	}
+ 
+ 	function DoCommand($cmd, &$response) {
+ 		$result=0;
+ 		if ($this->connection) {
+ 			fwrite($this->stream,$cmd.PHP_EOL);
+ 			$this->Sleep();
+ 			$result=$this->GetResponse($response);
+ 			$response=preg_replace("/^.*?\n(.*)\n([^\n]*)$/", "$2", $response);
+ 		}
+ 		return $result;
+ 	}
+ 	
+ 	function Sleep() {
+ 		if ($this->use_usleep) usleep($this->sleeptime);
+ 		else sleep(1);
+ 	}
+ 
+ 	function GetResponse(&$response) {
+ 		global $devicetype;
+ 		$time_start = time();
+ 		$data = "";
+ 
+ 		while( true && isset($this->stream)){
+ 			while( $buf = fgets($this->stream) ){
+ 				$response .= $buf;
+ 				plugin_routerconfigs_log("DEBUG: SSH buffer=$buf");
+ 				if(strstr($buf,"#") || strstr($buf,">") || strstr($buf,"]?") || strstr($buf, $devicetype['password']))
+ 					return 0;
+ 			}
+ 
+ 			if( (time()-$time_start) > $this->timeout ){
+ 				plugin_routerconfigs_log("DEBUG: SSH timeout of {$this->timeout} seconds has been reached");
+ 				return 8;
+ 			}
+ 		}
+ 		return 0;
+ 	}
+ 
+ 	function ConnectError($num) {
+ 		if ($this->show_connect_error) {
+ 			$this->error=$num;
+ 			switch ($num) {
+ 				case 1:
+ 					return 'WARNING: Unable to open ssh network connection';
+ 					break;
+ 				case 2:
+ 					return 'ERROR: Unknown host';
+ 					break;
+ 				case 3:
+ 					return 'ERROR: SSH login failed';
+ 					break;
+ 				case 4:
+ 					return "ERROR: PHP doesn't have the ssh2 module installed\nFollow the installation instructions in the official manual: http://www.php.net/manual/en/ssh2.installation.php";
+ 					break;
+ 				case 5:
+ 					return 'ERROR: Bad download of config';
+ 					break;
+ 				case 6:
+ 					return 'ERROR: SSH access not Permitted';
+ 					break;
+ 				case 7:
+ 					return 'ERROR: SSH no Config uploaded from Router';
+ 					break;
+ 				case 8:
+ 					return "NOTICE: SSH Timeout of {$this->timeout} seconds has been reached";
+ 					break;
+ 				case 9:
+ 					return "ERROR: SSH Enable login failed";
+ 			}
+ 		}
+ 	}
+ }
+ 
+ /*
  PHPTelnet 1.1
  by Antone Roundy
  adapted from code found on the PHP website
***************
*** 417,429 ****
  				@fputs($this->fp, $this->conn2);
  				$this->Sleep();
  				$this->GetResponse($r);
! 				if (strpos($r, $device['username']) === FALSE && strpos($r, 'Access not permitted.') !== FALSE || !$this->fp) {
  					echo $r;
  					return 6;
  				}
  
  				if (strpos($r, $devicetype['username']) !== FALSE) {
! 					fputs($this->fp, "$user\r");
  					$this->Sleep();
  				}
  				$r=explode("\n", $r);
--- 693,705 ----
  				@fputs($this->fp, $this->conn2);
  				$this->Sleep();
  				$this->GetResponse($r);
! 				if (strpos($r, $devicetype['username']) === FALSE && strpos($r, 'Access not permitted.') !== FALSE || !$this->fp) {
  					echo $r;
  					return 6;
  				}
  
  				if (strpos($r, $devicetype['username']) !== FALSE) {
! 					@fputs($this->fp, "$user\r");
  					$this->Sleep();
  				}
  				$r=explode("\n", $r);
***************
*** 434,440 ****
  				while ($x < 10) {
  					sleep(1);
  					$this->GetResponse($r);
! echo $r;
  					$res .= $r;
  					if (strpos($r, '#') !== FALSE || strpos($r, '> (') !== FALSE) {
  						break;
--- 710,716 ----
  				while ($x < 10) {
  					sleep(1);
  					$this->GetResponse($r);
! 					plugin_routerconfigs_log($ip."-> DEBUG: $r");
  					$res .= $r;
  					if (strpos($r, '#') !== FALSE || strpos($r, '> (') !== FALSE) {
  						break;
***************
*** 449,455 ****
  			} else $rv = 1;
  		}
  		
! 		if ($rv) echo $this->ConnectError($rv);
  		return $rv;
  	}
  	
--- 725,734 ----
  			} else $rv = 1;
  		}
  		
! 		if ($rv) {
! 			echo $ip."-> ".$this->ConnectError($rv)."<br>";
! 			plugin_routerconfigs_log($ip."-> ".$this->ConnectError($rv));
! 		}
  		return $rv;
  	}
  	
***************
*** 464,472 ****
  
  	function DoCommand($c, &$r) {
  		if ($this->fp) {
! 			fputs($this->fp, "$c\r");
  			$this->Sleep();
  			$this->GetResponse($r);
  			$r = preg_replace("/^.*?\n(.*)\n[^\n]*$/", "$1", $r);
  		}
  		return $this->fp ? 1 : 0;
--- 743,752 ----
  
  	function DoCommand($c, &$r) {
  		if ($this->fp) {
! 			@fputs($this->fp, "$c".PHP_EOL);
  			$this->Sleep();
  			$this->GetResponse($r);
+ 			plugin_routerconfigs_log("DEBUG: DoCommand result=$r");
  			$r = preg_replace("/^.*?\n(.*)\n[^\n]*$/", "$1", $r);
  		}
  		return $this->fp ? 1 : 0;
***************
*** 477,482 ****
--- 757,763 ----
  		stream_set_timeout($this->fp, 1);
  		do { 
  			$r .= fread($this->fp, 2048);
+ 			plugin_routerconfigs_log("DEBUG: Telnet response:$r");
  			$this->debug .= $r;
  			$s = socket_get_status($this->fp);
  		} while ($s['unread_bytes']);
***************
*** 509,534 ****
  			$this->error = $num;
  			switch ($num) {
  				case 1:
! 					return 'Unable to open network connection';
  					break;
  				case 2:
! 					return 'Unknown host';
  					break;
  				case 3:
! 					return 'Login failed';
  					break;
  				case 4:
! 					return 'Connect failed: Your servers PHP version is too low for PHP Telnet';
  					break;
  				case 5:
! 					return 'Bad download of config';
  					break;
  				case 6:
! 					return 'Access not Permitted';
  					break;
  				case 7:
! 					return 'No Config uploaded from Router';
  					break;
  			}
  		}
  		return '';
--- 790,817 ----
  			$this->error = $num;
  			switch ($num) {
  				case 1:
! 					return 'ERROR: Unable to open telnet network connection';
  					break;
  				case 2:
! 					return 'ERROR: Unknown host';
  					break;
  				case 3:
! 					return 'ERROR: TELNET login failed';
  					break;
  				case 4:
! 					return 'ERROR: Connect failed: Your servers PHP version is too low for PHP Telnet';
  					break;
  				case 5:
! 					return 'ERROR: Bad download of config';
  					break;
  				case 6:
! 					return 'ERROR: TELNET access not Permitted';
  					break;
  				case 7:
! 					return 'ERROR: TELNET no Config uploaded from Router';
  					break;
+ 				case 9:
+ 					return "ERROR: TELNET Enable login failed";
  			}
  		}
  		return '';
diff -crB routerconfigs-orig/routerconfigs.sql routerconfigs/routerconfigs.sql
*** routerconfigs-orig/routerconfigs.sql	2008-09-07 21:09:13.000000000 +0200
--- routerconfigs/routerconfigs.sql	2009-05-12 17:57:08.000000000 +0200
***************
*** 49,69 ****
    KEY devicetype (devicetype)
  ) ENGINE=MyISAM;
  
! CREATE TABLE plugin_routerconfigs_devicetypes (
!   id int(12) NOT NULL auto_increment,
    `name` varchar(128) NOT NULL,
!   username varchar(64) NOT NULL default 'sername:',
    `password` varchar(128) NOT NULL default 'assword:',
!   copytftp varchar(64) NOT NULL default 'copy tftp run',
!   version varchar(64) NOT NULL default 'show version',
!   confirm varchar(64) NOT NULL default '',
!   forceconfirm int(1) NOT NULL,
!   PRIMARY KEY  (id)
! ) TYPE=MyISAM;
! 
! INSERT INTO plugin_routerconfigs_devicetypes (id, name, username, password, copytftp, version, confirm, forceconfirm) VALUES (1, 'Cisco IOS', 'sername:', 'assword:', 'copy run tftp', 'show version', 'y', 0),
! (2, 'Cisco CatOS', 'sername:', 'assword:', 'copy config tftp', '', 'y', 1);
! 
! 
! 
  
--- 49,73 ----
    KEY devicetype (devicetype)
  ) ENGINE=MyISAM;
  
! REATE TABLE `plugin_routerconfigs_devicetypes` (
!   `id` int(12) NOT NULL auto_increment,
    `name` varchar(128) NOT NULL,
!   `username` varchar(64) NOT NULL default 'sername:',
    `password` varchar(128) NOT NULL default 'assword:',
!   `copytftp` varchar(64) NOT NULL default 'copy tftp run',
!   `version` varchar(64) NOT NULL default 'show version',
!   `confirm` varchar(64) NOT NULL default '',
!   `forceconfirm` int(1) NOT NULL,
!   `checkendinconfig` tinyint(1) NOT NULL default '1',
!   PRIMARY KEY  (`id`)
! ) ENGINE=MyISAM;
  
+ LOCK TABLES `plugin_routerconfigs_devicetypes` WRITE;
+ /*!40000 ALTER TABLE `plugin_routerconfigs_devicetypes` DISABLE KEYS */;
+ INSERT INTO `plugin_routerconfigs_devicetypes` VALUES
+ (1,'Cisco IOS','sername:','assword:','copy start tftp','show version','y',0,1),
+ (2,'Cisco CatOS','sername:','assword:','copy config','','y',1,1),
+ (3,'Cisco ASA','sername:','assword:','copy start tftp://%SERVER%/%FILE%','show version','y',0,1),
+ (4,'Cisco CSS','sername:','assword:','copy start tftp %SERVER% %FILE%','show chasis','n',0,0);
+ /*!40000 ALTER TABLE `plugin_routerconfigs_devicetypes` ENABLE KEYS */;
+ UNLOCK TABLES;
diff -crB routerconfigs-orig/router-devices.php routerconfigs/router-devices.php
*** routerconfigs-orig/router-devices.php	2008-09-07 21:09:13.000000000 +0200
--- routerconfigs/router-devices.php	2009-05-06 17:56:43.000000000 +0200
***************
*** 396,402 ****
  	}
  
  	load_current_session_value("page", "sess_routerconfigs_devices_current_page", "1");
! 	$num_rows = 30;
  
  	$sql = "SELECT * FROM plugin_routerconfigs_devices ";
  	if ($account != '') {
--- 395,401 ----
  	}
  
  	load_current_session_value("page", "sess_routerconfigs_devices_current_page", "1");
! 	$num_rows = 30000;
  
  	$sql = "SELECT * FROM plugin_routerconfigs_devices ";
  	if ($account != '') {
