Jump to content

[SOLVED] PHP Telnet help


jaimitoc30

Recommended Posts

??? Hi, everybody.  I have been reading all over the internet about this little problem that I have and I have decided to post it here.  This is a nice place that has been helping me with my PHP projects so far. I have a big problem here when trying to connect to a telnet server.  I am using PHPTelnet class from www.geckotribe.com/php-telnet/  It is a nice tool for making telnet connections and running commands.  The problem that I have is that the script gets stuck when it tries to fread() from the telnet server.  I am not sure why, and I am unable to accomplish the connection.  The server is using Pragma telnet server.  Is under Windows NT. Also, when I get the responses when it tries to log in it retrieves a lot of garbage to the variable $r.  To be more specific, it does the fread() when the script tries to log in, but when it tries to run a command and get the response from the server and assign it to the variable $r it gets stuck.  Below is the code for my telnet connection:

<?php

require_once "../SMSO/telnet/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('10.222.160.70','username','password');//I changed the username and password

switch ($result) {

case 0:

$telnet->DoCommand('hgsdc:msisdn=50766195185,sud=TS22-0;', $result);

// NOTE: $result may contain newlines

echo $result;

$telnet->DoCommand('hgsdc:msisdn=50766195185,sud=TS22-0;', $result);

echo $result;

// say Disconnect(0); to break the connection without explicitly logging out

$telnet->Disconnect();

break;

case 1:

echo '

 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;

}

?>

 

 

 

And below is the code for the PHPTelnet class. 

 

<?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);

fputs($this->fp,"\r");

if ($this->use_usleep) usleep($this->loginsleeptime);

else sleep(1);

$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,10000);

$s=socket_get_status($this->fp);

} while ($s['unread_bytes']);

}

 

function Sleep() {

if ($this->use_usleep) usleep($this->sleeptime);

else sleep(1);

}

 

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;

}

}

}

?>

 

I am not sure if the issue lies in the server.  In the server when I try to connect via SSH I get the windows prompt, when I try to connect via Telnet it automatically forwards me to the system where I need to run the commands.

 

I would appreciate any help with this and I assure you if you need assistance with something, I will do my best to help you.

 

Thanks in Advance.

 

Link to comment
Share on other sites

Here is my script:

 

<?php
require_once "../SMSO/telnet/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('10.222.160.70','scl_alt2','4pan3L');
switch ($result) {
case 0: 
$telnet->DoCommand('hgsdc:msisdn=50766195185,sud=TS22-0;', $result);
// NOTE: $result may contain newlines
echo $result;
$telnet->DoCommand('hgsdc:msisdn=50766195185,sud=TS22-0;', $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; 
}
?> 

 

 

 

Here is the class:

 

<?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);
			fputs($this->fp,"\r");
			if ($this->use_usleep) usleep($this->loginsleeptime);
			else sleep(1);
			$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,10000000);
		$s=socket_get_status($this->fp);
	} while ($s['unread_bytes']);
}

function Sleep() {
	if ($this->use_usleep) usleep($this->sleeptime);
	else sleep(1);
}

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;
	}
}
}
?>

Link to comment
Share on other sites

Ok, i was able to resolve this issue.

 

Basically I connected to the server via SSH using the ssh2 library, opened a shell in the server and ran the telnet command and then used fwrite to type the command and \r  for the enter.  It worked perfectly.  Just in case some of you need information on how I did this, I am available.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.