PHP implementation of the Telnet feature sample _php tutorial

Source: Internet
Author: User
Tags usleep
Copy CodeThe code is as follows:
Class Phptelnet {
var $show _connect_error=1;

var $use _usleep=0; Change to 1 for faster execution
Don ' t change the 1 on the Windows servers unless you have PHP 5
var $sleeptime = 125000;
var $loginsleeptime = 1000000;

var $fp =null;
var $loginprompt;

var $conn 1;
var $conn 2;

/*
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 \ n");
$this->sleep ();

Fputs ($this->fp, "$pass \ n");
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-&GT;FP);
$this->fp=null;
}
}

function DoCommand ($c,& $r) {
if ($this->fp) {
Fputs ($this->fp, "$c \ n");
$this->sleep ();
$this->getresponse ($R);
$r =preg_replace ("/^.*?\n (. *) \n[^\n]*$/", "$", $r);
}
return $this->fp?1:0;
}

Function GetResponse (& $r) {
$r = ";
do {
$r. =fread ($this-&GT;FP, 1024);
$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-&GT;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-&GT;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] 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 server\ ' s PHP version is too low for PHP Telnet
'; Break
}
}
}

?>

http://www.bkjia.com/PHPjc/750857.html www.bkjia.com true http://www.bkjia.com/PHPjc/750857.html techarticle Copy the code as follows: PHP 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 y ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.