php telnet功能實例代碼

WBOY
發布: 2016-07-25 09:13:01
原創
992 人瀏覽過

例子,php telnet功能實作程式碼。

  1. class PHPTelnet {

  2. var $show_connect_error=1;
  3. <p> var $use_usleep=0; // 更改為1 以加快執行速度
  4. // 在Windows 伺服器上不要更改為1,除非您有PHP 5
  5. var $sleeptime=125000;
  6. var $loginsleeptime= 1000000;
  7. var $fp=NULL;

  8. var $loginprompt;
  9. var $conn1;

  10. var $conn2;
  11. /*

  12. 0 = 成功
  13. 1 = 無法開啟網路連線
  14. 2 = 未知主機
  15. 3 = 登入失敗
  16. 4 = PHP 版本過低
  17. * /
  18. 函數Connect($server,$user,$pass) {
  19. $rv=0;
  20. $vers=explode('.',PHP_VERSION);
  21. $needvers=array(4, 3,0) ;
  22. $j=count($vers);
  23. $k=count($needvers);
  24. if ($k for ($i= 0;$i if (($vers[$i]+0)>$needvers[$i]) break;
  25. if (($vers[ $i]) +0) $this->ConnectError(4);
  26. return 4;
  27. }
  28. }
  29. $this->Disconnect();
  30. if (strlen($server)) {

  31. if (preg_match('/[^0-9.]/',$server )) {
  32. $ip=gethostbyname($server);
  33. if ( $ip==$server) {
  34. $ip='';
  35. $rv=2;
  36. }
  37. } else $ip=$server;
  38. } else $ip='127.0 .0.1';
  39. if (strlen($ip)) {

  40. if ($this ->fp=fsockopen($ip,23)) {
  41. fputs($this->fp,$this->conn1);
  42. $this->Sleep();
  43. fputs($this->fp,$this->conn2);
  44. $this->Sleep();
  45. $this->GetResponse($r);
  46. $r=explode (" n",$r);
  47. $this->loginprompt=$r[count($r)-1];
  48. fputs($this->fp,"$usern" );

  49. $this->Sleep();
  50. fputs($this->fp,"$passn");

  51. if ($this->use_usleep) usleep( $this->loginsleeptime);
  52. else sleep(1);
  53. $ this->GetResponse($r);
  54. $r=explode("n",$r);
  55. if ( ($r[count($r)-1]=='')|| ($this->loginprompt==$r[count($r)-1])) {
  56. $rv=3;
  57. $this->Disconnect();
  58. }
  59. } else $rv=1;
  60. }
  61. if ($rv) $this->ConnectError($ rv);

  62. return $rv;
  63. }
  64. function Disconnect($exit=1) {

  65. if ($this->fp) {
  66. if ( $exit) $this->DoCommand('exit',$junk);
  67. fclose( $this->fp);
  68. $this->fp=NULL;
  69. }
  70. } p>
  71. function DoCommand($c,&$r) {

  72. if ($this->fp) {
  73. fputs($this->fp,"$cn");
  74. $this->; Sleep();
  75. $this->GetResponse($r);
  76. $r=preg_replace("/^.*?n(.*)n[^n]*$/ ","$1" ,$r);
  77. }
  78. 回傳$this->fp?1:0;
  79. }
  80. function GetResponse(&$r) {

  81. $r='';
  82. do {
  83. $r.=fread($this->fp, 1024);
  84. $s=socket_get_status($ this->fp);
  85. } while ($s['unread_bytes']);
  86. }
  87. function Sleep() {

  88. if ($this->use_usleep) usleep($this->sleeptime) ;
  89. else sleep(1);
  90. }
  91. //telnet功能主函數

  92. function PHPTelnet() {
  93. $this->conn1=chr(0xFF).chr(0xFB).chr(0x1F).chr( 0xFF).chr(0xFB).
  94. chr(0x20).chr(0xFF).chr(0xFB).chr(0x18).chr(0xFF).chr(0xFB).
  95. chr(0x27).chr (0xFF).chr(0xFD)。 ).chr(0xFC).
  96. chr(0x23).chr(0xFF).chr(0xFC).chr(0x24).chr(0xFF).chr(0xFA).
  97. chr(0x1F).chr( 0x00).chr(0x50).chr(0x00).chr(0x18).chr(0xFF).
  98. chr(0xF0).chr(0xFF).chr(0xFA).chr(0x20).chr(0x00) .chr(0x33).
  99. chr(0x38).chr(0x34).chr(0x30).chr(0x30).chr(0x2C).chr(0x33).
  100. chr(0x38). ).chr(0x30).chr( 0x30).chr(0xFF).chr(0xF0).
  101. chr(0xFF).chr(0xFA).chr(0x27).chr(0x00).chr(0xFF)。 chr(0xF0).
  102. chr (0xFF).chr(0xFA).chr(0x18).chr(0x00).chr(0x58).chr(0x54).
  103. chr(0x45).chr(0x52) .chr(0x4D).chr(0xFF ).chr(0xF0);
  104. $this->conn2=chr(0xFF).chr(0xFC).chr(0x01).chr(0xFF).chr(0xFC).
  105. 字元(0x22)。 chr(0xFF).chr(0xFE).chr(0x05).chr(0xFF).chr(0xFC).chr(0x21);
  106. }
  107. function ConnectError($num) {

  108. if ($this->show_connect_error) switch ($num) {
  109. case 1: echo '
    >[PHP Telnet] 連線失敗:無法開啟網路連線
    '; Break;
  110. 情況2: echo '
    [PHP Telnet] 連線失敗:未知主機
    '; Break;
  111. 情況3: echo '
    [PHP Telnet] ;連線失敗:登入失敗
    '; Break;
  112. 情況4: echo '
    [PHP Telnet] 連線失敗:您的伺服器的PHP 版本太低,無法支援PHP Telnet
    '; 中斷;
  113. }
  114. }
  115. }
  116. ?>
複製程式碼

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板