PHP implementation Telnet Function Example _php instance

Source: Internet
Author: User
Tags chr explode usleep

Copy Code 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 for 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 (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 ' <br/>[php Telnet] <a href= "http://www.jb51.net/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.jb51.net/php-telnet/errors/unknown-host.php" >connect Failed:unknown host</a><br/> '; Break
Case 3:echo ' <br/>[php Telnet] <a href= "http://www.jb51.net/php-telnet/errors/login.php" >connect failed: Login failed</a><br/> '; Break
Case 4:echo ' <br/>[php Telnet] <a href= "http://www.jb51.net/php-telnet/errors/php-version.php" >connect Failed:your Server\ ' s PHP version is too to PHP telnet</a><br/> '; Break
}
}
}

?>

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.