使用 PHP 进行 telnet 连接

发布于 2024-07-22 01:38:06 字数 165 浏览 6 评论 0原文

我需要编写一个 PHP 脚本来远程登录到路由器,运行命令并获取结果。 有谁知道 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?

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

少跟Wǒ拽 2024-07-29 01:38:06

Nicholas Hall 的 Github 上有一个可爱的类可用于 PHP telnet 连接:
https://github.com/ngharo/Random-PHP -Classes/blob/master/Telnet.class.php

There is a lovely class available for PHP telnet connectivity on Nicholas Hall's Github:
https://github.com/ngharo/Random-PHP-Classes/blob/master/Telnet.class.php

宣告ˉ结束 2024-07-29 01:38:06

使用 stdin/stream_select & 阻塞流给你一个像客户端一样的 20 行 telnet

<?

$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);

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);
左岸枫 2024-07-29 01:38:06

Pear::Net_Socket: http://pear.php.net/package/Net_Socket
为简单的 PHP telnet 机器人或会话扩展此类。

Pear::Net_Socket: http://pear.php.net/package/Net_Socket
Extend this class for a simple PHP telnet bot or session.

追风人 2024-07-29 01:38:06
<?php

$file = 'somefile.txt';
$remote_file = 'readme.txt';
// set up basic connection
$ftp_server = '127.0.0.1';
$ftp_user_name = 'Till';
$ftp_user_pass = 'Kcp05';
$conn_id = ftp_connect($ftp_server);
// login with username and password
ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// upload a file
ftp_nb_put($conn_id, $remote_file, $file, FTP_ASCII);
// close the connection
echo "$file sent to $ftp_server as $remote_file\n<br/>";
ftp_close($conn_id);


// finished copying the input.dat to the till now, just need to execute the print command.
// That will copy somefile.txt in the same folder as this .php file to the ftp server root dir.


$header1=chr(0xFF).chr(0xFB).chr(0x1F).chr(0xFF).chr(0xFB).chr(0x20).chr(0xFF).chr(0xFB).chr(0x18).chr(0xFF).chr(0xFB).chr(0x27).chr(0xFF).chr(0xFD).chr(0x01).chr(0xFF).chr(0xFB).chr(0x03).chr(0xFF).chr(0xFD).chr(0x03).chr(0xFF).chr(0xFC).chr(0x23).chr(0xFF).chr(0xFC).chr(0x24).chr(0xFF).chr(0xFA).chr(0x1F).chr(0x00).chr(0x50).chr(0x00).chr(0x18).chr(0xFF).chr(0xF0).chr(0xFF).chr(0xFA).chr(0x20).chr(0x00).chr(0x33).chr(0x38).chr(0x34).chr(0x30).chr(0x30).chr(0x2C).chr(0x33).chr(0x38).chr(0x34).chr(0x30).chr(0x30).chr(0xFF).chr(0xF0).chr(0xFF).chr(0xFA).chr(0x27).chr(0x00).chr(0xFF).chr(0xF0).chr(0xFF).chr(0xFA).chr(0x18).chr(0x00).chr(0x58).chr(0x54).chr(0x45).chr(0x52).chr(0x4D).chr(0xFF).chr(0xF0);

$fp=pfsockopen("127.0.0.1",23);

echo "Telnet session opening ...";

sleep(4);

fputs($fp,$header1); 
sleep(4); 

fputs($fp,"Till\r");
sleep(2); 
fputs($fp,"Kcp05\r"); 

sleep(2);
fputs($fp,"notepad\r"); 

sleep(3);

echo "Telnet session closing ...";

fclose($fp);

?> 

这对我有用。 第一部分将 ftp 文件上传到服务器,第二部分将登录到 telnet 服务器,并执行一个可以使用您刚刚通过 ftp 上传的文件的程序。 刚刚测试过。

<?php

$file = 'somefile.txt';
$remote_file = 'readme.txt';
// set up basic connection
$ftp_server = '127.0.0.1';
$ftp_user_name = 'Till';
$ftp_user_pass = 'Kcp05';
$conn_id = ftp_connect($ftp_server);
// login with username and password
ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// upload a file
ftp_nb_put($conn_id, $remote_file, $file, FTP_ASCII);
// close the connection
echo "$file sent to $ftp_server as $remote_file\n<br/>";
ftp_close($conn_id);


// finished copying the input.dat to the till now, just need to execute the print command.
// That will copy somefile.txt in the same folder as this .php file to the ftp server root dir.


$header1=chr(0xFF).chr(0xFB).chr(0x1F).chr(0xFF).chr(0xFB).chr(0x20).chr(0xFF).chr(0xFB).chr(0x18).chr(0xFF).chr(0xFB).chr(0x27).chr(0xFF).chr(0xFD).chr(0x01).chr(0xFF).chr(0xFB).chr(0x03).chr(0xFF).chr(0xFD).chr(0x03).chr(0xFF).chr(0xFC).chr(0x23).chr(0xFF).chr(0xFC).chr(0x24).chr(0xFF).chr(0xFA).chr(0x1F).chr(0x00).chr(0x50).chr(0x00).chr(0x18).chr(0xFF).chr(0xF0).chr(0xFF).chr(0xFA).chr(0x20).chr(0x00).chr(0x33).chr(0x38).chr(0x34).chr(0x30).chr(0x30).chr(0x2C).chr(0x33).chr(0x38).chr(0x34).chr(0x30).chr(0x30).chr(0xFF).chr(0xF0).chr(0xFF).chr(0xFA).chr(0x27).chr(0x00).chr(0xFF).chr(0xF0).chr(0xFF).chr(0xFA).chr(0x18).chr(0x00).chr(0x58).chr(0x54).chr(0x45).chr(0x52).chr(0x4D).chr(0xFF).chr(0xF0);

$fp=pfsockopen("127.0.0.1",23);

echo "Telnet session opening ...";

sleep(4);

fputs($fp,$header1); 
sleep(4); 

fputs($fp,"Till\r");
sleep(2); 
fputs($fp,"Kcp05\r"); 

sleep(2);
fputs($fp,"notepad\r"); 

sleep(3);

echo "Telnet session closing ...";

fclose($fp);

?> 

that worked for me. the first part will upload the ftp file to the server, and the second part will lo onto the telnet server, and execute a program that can use the file you just uploaded by ftp. tested it just now.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文