[Project] Making Telnet Connection
Hi there,
I am new to php. I would like to make a website that can make telnet connection. Connecting to the terminal server and login to the selected networking device. I saw this script (requires the other file as well) in a website for making telnet connection but i do not know what to put at the 'DoCommand' part so i only put 'enable'. When i load the page, i see 'enable Password: Password: '. How do i put the password in so that it will go be able to do the 'sh run' command? Anyone familiar with telnet connection?
PHP Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>
<body>
<?php
require_once "PHPTelnet.php";
$telnet = new PHPTelnet();
$telnet->show_connect_error=0;
// if the first argument to Connect is blank,
// PHPTelnet will connect to the local host via 127.0.0.1
$result = $telnet->Connect('172.16.221.210','cisco','class'); //wwww.somewhere.com, username, password
switch ($result) {
case 0:
$telnet->DoCommand('enable', $result);
// NOTE: $result may contain newlines
echo $result;
$telnet->DoCommand('sh run', $result);
echo $result;
// say Disconnect(0); to break the connection without explicitly logging out
$telnet->Disconnect();
break;
case 1:
echo '[PHP Telnet] Connect failed: Unable to open network connection';
break;
case 2:
echo '[PHP Telnet] Connect failed: Unknown host';
break;
case 3:
echo '[PHP Telnet] Connect failed: Login failed';
break;
case 4:
echo '[PHP Telnet] Connect failed: Your PHP version does not support PHP Telnet';
break;
}
?>
</body>
</html>
Re: [Project] Making Telnet Connection
well, there isn't enough if to tell you what to do. What you need to post is the PHPTelnet.php, and I can probably tell what you need to post. It looks like your trying to connect to a cisco device and issue a show run, but you need input the password before you get into privledge mode.
Re: [Project] Making Telnet Connection
Here's the PHPTelnet.php file:
PHP Code:
<?php
/*
PHPTelnet 1.1
by Antone Roundy
adapted from code found on the PHP website
public domain
*/
class PHPTelnet {
var $show_connect_error=1;
var $use_usleep=0; // change to 1 for faster execution
// don't change to 1 on Windows servers unless you have PHP 5
var $sleeptime=125000;
var $loginsleeptime=1000000;
var $fp=NULL;
var $loginprompt;
var $conn1;
var $conn2;
/*
0 = success
1 = couldn't open network connection
2 = unknown host
3 = login failed
4 = PHP version too low
*/
function Connect($server,$user,$pass) {
$rv=0;
$vers=explode('.',PHP_VERSION);
$needvers=array(4,3,0);
$j=count($vers);
$k=count($needvers);
if ($k<$j) $j=$k;
for ($i=0;$i<$j;$i++) {
if (($vers[$i]+0)>$needvers[$i]) break;
if (($vers[$i]+0)<$needvers[$i]) {
$this->ConnectError(4);
return 4;
}
}
$this->Disconnect();
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->fp=fsockopen($ip,23)) {
fputs($this->fp,$this->conn1);
$this->Sleep();
fputs($this->fp,$this->conn2);
$this->Sleep();
$this->GetResponse($r);
$r=explode("\n",$r);
$this->loginprompt=$r[count($r)-1];
fputs($this->fp,"$user\r");
$this->Sleep();
fputs($this->fp,"$pass\r");
if ($this->use_usleep) usleep($this->loginsleeptime);
else sleep(1);
$this->GetResponse($r);
$r=explode("\n",$r);
if (($r[count($r)-1]=='')||($this->loginprompt==$r[count($r)-1])) {
$rv=3;
$this->Disconnect();
}
} else $rv=1;
}
if ($rv) $this->ConnectError($rv);
return $rv;
}
function Disconnect($exit=1) {
if ($this->fp) {
if ($exit) $this->DoCommand('exit',$junk);
fclose($this->fp);
$this->fp=NULL;
}
}
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;
}
function GetResponse(&$r) {
$r='';
do {
$r.=fread($this->fp,1000);
$s=socket_get_status($this->fp);
} while ($s['unread_bytes']);
}
function Sleep() {
if ($this->use_usleep) usleep($this->sleeptime);
else sleep(2);
}
function PHPTelnet() {
$this->conn1=chr(0xFF).chr(0xFB).chr(0x1F).chr(0xFF).chr(0xFB).
chr(0x20).chr(0xFF).chr(0xFB).chr(0x18).chr(0xFF).chr(0xFB).
chr(0x27).chr(0xFF).chr(0xFD).chr(0x01).chr(0xFF).chr(0xFB).
chr(0x03).chr(0xFF).chr(0xFD).chr(0x03).chr(0xFF).chr(0xFC).
chr(0x23).chr(0xFF).chr(0xFC).chr(0x24).chr(0xFF).chr(0xFA).
chr(0x1F).chr(0x00).chr(0x50).chr(0x00).chr(0x18).chr(0xFF).
chr(0xF0).chr(0xFF).chr(0xFA).chr(0x20).chr(0x00).chr(0x33).
chr(0x38).chr(0x34).chr(0x30).chr(0x30).chr(0x2C).chr(0x33).
chr(0x38).chr(0x34).chr(0x30).chr(0x30).chr(0xFF).chr(0xF0).
chr(0xFF).chr(0xFA).chr(0x27).chr(0x00).chr(0xFF).chr(0xF0).
chr(0xFF).chr(0xFA).chr(0x18).chr(0x00).chr(0x58).chr(0x54).
chr(0x45).chr(0x52).chr(0x4D).chr(0xFF).chr(0xF0);
$this->conn2=chr(0xFF).chr(0xFC).chr(0x01).chr(0xFF).chr(0xFC).
chr(0x22).chr(0xFF).chr(0xFE).chr(0x05).chr(0xFF).chr(0xFC).chr(0x21);
}
function ConnectError($num) {
if ($this->show_connect_error) switch ($num) {
case 1: echo '<br />[PHP Telnet] <a href="http://www.geckotribe.com/php-telnet/errors/fsockopen.php">Connect failed: Unable to open network connection</a><br />'; break;
case 2: echo '<br />[PHP Telnet] <a href="http://www.geckotribe.com/php-telnet/errors/unknown-host.php">Connect failed: Unknown host</a><br />'; break;
case 3: echo '<br />[PHP Telnet] <a href="http://www.geckotribe.com/php-telnet/errors/login.php">Connect failed: Login failed</a><br />'; break;
case 4: echo '<br />[PHP Telnet] <a href="http://www.geckotribe.com/php-telnet/errors/php-version.php">Connect failed: Your server\'s PHP version is too low for PHP Telnet</a><br />'; break;
}
}
}
?>
Re: [Project] Making Telnet Connection
PHP Code:
require_once "PHPTelnet.php";
$telnet = new PHPTelnet();
$telnet->show_connect_error=0;
// if the first argument to Connect is blank,
// PHPTelnet will connect to the local host via 127.0.0.1
$result = $telnet->Connect('172.16.221.210','cisco','class'); //wwww.somewhere.com, username, password
switch ($result) {
case 0:
$telnet->DoCommand('enable', $result);
// NOTE: $result may contain newlines
echo $result; // You need to be checking all possible resultants for the function DoCommand.
//After that should determine you next doDocommand.
//for example, issuing a password instead of show run?
$telnet->DoCommand('sh run', $result);
//Considering the PHPTelnet, I probably would use a switch statement
echo $result;
// say Disconnect(0); to break the connection without explicitly logging out
$telnet->Disconnect();
Re: [Project] Making Telnet Connection
I see... now i tried to pass in the password and other command but i got this error in return:
Code:
clear line 34 [confirm]2CO# Trying 2r2 (192.2.1.1, 2034)... Open
Fatal error: Maximum execution time of 30 seconds exceeded in C:\Program Files\Apache Group\Apache2\htdocs\terminalserver_D\PHPTelnet.php on line 99
Here's my current php coding for the telnetTest.php file:
PHP Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>
<body>
<?php
require_once "PHPTelnet.php";
$telnet = new PHPTelnet();
$telnet->show_connect_error=0;
// if the first argument to Connect is blank,
// PHPTelnet will connect to the local host via 127.0.0.1
$result = $telnet->Connect('172.16.221.210','cisco','class'); //wwww.somewhere.com, username, password
//echo $result;
$telnet->DoCommand('class', $temp);
//echo $temp;
//$telnet->DoCommand('class', $temp);
switch ($result) {
case 0:
$telnet->DoCommand('enable', $result);
$telnet->DoCommand('class', $temp);
// NOTE: $result may contain newlines
$telnet->DoCommand('clear line 34', $result);
echo $result;
$telnet->DoCommand(' ', $result);
echo $result;
$telnet->DoCommand('2r2', $result);
echo $result;
$telnet->DoCommand('enable', $result);
$telnet->DoCommand('class', $temp);
//$telnet->DoCommand('OK', $result);
//$telnet->DoCommand('2r2', $result);
//echo $result;
//$telnet->DoCommand('enable', $result);
//$telnet->DoCommand('class', $temp);
//echo $result;
//$telnet->DoCommand('sh run', $temp);
//echo $result;
$telnet->DoCommand(' ', $result);
echo $result;
$telnet->DoCommand(' ', $result);
echo $result;
// say Disconnect(0); to break the connection without explicitly logging out
$telnet->Disconnect();
break;
case 1:
echo '[PHP Telnet] Connect failed: Unable to open network connection';
break;
case 2:
echo '[PHP Telnet] Connect failed: Unknown host';
break;
case 3:
echo '[PHP Telnet] Connect failed: Login failed';
break;
case 4:
echo '[PHP Telnet] Connect failed: Your PHP version does not support PHP Telnet';
break;
}
?>
</body>
</html>
How do i make it that i can use the form element to key in username, telnet password as well as enable password to login to the selected device?
But i have to be able to connect to the Terminal Server first to make the telnet connection.
How do i do that?
Re: [Project] Making Telnet Connection
the programmed timeout which looks to be deal with a apache and php.ini possibly. You will have to google for how to fix that error message, and it will depend on your apache/php ver. it isn't your code it is the server.
Re: [Project] Making Telnet Connection
So do you mean its my server having the problem and i have to use search engines to look up for the error?
>.<
Re: [Project] Making Telnet Connection
your php script is timing out. i don't know why someone would suggest for you to have to look up that error on google -- it's kind of self explanatory.
use set_time_limit to allow your script to run freely, and this should fix your problem (at least for this portion of your problems!)
PHP Code:
<?php
set_time_limit(0);
//........
Re: [Project] Making Telnet Connection
Quote:
Originally Posted by FiOh
So do you mean its my server having the problem and i have to use search engines to look up for the error?
>.<
http://www.google.com/search?q=Fatal...x=&startPage=1