PHP implementation of Telnet function

Source: Internet
Author: User

Use PHP to implement Telnet connection, pass command, get return value and other functions!

telnet.php

  1. Error_reporting (-1);
  2. Class Telnet {
  3. var $sock = NULL;
  4. function Telnet ($host, $port) {
  5. $this->sock = Fsockopen ($host, $port);
  6. Socket_set_timeout ($this->sock,2,0);
  7. }
  8. function Close () {
  9. if ($this->sock) fclose ($this->sock);
  10. $this->sock = NULL;
  11. }
  12. function Write ($buffer) {
  13. $buffer = Str_replace (Chr (255), Chr (255). chr (255), $buffer);
  14. Fwrite ($this->sock, $buffer);
  15. }
  16. function getc () {
  17. Return fgetc ($this->sock);
  18. }
  19. function Read_till ($what) {
  20. $buf = ";
  21. while (1) {
  22. $IAC = Chr (255);
  23. $DONT = Chr (254);
  24. $DO = Chr (253);
  25. $WONT = Chr (252);
  26. $WILL = chr (251);
  27. $theNULL = chr (0);
  28. $c = $this->getc ();
  29. if ($c = = = False) return $buf;
  30. if ($c = = $theNULL) {
  31. Continue
  32. }
  33. if ($c = = "1") {
  34. Continue
  35. }
  36. if ($c! = $IAC) {
  37. $buf. = $c;
  38. if ($what = = (substr ($buf, strlen ($buf)-strlen ($what)))) {
  39. return $buf;
  40. }
  41. else {
  42. Continue
  43. }
  44. }
  45. $c = $this->getc ();
  46. if ($c = = $IAC) {
  47. $buf. = $c;
  48. }
  49. else if (($c = = $DO) | | ($c = = $DONT)) {
  50. $opt = $this->getc ();
  51. echo "We wont". Ord ($opt). " \ n ";
  52. Fwrite ($this->sock, $IAC. $WONT. $opt);
  53. }
  54. ElseIf (($c = = $WILL) | | ($c = = $WONT)) {
  55. $opt = $this->getc ();
  56. echo "We dont". Ord ($opt). " \ n ";
  57. Fwrite ($this->sock, $IAC. $DONT. $opt);
  58. }
  59. else {
  60. echo "Where are we?" C= ". Ord ($c). \ n ";
  61. }
  62. }
  63. }
  64. }
  65. /*
  66. ?? ÷??? ???
  67. $telnet = new Telnet ("192.168.0.1", 23);
  68. echo $telnet->read_till ("Login:");
  69. $telnet->write ("kongxx\r\n");
  70. echo $telnet->read_till ("Password:");
  71. $telnet->write ("kongxx\r\n");
  72. echo $telnet->read_till (":>");
  73. $telnet->write ("ls\r\n");
  74. echo $telnet->read_till (":>");
  75. echo $telnet->close ();
  76. */
Copy Code
PHP, Telnet
  • 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.