php telnet 脚本没有响应
我在 Windows XP Professional 上运行 PHP5。我正在尝试编写一个 telnet php 脚本,它可以简单地连接、发送文本字符串并获取响应缓冲区并将其输出。我使用这里的 telnet 类文件:
http://cvs .adfinis.ch/cvs.php/phpStreamcast/telnet.class.php
我在另一个线程中找到了它。
<?php
error_reporting(255);
ini_set('display_errors', true);
echo "1<br>";
require_once("telnet_class.php");
$telnet = new Telnet();
$telnet->set_host("10.10.5.7");
$telnet->set_port("2002");
$telnet->connect();
//$telnet->wait_prompt();
$telnet->write('SNRD 1%0d');
echo "3<br>";
$result = $telnet->get_buffer();
echo $result;
print_r($result);
// flush_now();
echo "4<br>";
$telnet->disconnect();
?>
我没有收到任何类型的错误或响应。如果我发送一个无效的字符串,我至少应该得到一个“ERR”响应,但我什至没有得到它。有什么想法我可能做错了什么吗?如果我从命令提示符执行相同的操作,我会收到所需的字符串输出。难道是因为 write 函数正在发送
I'm running PHP5 on Windows XP Professional. I'm trying to write a telnet php script which simply connects, sends a text string and grabs the response buffer and outputs it. I'm using the telnet class file from here:
http://cvs.adfinis.ch/cvs.php/phpStreamcast/telnet.class.php
which i found in another thread.
<?php
error_reporting(255);
ini_set('display_errors', true);
echo "1<br>";
require_once("telnet_class.php");
$telnet = new Telnet();
$telnet->set_host("10.10.5.7");
$telnet->set_port("2002");
$telnet->connect();
//$telnet->wait_prompt();
$telnet->write('SNRD 1%0d');
echo "3<br>";
$result = $telnet->get_buffer();
echo $result;
print_r($result);
// flush_now();
echo "4<br>";
$telnet->disconnect();
?>
I'm not receiving any kind of errors or response. If I send an invalid string, I should get an 'ERR' response in the least however I don't even get that. Any ideas what i could be doing wrong? If I do the same thing from the command prompt, I receive the string output I need. Could this is because the write function is sending
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在阅读了源代码和标题中提到的原始(法语)站点后......
好吧,解释: get_buffer() 就是这样做的,读取缓冲区中的内容。要在缓冲区中获取某些内容,您必须执行 read_to($match),它将读入缓冲区直至 $match。之后, get_buffer 应该为您提供所需的字符串。
编辑:
如果您找不到您感兴趣的字符串后面的某个字符串,则 read_to 将因 read_to 方法的这一部分而以错误结束(原始法语注释的翻译是我的):这
意味着当套接字在没有匹配的情况下关闭时请求的字符串,将返回 TELNET_ERROR。但是,您要查找的字符串此时应该在缓冲区中......您在 read_to 的参数中放入了什么? “\n” 就像我所做的那样还是只是“”?
编辑2:
get_buffer 也有问题。 IMO 这个类并不是真正的省时工具;-)
它会丢弃响应的最后一行,在您的情况下,包含以下内容的行:
回答。
我建议向类中添加 get_buffer 的“轻量级”版本,如下所示
}
并自己在结果中进行必要的修剪/搜索。
您可能还想添加以下常量
并像这样更改 read_to ,
以便自己处理该特殊情况(在您的情况下,结果代码 TELNET_EOF 不必被视为错误)。所以最后你的代码应该看起来或多或少像这样:
After some reading in the source code and on the original (french) site referred to in the header....
Okay, explanation: get_buffer() does just that, read what's in the buffer. To get something in the buffer you have to execute read_to($match) who will read into buffer up to $match. After that, get_buffer should give you the desired string.
EDIT:
if you cannot find some string that follows the string you are interested in read_to will end in an error due to this part of the read_to method (translation of original french comment is mine):
Meaning that when the socket is closed without a match of the requested string, TELNET_ERROR will be returned. However, the string you're looking for should at that point be in the buffer.... What did you put in read_to's argument? "\n" like what I did or just "" ?
EDIT2 :
there's also a problem with get_buffer. IMO this class is not really a timesaver ;-)
It will throw away the last line of the response, in your case the one that contains the
answer.
I suggest to add a "light" version of get_buffer to the class, like this
}
and do the necessary trimming/searching in the result yourself.
You might also want to add the following constant
and change read_to like this
in order to treat that special case yourself (a result code TELNET_EOF doesn't have to be treated as an error in your case). So finally your code should look more or less like this:
您检查过 telnet 类是如何工作的吗?也许它不是为在 Windows 下运行而设计的。如果您正在谈论的是一个简单的套接字,也许可以考虑使用常规的套接字连接。
https://www.php.net/sockets
如果你打开了一个你没有打开的套接字close,只要脚本正在运行,您就应该在 netstat 中看到一个条目。
netstat -na|查找“:2002”
Have you checked how the telnet class works? Maybe it wasn't designed to run under windows. If it's a simple socket that you're speaking to, maybe consider using a regular socket-connection instead.
https://www.php.net/sockets
If you open up a socket which you don't close, you should se an entry in netstat as long as your script is running.
netstat -na|find ":2002"
您不能像这样插入十六进制值。远程进程刚刚看到
,现在正在等待线路终止。试试这个
或
双引号非常关键,请参阅 此处
编辑:
您可能会尝试添加一些错误报告,因为现在您实际上并没有检查太多(error_reporting 不会显示有关 telnet 类中的错误的任何内容)....例如:
另外,您确定需要 \r\n 行终止吗? write 被定义为
并且
?
另外,您是否使用命令行 telnet 客户端测试了主机和端口?喜欢
?
You can't insert hex values like that. The remote process just sees
and now it's waiting for the line to be terminated. Try this
or
The double quotes are quite critical, see here
EDIT:
you might try adding some error reporting as right now you don't really check much (error_reporting won't show anything on the errors in the telnet class).... For example:
also, are you sure you need \r\n line termination? write is defined as
and does
?
Also, did you test the host and port with the command line telnet client? Like
?