[Project] Making Telnet Connection

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
FiOh
Forum Commoner
Posts: 39
Joined: Tue Dec 12, 2006 12:20 am

[Project] Making Telnet Connection

Post by FiOh »

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?

Code: Select all

<!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>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Don't you have a thread on this already?

viewtopic.php?t=62010
FiOh
Forum Commoner
Posts: 39
Joined: Tue Dec 12, 2006 12:20 am

Post by FiOh »

But this thread here i am asking for help to write my own telnet code.
>_<
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Well then, read up on telnet. :?
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post by nickvd »

I'd recommend that you start here: http://www.faqs.org/rfcs/rfc854.html
Then after you're finished, take a gander at this: http://www.faqs.org/rfcs/rfc318.html
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: [Project] Making Telnet Connection

Post by onion2k »

FiOh wrote: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: '.
The problem is that the enable command is interactive, it expects a user to enter a password. I imagine that another call to DoCommand with just the password may work if your telnet class is just passing whatever you enter and reading the response. Otherwise you'll need to work out a method of running 'enable' in a non-interactive fashion, so it doesn't ask for a password.

IMPORTANT NOTE: The telnet protocol is unencrypted and easy to snoop on. Passing a password in plaintext over it means you're opening your system up to the world. If this is something important I would seriously reconsider your approach.
FiOh
Forum Commoner
Posts: 39
Joined: Tue Dec 12, 2006 12:20 am

Re: [Project] Making Telnet Connection

Post by FiOh »

onion2k wrote:
FiOh wrote: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: '.
The problem is that the enable command is interactive, it expects a user to enter a password. I imagine that another call to DoCommand with just the password may work if your telnet class is just passing whatever you enter and reading the response. Otherwise you'll need to work out a method of running 'enable' in a non-interactive fashion, so it doesn't ask for a password.

IMPORTANT NOTE: The telnet protocol is unencrypted and easy to snoop on. Passing a password in plaintext over it means you're opening your system up to the world. If this is something important I would seriously reconsider your approach.
Do you mean it is dangerous to make a telnet connection? But the web page i am using is through local host...

This is the PHPTelnet.php file:

Code: Select all

<?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); //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;
		}
	}
}
?>
This is my current telnetTest.php:

Code: Select all

<!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>
The results returns from the browser is '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'.


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?
:?:
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: [Project] Making Telnet Connection

Post by onion2k »

FiOh wrote: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?
It sounds rather like you're trying to make an interactive session through a browser. You can do it, but you'll need to keep the script alive while the user is entering things. PHP isn't really designed for that. It's meant for non-interactive scripts that run, do stuff, and close. You might be better off thinking of a different approach.

Of course, if you're just asking how to use form data in your script then you'll need to read http://uk.php.net/manual/en/language.va ... ternal.php
Post Reply