php telnet功能实现代码

php中文网
发布: 2016-07-25 09:13:02
原创
1764人浏览过

本节内容: php telnet功能实例

文件:telnet.php

  1. error_reporting(-1);

  2. //telnet功能类

  3. class Telnet {
  4. var $sock = NULL;
  5. function telnet($host,$port) {
  6. $this->sock = fsockopen($host,$port);
  7. socket_set_timeout($this->sock,2,0);
  8. }
  9. function close() {

  10. if ($this->sock) fclose($this->sock);
  11. $this->sock = NULL;
  12. }
  13. function write($buffer) {
  14. $buffer = str_replace(chr(255),chr(255).chr(255),$buffer);
  15. fwrite($this->sock,$buffer);
  16. }
  17. function getc() {
  18. return fgetc($this->sock);
  19. }
  20. function read_till($what) {

  21. $buf = '';
  22. while (1) {
  23. $IAC = chr(255);
  24. $DONT = chr(254);
  25. $DO = chr(253);
  26. $WONT = chr(252);
  27. $WILL = chr(251);
  28. $theNULL = chr(0);
  29. $c = $this->getc();
  30. if ($c === false) return $buf;
  31. if ($c == $theNULL) {
  32. continue;
  33. }
  34. if ($c == "1") {
  35. continue;
  36. }
  37. if ($c != $IAC) {

    代码小浣熊
    代码小浣熊

    代码小浣熊是基于商汤大语言模型的软件智能研发助手,覆盖软件需求分析、架构设计、代码编写、软件测试等环节

    代码小浣熊 51
    查看详情 代码小浣熊
  38. $buf .= $c;
  39. if ($what == (substr($buf,strlen($buf)-strlen($what)))) {
  40. return $buf;
  41. }
  42. else {
  43. continue;
  44. }
  45. } // bbs.it-home.org

    立即学习PHP免费学习笔记(深入)”;

  46. $c = $this->getc();

  47. if ($c == $IAC) {
  48. $buf .= $c;
  49. }
  50. else if (($c == $DO) || ($c == $DONT)) {
  51. $opt = $this->getc();
  52. // echo "we wont ".ord($opt)."\n";
  53. fwrite($this->sock,$IAC.$WONT.$opt);
  54. }
  55. elseif (($c == $WILL) || ($c == $WONT)) {
  56. $opt = $this->getc();
  57. // echo "we dont ".ord($opt)."\n";
  58. fwrite($this->sock,$IAC.$DONT.$opt);
  59. }
  60. else {
  61. // echo "where are we? c=".ord($c)."\n";
  62. }
  63. }
  64. }
  65. }
  66. /*

  67. 使用方法
  68. telnet类的调用
  69. $telnet = new telnet("192.168.0.1",23);
  70. echo $telnet->read_till("login: ");
  71. $telnet->write("kongxx\r\n");
  72. echo $telnet->read_till("password: ");
  73. $telnet->write("KONGXX\r\n");
  74. echo $telnet->read_till(":> ");
  75. $telnet->write("ls\r\n");
  76. echo $telnet->read_till(":> ");
  77. echo $telnet->close();
  78. */
复制代码


PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号