php telnet功能实例代码

WBOY
发布: 2016-07-25 09:13:01
原创
990 人浏览过

例子,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. ?>
复制代码


来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板