使用PHP进行telnet连接 [英] telnet connection using PHP
本文介绍了使用PHP进行telnet连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要编写一个PHP脚本以telnet到路由器,运行命令并获取结果.有谁知道PHP中的telnet连接库?
I need to write a PHP script to telnet to a router, run a command and fetch the results. does anyone know a telnet connection library in PHP?
更新:该请求(很明显)是很久以前的.最后,我不得不编写自己需要的客户端库.该库(以及更多模块)的代码是开源的,可在 github . 谢谢大家的回答.
Update: This request (as is obvious) was for a long time ago. In the end I had to write the client library that I needed myself. The code for this library (and many more modules) is open source and available on github. Thanks everyone for your answers.
推荐答案
使用stdin/stream_select&阻止流为您提供了20行类似于客户端的telnet
using stdin/stream_select & blocking streams gives you a 20 lines telnet like client
<?
$socket = fsockopen("192.168.52.1", 8000);
if(!$socket)return;
stream_set_blocking($socket, 0);
stream_set_blocking(STDIN, 0);
do {
echo "$ ";
$read = array( $socket, STDIN); $write = NULL; $except = NULL;
if(!is_resource($socket)) return;
$num_changed_streams = @stream_select($read, $write, $except, null);
if(feof($socket)) return ;
if($num_changed_streams === 0) continue;
if (false === $num_changed_streams) {
/* Error handling */
var_dump($read);
echo "Continue\n";
die;
} elseif ($num_changed_streams > 0) {
echo "\r";
$data = fread($socket, 4096);
if($data !== "")
echo "<<< $data";
$data2 = fread(STDIN, 4096);
if($data2 !== "") {
echo ">>> $data2";
fwrite($socket, trim($data2));
}
}
} while(true);
这篇关于使用PHP进行telnet连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文