【问题标题】:Run sh file wiith ssh2_exec (PHP)使用 ssh2_exec (PHP) 运行 sh 文件
【发布时间】:2018-01-25 01:50:47
【问题描述】:

我正在尝试使用 ssh2_exec 运行 sh 文件并在完成后获取流数据。

顺便说一句,我正在使用 CentOS。

但是当我使用ssh2_exec('./file.sh'); 时,什么都没有发生..

文件运行完成后是否可以获得执行输出?

【问题讨论】:

标签: php ssh centos sh


【解决方案1】:

根据PHP manual about ssh2_exec的示例

$connection = ssh2_connect('shell.example.com', 22);
ssh2_auth_password($connection, 'username', 'password');
$stream = ssh2_exec($connection, '/usr/local/bin/php -i');

ssh2_exec() 用于将命令传递给 shell 会话/连接。

为了读取流内容,您可以尝试:

echo "Output: " . stream_get_contents($stream);

另一方面,如果您希望在 php/host 服务器上运行命令,您可以尝试使用 shell_exec:

$output = shell_exec('ls -lart');
echo "<pre>$output</pre>";

【讨论】:

    猜你喜欢
    • 2017-11-06
    • 1970-01-01
    • 2013-07-30
    • 2016-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-08
    • 2012-11-24
    相关资源
    最近更新 更多