Home > Backend Development > PHP Tutorial > php telnet function example code

php telnet function example code

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-25 09:13:01
Original
1051 people have browsed it

Example, php telnet function implementation code.

  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<$j) $j=$k;
  25. for ($i=0;$i<$j;$i++) {
  26. if (($vers[$i]+0)>$needvers[$i]) break;
  27. if (($vers[$i]+0)<$needvers[$i]) {
  28. $this->ConnectError(4);
  29. return 4;
  30. }
  31. }

  32. $this->Disconnect();

  33. if (strlen($server)) {

  34. if (preg_match('/[^0-9.]/',$server)) {
  35. $ip=gethostbyname($server);
  36. if ($ip==$server) {
  37. $ip='';
  38. $rv=2;
  39. }
  40. } else $ip=$server;
  41. } else $ip='127.0.0.1';

  42. if (strlen($ip)) {

  43. if ($this->fp=fsockopen($ip,23)) {
  44. fputs($this->fp,$this->conn1);
  45. $this->Sleep();

  46. fputs($this->fp,$this->conn2);

  47. $this->Sleep();
  48. $this->GetResponse($r);
  49. $r=explode("n",$r);
  50. $this->loginprompt=$r[count($r)-1];

  51. fputs($this->fp,"$usern");

  52. $this->Sleep();

  53. fputs($this->fp,"$passn");

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

  64. if ($rv) $this->ConnectError($rv);

  65. return $rv;
  66. }

  67. function Disconnect($exit=1) {

  68. if ($this->fp) {
  69. if ($exit) $this->DoCommand('exit',$junk);
  70. fclose($this->fp);
  71. $this->fp=NULL;
  72. }
  73. }

  74. function DoCommand($c,&$r) {

  75. if ($this->fp) {
  76. fputs($this->fp,"$cn");
  77. $this->Sleep();
  78. $this->GetResponse($r);
  79. $r=preg_replace("/^.*?n(.*)n[^n]*$/","$1",$r);
  80. }
  81. return $this->fp?1:0;
  82. }

  83. function GetResponse(&$r) {

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

  90. function Sleep() {

  91. if ($this->use_usleep) usleep($this->sleeptime);
  92. else sleep(1);
  93. }

  94. //telnet功能主函数

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

  111. function ConnectError($num) {

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

复制代码


Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template