FreeBSD Telnet using PHP

Hi

i would to telnet my fbsd server using web page based on php

what should i do to achieve that??

for example i want to execute the command :
Code:
#telnet localhost 2601
password:zebra
>show ip route

any idea??
 
I face strange thing!! I have found this php code :
PHP:
<?php
require_once "PHPTelnet.php";

$telnet = new PHPTelnet();

// if the first argument to Connect is blank,
// PHPTelnet will connect to the local host via 127.0.0.1
$result = $telnet->Connect('127.0.0.1','','zebra');

if ($result == 0) { 
$telnet->DoCommand('show ip route', $result);
// NOTE: $result may contain newlines
echo $result;
//$telnet->DoCommand('another command', $result);
//echo $result;
// say Disconnect(0); to break the connection without 

explicitly logging out
$telnet->Disconnect(); 
}
?>

with this php class:
PHP:
<?php
/*
PHPTelnet 1.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,2601)) {
				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(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 '
[PHP Telnet] <a href="http://www.geckotribe.com/php-telnet/errors/fsockopen.php">Connect failed: Unable to open network connection</a>
'; break;
		case 2: echo '
[PHP Telnet] <a href="http://www.geckotribe.com/php-telnet/errors/unknown-host.php">Connect failed: Unknown host</a>
'; break;
		case 3: echo '
[PHP Telnet] <a href="http://www.geckotribe.com/php-telnet/errors/login.php">Connect failed: Login failed</a>
'; break;
		case 4: echo '
[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>
'; break;
		}
	}
}

return;
?>

when i try to execute this code with freebsd hosting , it does not work it shows nothig !! (blank page)

but when i try it in windows machine it works fine (i have changed the ip of teleneting to freebsd ip)
i mean if this php codes in my windows machine (with telnet ip =freebsd server ip) then i can telent freebsd server with no problems, but if i put this php codes in freebsd machine (ip =localhost) they do not work???
any ideas plz??
 
killasmurf86 said:
check if telnet is listening to port 23 on loopback device
# sockstat -4 | grep 23

But i want to telnet to the port 2601 not 23

# telnet localhost 2601

this is for zebra daemon.

Telneting sucseeds as a normal command in freebsd shell, but it does not with using previous php codes.
 
killasmurf86 said:
then check if telnet is listening to port 2601
Code:
# sockstat -4 | grep 2601

This is your command output:

Code:
quagga zebra 344 8 tcp4 *:2601 *:*


does it refer to any thing wrong?
 
killasmurf86 said:
I don't think that's telnet listening to port 2601

But # telnet localhost 2601 works fine if i execute it directly in FreeBSD shell. Does that mean that telnet is listening to port 2601 correctly?

Ok, How can i force it to listen to the port 2601?
 
I dunno, I never used telnet

in your code
Code:
$result = $telnet->Connect('127.0.0.1','','zebra');
maybe replace 'zebra' with 2601, maybe that'll work
 
killasmurf86 said:
I dunno, I never used telnet

in your code
Code:
$result = $telnet->Connect('127.0.0.1','','zebra');
maybe replace 'zebra' with 2601, maybe that'll work

No, I already have selected the port 2601 for telneting (via the class code,see the second php code).
'zebra' is the password not the port , that code should be in form of:
Code:
$result = $telnet->Connect('IP','username','password');

* The previuos php code work fine if i put it in windows machine with XAMPP (apache & php).
In windows i put these options
Code:
$result = $telnet->Connect('192.168.56.102','','zebra');
where 192.168.56.102 is the IP address of FreeBSD server which contains quagga (zebra) daemon. It works very nice and i can do any telnet command i need to freebsd zebara daemon.

But what I need is to put that php pages in the FreeBSD server itself so i must consider this code with freebsd hosting
Code:
$result = $telnet->Connect('localhost','','zebra');
..
but that code does not work, it does nothing!! :(:(:(:(:(:( I hope that my problem is clear now ??
 
killasmurf, you're confusing 'telnet' with 'telnetd'. You can telnet to any tcp port and perform actions, e.g. 25, 80, 110, 21, etc. In this sense, 'telnet' means something like 'connect to port and issue interactive commands'.

Example (commands issued are in black, system output in grey):

Code:
$ telnet www.freebsd.org 80
[color="YellowGreen"]Trying 69.147.83.33...
Connected to www.freebsd.org.
Escape character is '^]'.[/color]
HEAD / HTTP/1.0

[color="YellowGreen"]HTTP/1.0 301 Moved Permanently
Location: http://www.freebsd.org/
Connection: close
Date: Fri, 26 Mar 2010 22:13:30 GMT
Server: httpd/1.4.x LaHonda

Connection closed by foreign host.[/color]

OP simply wants to replace this 'manual command-line telnet' with a PHP-coded one.
 
Code:
<?php

if($c = fsockopen("127.0.0.1", 2601, $errno, $errstr)) {
 echo "Connected to zebra\n";
 // ...
} else {
 echo "Can't connect:\n"
 .$errno.": ".$errstr."\n";
}

Save this to zebra.php and do php -f zebra.php

fopen, fsockopen, etc. comes with default php setup.

The code I wrote is for debug. If it can connect then we could add something too for executing your command.
 
jailed said:
Code:
<?php

if($c = fsockopen("127.0.0.1", 2601, $errno, $errstr)) {
 echo "Connected to zebra\n";
 // ...
} else {
 echo "Can't connect:\n"
 .$errno.": ".$errstr."\n";
}

Save this to zebra.php and do php -f zebra.php

fopen, fsockopen, etc. comes with default php setup.

The code I wrote is for debug. If it can connect then we could add something too for executing your command.

It works fine! This is the output:

Connected to zebra

I hope this will be good news that can make our problem easy to solve. Am i right?
 
Yes, it's good thing.

First, delete or comment this line (18) in your code:
Code:
explicitly logging out

I read the php class you copied in your first post. And realized that it's using preg_match function.

So, do you have PCRE extension installed?

If you installed PHP with ports,

Code:
cd /usr/ports/lang/php5-extensions/
make config

On the config screen check if PCRE is selected. If not, select PCRE and press ok. Then,
Code:
make install clean

If it's still not working after you checked for PCRE, we can write a clean code or continue investigating.
 
Thanks for all members the problem was solved. Socket support was disabled. I enabled and all things proceed straightforward now.
 
Back
Top