如何使用 php 连接 telnet 并发送命令并将输出写入文本文件
我需要远程登录到端口并发送命令并使用 PHP 将输出写入 txt 文件。我该如何操作?
在这个论坛中有一个相同的问题名称 使用 PHP 的 telnet 连接 但他们有一个解决方案链接和解决方案链接未打开,因此我必须再次提出问题。
我还尝试了 php 站点 中的代码,但它没有保存正确的输出到文本文件。代码:
<?php
$fp = fsockopen("localhost", 80, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
$out = "GET / HTTP/1.1\r\n";
$out .= "Host: localhost\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);
while (!feof($fp)) {
echo fgets($fp, 128);
}
fclose($fp);
}
?>
那么,请帮我解决问题。我如何远程登录到本地主机端口 80 并发送命令 GET / HTTP/1.1 并将输出写入文本文件?
i need to telnet to a port and send command and write the output into a txt file using PHP.How i do it?
in this forum have a same question name telnet connection using PHP but their have a solution link and the solution link is not open so i have to make the question again.
Also i try the code below from php site but it does not save the proper output into a text file.Code:
<?php
$fp = fsockopen("localhost", 80, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
$out = "GET / HTTP/1.1\r\n";
$out .= "Host: localhost\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);
while (!feof($fp)) {
echo fgets($fp, 128);
}
fclose($fp);
}
?>
So,please help me to solve the problem.How i telnet to localhost port 80 and send command GET / HTTP/1.1 and write the output into a text file?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当然,通过简单的添加,您的示例脚本就可以将输出写入文件:
话又说回来,我同意 Eduard7 的观点;不手动执行请求并让 PHP 为您解决会更容易:
With a simple additition, your example script can write the output to a file, of course:
Then again, I agree with Eduard7; it's easier not to do the request manually and just let PHP solve it for you:
你真的想用 telnet 来做到这一点吗?怎么样:
或者如果您想自定义请求,您可以使用 cURL - http:// php.net/manual/en/book.curl.php
You really want to do this with telnet? What about:
Or if You want to customize the request, you can use cURL - http://php.net/manual/en/book.curl.php