0

我想知道是否有办法让 php 页面发送 SSH 命令并立即断开连接?喜欢屏幕它,所以它在断开连接后继续?

这可能吗?我的朋友已经做到了,但不会告诉我如何......他说套接字......?

4

2 回答 2

0

重写 freeduck 的答案以使用phpseclib,一个纯 PHP SSH 实现......

<?php
include('Net/SSH2.php');

$ssh = new Net_SSH2('www.domain.tld');
if (!$ssh->login('username', 'password')) {
    exit('Login Failed');
}

echo $ssh->exec('nohup lus > test.txt &');
?>
于 2013-06-10T14:47:49.253 回答
0

您可以使用屏幕,但我建议使用 nohup。如果您使用的是公钥/私钥登录而不是密码,并且没有安全模式限制,那么您可以执行以下操作:

<?php exec("ssh <User>@<Host> 'nohup ls > test.txt &'"); ?>

或者使用PHP ssh2模块

<?php
$connection = ssh2_connect('shell.example.com', 22);
ssh2_auth_password($connection, 'username', 'password');

$stream = ssh2_exec($connection, 'nohup ls > test.txt &');
?>

nohup 和 & 字符仍会立即断开您的连接。

于 2013-06-10T09:20:36.170 回答