如何使用PHP单独运行2个SSH命令?

时间:2015-07-10 16:12:29

标签: php ssh

我正在尝试通过PHP使用SSH连接来运行Bash脚本。

我编写了一个脚本来为MySQL数据库进行备份和恢复,我仍在测试实现这一目标。我在尝试时遇到了一个问题 运行两个不同的简单命令。我的代码是:

<?php
if (!function_exists("ssh2_connect")) die("function ssh2_connect doesn't exist");

if(!($con = ssh2_connect("server.hosting.com", 22))){
    echo "fail: unable to establish connection\n";
} else {

    if(!ssh2_auth_password($con, "username", "password")) {
        echo "fail: unable to authenticate\n";
    } else {
        // allright, we're in!
        echo "okay: logged in...\n";




        if (!($stream = ssh2_exec($con, "cd directory " ))) {
            echo "fail: unable to execute command\n";
        } else {
            // collect returning data from command
            stream_set_blocking($stream, true);
            $data = "";
            while ($buf = fread($stream,4096)) {
                $data .= $buf;
            }
            fclose($stream);
        }
if (!($stream = ssh2_exec($con, "mkdir directiry2" ))) {
            echo "fail: unable to execute command\n";
        } else {
            // collect returning data from command
            stream_set_blocking($stream, true);
            $data = "";
            while ($buf = fread($stream,4096)) {
                $data .= $buf;
            }
            fclose($stream);
        }
    }
}
?>

它仍会创建另一个目录,但不在“目录”中! 请帮忙!!!

0 个答案:

没有答案