ホームページ  >  記事  >  バックエンド開発  >  PHPはTelnet機能を実装します

PHPはTelnet機能を実装します

WBOY
WBOYオリジナル
2016-07-25 08:42:051866ブラウズ

php を使用して、telnet の接続、転送コマンド、获取返し值などの機能を実行します!

telnet.php

    error_reporting(-1);
  1. class Telnet {
  2. var $sock = NULL;
  3. function telnet($host,$port) {
  4. $this->sock = fsockopen($ host,$port);
  5. socket_set_timeout($this->sock,2,0);
  6. }
  7. function close() {
  8. if ($this->sock) fclose($this->sock);
  9. $this->sock = NULL;
  10. }
  11. function write($buffer) {
  12. $buffer = str_replace(chr(255),chr(255).chr(255),$buffer);
  13. fwrite($ this->sock,$buffer);
  14. }
  15. function getc() {
  16. return fgetc($this->sock);
  17. }
  18. 関数 read_till($what) {
  19. $buf = '';
  20. while (1) {
  21. $IAC = chr(255);
  22. $DONT = chr(254);
  23. $DO = chr(253) );
  24. $WONT = chr(252);
  25. $WILL = chr(251);
  26. $theNULL = chr(0);
  27. $c = $this->getc();
  28. if ($ c === false) return $buf;
  29. if ($c == $theNULL) {
  30. continue;
  31. }
  32. if ($c == "1") {
  33. continue;
  34. }
  35. if ($c != $IAC) {
  36. $buf .= $c;
  37. if ($what == (substr($buf,strlen($buf)-strlen($what)))) {
  38. return $buf;
  39. }
  40. else {
  41. continue;
  42. }
  43. }
  44. $c = $this->getc();
  45. if ($c == $IAC) {
  46. $buf .= $c;
  47. }
  48. else if ( ($c == $DO) || ($c == $DONT)) {
  49. $opt = $this->getc();
  50. // echo ".ord($opt)."n ";
  51. fwrite($this->sock,$IAC.$WONT.$opt);
  52. }
  53. elseif (($c == $WILL) || ($c == $WONT)) {
  54. $opt = $this->getc();
  55. // echo "".ord($opt)."n";
  56. fwrite($this->sock,$IAC.$DONT.$opt);
  57. }
  58. else {
  59. // echo "ここはどこ? c=".ord($c)."n";
  60. }
  61. }
  62. }
  63. }
  64. /*
  65. ??÷??? ???
  66. $telnet = new telnet("192.168.0.1",23);
  67. echo $telnet->read_till("login: ");
  68. $telnet->write("kongxxrn");
  69. echo $ telnet->read_till("password: ");
  70. $telnet->write("KONGXXrn");
  71. echo $telnet->read_till(":> ");
  72. $telnet->write(" lsrn");
  73. echo $telnet->read_till(":> ");
  74. echo $telnet->close();
  75. */
复制代
PHP、Telnet

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。