欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php telnet功能实例代码

程序员文章站 2022-03-26 16:22:11
...

例子,php telnet功能实现代码。

  1. class PHPTelnet {

  2. var $show_connect_error=1;
  3. var $use_usleep=0; // change to 1 for faster execution

  4. // don't change to 1 on Windows servers unless you have 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 = success
  13. 1 = couldn't open network connection
  14. 2 = unknown host
  15. 3 = login failed
  16. 4 = PHP version too low
  17. */
  18. function 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,"$user\n");

  49. $this->Sleep();
  50. fputs($this->fp,"$pass\n");

  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. }
  71. function DoCommand($c,&$r) {

  72. if ($this->fp) {
  73. fputs($this->fp,"$c\n");
  74. $this->Sleep();
  75. $this->GetResponse($r);
  76. $r=preg_replace("/^.*?\n(.*)\n[^\n]*$/","$1",$r);
  77. }
  78. return $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(0x01).chr(0xFF).chr(0xFB).
  96. chr(0x03).chr(0xFF).chr(0xFD).chr(0x03).chr(0xFF).chr(0xFC).
  97. chr(0x23).chr(0xFF).chr(0xFC).chr(0x24).chr(0xFF).chr(0xFA).
  98. chr(0x1F).chr(0x00).chr(0x50).chr(0x00).chr(0x18).chr(0xFF).
  99. chr(0xF0).chr(0xFF).chr(0xFA).chr(0x20).chr(0x00).chr(0x33).
  100. chr(0x38).chr(0x34).chr(0x30).chr(0x30).chr(0x2C).chr(0x33).
  101. chr(0x38).chr(0x34).chr(0x30).chr(0x30).chr(0xFF).chr(0xF0).
  102. chr(0xFF).chr(0xFA).chr(0x27).chr(0x00).chr(0xFF).chr(0xF0).
  103. chr(0xFF).chr(0xFA).chr(0x18).chr(0x00).chr(0x58).chr(0x54).
  104. chr(0x45).chr(0x52).chr(0x4D).chr(0xFF).chr(0xF0);
  105. $this->conn2=chr(0xFF).chr(0xFC).chr(0x01).chr(0xFF).chr(0xFC).
  106. chr(0x22).chr(0xFF).chr(0xFE).chr(0x05).chr(0xFF).chr(0xFC).chr(0x21);
  107. }
  108. function ConnectError($num) {

  109. if ($this->show_connect_error) switch ($num) {
  110. case 1: echo '
    [PHP Telnet] Connect failed: Unable to open network connection
    '; break;
  111. case 2: echo '
    [PHP Telnet] Connect failed: Unknown host
    '; break;
  112. case 3: echo '
    [PHP Telnet] Connect failed: Login failed
    '; break;
  113. case 4: echo '
    [PHP Telnet] Connect failed: Your server\'s PHP version is too low for PHP Telnet
    '; break;
  114. }
  115. }
  116. }
  117. ?>
复制代码