在 PHP 中使用 SSH2_Shell 等待命令完成

发布于 2024-11-02 06:29:36 字数 941 浏览 1 评论 0原文

我有一个在 PHP 中工作的 SSH2_Shell 会话。我的问题是,我需要一个命令才能完全完成,然后才能继续执行下一个命令。到目前为止,这是我的代码:

    $command_capture = "cd /mnt/NADS/scripts/";
    $command_capture2 = "./tcpdump.sh $capture_name $sleep";


    if (!($connection = ssh2_connect("172.20.1.18", 22))) {
        echo "fail: unable to establish connection";
    } 
    if (!ssh2_auth_password($connection, "root", "Hideandseek")) {
        echo "fail: unable to authenticate";
    }
    $stream = ssh2_shell($connection);

    fwrite($stream, $command_capture. PHP_EOL);
    sleep(1);
    fwrite($stream, $command_capture2 . PHP_EOL);
    sleep(5);
    $data="";
    while($buf = stream_get_contents($stream)){
        $data.=$buf;
    }
    echo $data;
    fclose($stream);

tcpdump.sh 脚本正在运行 lftp 命令,但没有足够的时间来完成。我无法使用睡眠,因为它可能需要比指定时间更长的时间,并且如果只需要几秒钟,我不想让用户等待。我没有像我那样幸运地实现stream_set_blocking,它似乎冻结了我的浏览器。

总的来说,我需要一种方法来检测命令何时完成并进入下一个命令。

提前致谢!

I have an SSH2_Shell session working in PHP. my issue is that i need a command to completely finish before moving onto the next command. Here is my code so far:

    $command_capture = "cd /mnt/NADS/scripts/";
    $command_capture2 = "./tcpdump.sh $capture_name $sleep";


    if (!($connection = ssh2_connect("172.20.1.18", 22))) {
        echo "fail: unable to establish connection";
    } 
    if (!ssh2_auth_password($connection, "root", "Hideandseek")) {
        echo "fail: unable to authenticate";
    }
    $stream = ssh2_shell($connection);

    fwrite($stream, $command_capture. PHP_EOL);
    sleep(1);
    fwrite($stream, $command_capture2 . PHP_EOL);
    sleep(5);
    $data="";
    while($buf = stream_get_contents($stream)){
        $data.=$buf;
    }
    echo $data;
    fclose($stream);

the tcpdump.sh script is running a lftp command but is not being given anough time to complete. I cant use sleep as it may take longer then the specified time and i dont want to make the user wait if it only needs a few seconds. I have not had luck implementing stream_set_blocking as when i do, it seems to freeze up my browser.

overall, I need a way to detect when a command has finished and move into the next command.

Thanks in advance!

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

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

发布评论

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

评论(3

愁杀 2024-11-09 06:29:36

感谢您的想法,但我想我已经解决了。命令完成后,我对一些特殊字符进行了回显,然后使用 strpos() 搜索这些字符。我认为可能有帮助的另一件事是调整 php.ini 文件中的 max_execution_time 设置。我在这里找到了答案:

http://verysimple.com/2006/03/30/fatal-error-maximum-execution-time-of-30-seconds-exceeded/

这是我的新代码

    $data="";
    while (true){
        $data .= stream_get_contents($stream);
        if (strpos($data,"XOXO") !== false) {
            echo "okay: command finished\n";
            break;
        }
    }
    echo $data;
    fclose($stream);

Thanks for the ideas but I think I got it solved. I did an echo of a few special characters when the command finished and then I would search for the characters using strpos(). Another thing I think may have helped was adjusting the max_execution_time setting in the php.ini file. I found that answer here:

http://verysimple.com/2006/03/30/fatal-error-maximum-execution-time-of-30-seconds-exceeded/

And here is my new code

    $data="";
    while (true){
        $data .= stream_get_contents($stream);
        if (strpos($data,"XOXO") !== false) {
            echo "okay: command finished\n";
            break;
        }
    }
    echo $data;
    fclose($stream);
为你拒绝所有暧昧 2024-11-09 06:29:36

我有一个在 github 上实现 Secure Shell2 的 SSH2 类。查看 https://github。 com/bubba-h57/PHP-SSH2 看看是否有帮助。

不过,我仍在继续寻找更好的方法来处理查找提示,因此任何建议或贡献都将受到欢迎。希望它能有所帮助,哪怕只是给你一些想法。

I have an SSH2 class that implements the Secure Shell2 over on github .. check out https://github.com/bubba-h57/PHP-SSH2 and see if that helps.

I continue to search for a better way to handle finding the prompts though, so any advice or contribution to that would be welcomed. Hopefully it will help, if nothing more than giving you some ideas.

眼眸里的快感 2024-11-09 06:29:36

您可以使用 phpseclib,一个纯 PHP SSH 实现。例如。

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

$ssh = new Net_SSH2();
$ssh->login('username', 'password');

echo $ssh->exec('command');
echo $ssh->exec('command2');
?>

如果您确实需要 shell 支持,您可以执行以下操作(使用最新的 SVN):

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

$ssh = new Net_SSH2();
$ssh->login('username', 'password');

$ssh->read('[prompt]');
$ssh->write("command\n");
echo $ssh->read('[prompt]');
$ssh->write("command\n");
echo $ssh->read('[prompt]');
?>

You could use phpseclib, a pure PHP SSH implementation. eg.

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

$ssh = new Net_SSH2();
$ssh->login('username', 'password');

echo $ssh->exec('command');
echo $ssh->exec('command2');
?>

If you really need shell support you could do the following (with the latest SVN):

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

$ssh = new Net_SSH2();
$ssh->login('username', 'password');

$ssh->read('[prompt]');
$ssh->write("command\n");
echo $ssh->read('[prompt]');
$ssh->write("command\n");
echo $ssh->read('[prompt]');
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文