Re: Can PHP works with telnet?
| From: | HoWang Wang | Date: | Mon, 23 Jan 2006 10:56:13 +0000 |
| Subject: | Re: Can PHP works with telnet? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-229064@lists.php.net to get a copy of this message | ||
Hi,
It's me again. I have take a look on http://www.geckotribe.com/php-telnet/ , but I think the codes are not friendly to me. So I create my own telnet class for my program. I'm going to post it here for reference. But it is made just for my program so you may have difficulty when you try to use it on a normal telnet connection.
<?php
class telnet
{
// Host
var $host = '127.0.0.1';
// Port
var $port = 23;
// Use usleep?
var $usleep = 1;
// --- End of config ---
// File Pointer
var $fp;
// This is the Telnet headers
var $header1;
var $header2;
function telnet($host,$port)
{
if (!empty($host)) $this->host = $host;
if (!empty($port)) $this->port = $port;
$this->header1=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->header2=chr(0xFF).chr(0xFC).chr(0x01).chr(0xFF).chr(0xFC).chr(0x22).chr(0xFF).chr(0xFE).
chr(0x05).chr(0xFF).chr(0xFC).chr(0x21);$this->connect(); } function sleep() { if ($this->usleep) { usleep(125000); } else { sleep(1); } } function connect() { $this->fp = fsockopen($this->host,$this->port); fputs($this->fp,$this->header1); $this->sleep(); fputs($this->fp,$this->header2); $this->sleep(); } function docmd($cmd) { fputs($this->fp,$cmd . "\r"); $this->sleep(); } function readresp() { do { // read line by line, or at least small chunks $output.=fread($fp, 80); $stat=socket_get_status($fp); } while($stat["unread_bytes"]); $output = str_replace("\n", "<br>", $output); echo $output; } function disconnect() { fclose($this->fp); } } ?> Thanks to all the repliers. HoWang Wang wrote:
Hi all, Recently I got a job to write a PHP webpage to display some infomation which have to obtain from telnet. Telnet is the only way to query the software. I have made a quick search on php.net but I can't found amy extension support telnet. Is there any way to do so? Or it is impossible with PHP but can be done with external binaries? Please help, thanks. HoWang Wong