PHP Telnet Feature Instance code

Source: Internet
Author: User

example, the PHP telnet feature implements the 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 the 1 on the Windows servers unless you have PHP 5
  5. var $sleeptime = 125000;
  6. var $loginsleeptime = 1000000;

  7. var $fp =null;

  8. var $loginprompt;

  9. var $conn 1;

  10. var $conn 2;

  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, "$user \ n");

  52. $this->sleep ();

  53. Fputs ($this->fp, "$pass \ n");

  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, "$c \ n");
  77. $this->sleep ();
  78. $this->getresponse ($R);
  79. $r =preg_replace ("/^.*?\n (. *) \n[^\n]*$/", "$", $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 function Main function

  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. ?>

Copy Code
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.