php來實現telnet的串連、傳遞命令、擷取傳回值等功能!

來源:互聯網
上載者:User

php來實現telnet的串連、傳遞命令、擷取傳回值等功能!

  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. $telnet = new telnet("192.168.0.1",23);
  67. echo $telnet->read_till("login: ");
  68. $telnet->write("kongxx\r\n");
  69. echo $telnet->read_till("password: ");
  70. $telnet->write("KONGXX\r\n");
  71. echo $telnet->read_till(":> ");
  72. $telnet->write("ls\r\n");
  73. echo $telnet->read_till(":> ");
  74. echo $telnet->close();
  75. */
複製代碼
等功能, 來實現, 傳回值
  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.