例子,php telnet功能實作程式碼。
-
-
class PHPTelnet {
- var $show_connect_error=1;
- <p> var $use_usleep=0; // 更改為1 以加快執行速度
- // 在Windows 伺服器上不要更改為1,除非您有PHP 5
- var $sleeptime=125000;
- var $loginsleeptime= 1000000;
var $fp=NULL;
- var $loginprompt;
var $conn1;
- var $conn2;
/*
- 0 = 成功
- 1 = 無法開啟網路連線
- 2 = 未知主機
- 3 = 登入失敗
- 4 = PHP 版本過低
- * /
- 函數Connect($server,$user,$pass) {
- $rv=0;
- $vers=explode('.',PHP_VERSION);
- $needvers=array(4, 3,0) ;
- $j=count($vers);
- $k=count($needvers);
- if ($k for ($i= 0;$i if (($vers[$i]+0)>$needvers[$i]) break;
- if (($vers[ $i]) +0) $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,"$usern" );
- $this->Sleep();
fputs($this->fp,"$passn");
- 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;
- }
- } p>
function DoCommand($c,&$r) {
- if ($this->fp) {
- fputs($this->fp,"$cn");
- $this->; Sleep();
- $this->GetResponse($r);
- $r=preg_replace("/^.*?n(.*)n[^n]*$/ ","$1" ,$r);
- }
- 回傳$this->fp?1:0;
- }
function GetResponse(&$r) {
- $r='';
- do {
- $r.=fread($this->fp, 1024);
- $s=socket_get_status($ this->fp);
- } while ($s['unread_bytes']);
- }
function Sleep() {
- if ($this->use_usleep) usleep($this->sleeptime) ;
- else sleep(1);
- }
//telnet功能主函數
- 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(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(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).
- 字元(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] 連線失敗:無法開啟網路連線 '; Break;
- 情況2: echo '
[PHP Telnet] 連線失敗:未知主機 '; Break;
- 情況3: echo '
[PHP Telnet] ;連線失敗:登入失敗 '; Break;
- 情況4: echo '
[PHP Telnet] 連線失敗:您的伺服器的PHP 版本太低,無法支援PHP Telnet '; 中斷;
- }
- }
- }
- ?>
-
-
複製程式碼
|
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
-
2024-10-22 09:46:29
-
2024-10-13 13:53:41
-
2024-10-12 12:15:51
-
2024-10-11 22:47:31
-
2024-10-11 19:36:51
-
2024-10-11 15:50:41
-
2024-10-11 15:07:41
-
2024-10-11 14:21:21
-
2024-10-11 12:59:11
-
2024-10-11 12:17:31