php来实现telnet的连接、传递命令、获取返回值等功能!

<?php
02. error_reporting(-1);
03.  
04. class Telnet {
05. var $sock = NULL;
06.  
07. function telnet($host,$port) {
08. $this->sock = fsockopen($host,$port);
09. socket_set_timeout($this->sock,2,0);
10. }
11.  
12. function close() {
13. if ($this->sock)  fclose($this->sock);
14. $this->sock = NULL;
15. }
16.  
17. function write($buffer) {
18. $buffer str_replace(chr(255),chr(255).chr(255),$buffer);
19. fwrite($this->sock,$buffer);
20. }
21.  
22. function getc() {
23. return fgetc($this->sock);
24. }
25.  
26. function read_till($what) {
27. $buf '';
28. while (1) {
29. $IAC chr(255);
30.  
31. $DONT chr(254);
32. $DO chr(253);
33.  
34. $WONT chr(252);
35. $WILL chr(251);
36.  
37. $theNULL chr(0);
38.  
39. $c $this->getc();
40.  
41. if ($c === false) return $buf;
42. if ($c == $theNULL) {
43. continue;
44. }
45.  
46. if ($c == "1") {
47. continue;
48. }
49.  
50. if ($c != $IAC) {
51. $buf .= $c;
52.  
53. if ($what == (substr($buf,strlen($buf)-strlen($what)))) {
54. return $buf;
55. }
56. else {
57. continue;
58. }
59. }
60.  
61. $c $this->getc();
62.  
63. if ($c == $IAC) {
64. $buf .= $c;
65. }
66. else if (($c == $DO) || ($c == $DONT)) {
67. $opt $this->getc();
68. // echo "we wont ".ord($opt)."\n";
69. fwrite($this->sock,$IAC.$WONT.$opt);
70. }
71. elseif (($c == $WILL) || ($c == $WONT)) {
72. $opt $this->getc();
73. // echo "we dont ".ord($opt)."\n";
74. fwrite($this->sock,$IAC.$DONT.$opt);
75. }
76. else {
77. // echo "where are we? c=".ord($c)."\n";
78. }
79. }
80. }
81. }
82.  
83. /*
84. $telnet = new telnet("192.168.0.1",23);
85. echo $telnet->read_till("login: ");
86. $telnet->write("kongxx\r\n");
87. echo $telnet->read_till("password: ");
88. $telnet->write("KONGXX\r\n");
89. echo $telnet->read_till(":> ");
90. $telnet->write("ls\r\n");
91. echo $telnet->read_till(":> ");
92. echo $telnet->close();
93.  
94. */
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值