【问题标题】:SSH2 over PHP, Echo Back ResultsSSH2 over PHP,回显结果
【发布时间】:2010-09-13 22:38:40
【问题描述】:

我目前在 PHP 上使用 SSH2。这是我第一次这样做,我希望我能得到一些可靠的反馈。

基本上,我试图做的是向服务器进行身份验证(通过 SSH),运行命令,然后回显该命令的结果。

这是我目前使用的:

if (!function_exists("ssh2_connect")) die("function ssh2_connect doesn't exist");
// log in at server1.example.com on port 22
if(!($con = ssh2_connect("myserver.com", 22))){
echo "fail: unable to establish connection\n";
} else {
// try to authenticate with username and password
if(!ssh2_auth_password($con, "myusername", "mypassword")) {
    echo "fail: unable to authenticate\n";
} else {
    // allright, we're in!
    echo "okay: logged in...\n";

    // execute a command
    if(!($stream = ssh2_exec($con, "ls -al" )) ){
        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;
   echo $data;
        }
        fclose($stream);
    }
}
}
 ?>

现在,只是为了测试,我正在尝试返回(回显)命令“ls -al”

我知道这个命令有效,因为我可以在 Ubuntu 的终端中这样做。最后,我知道它正在进行身份验证,因为使用上面的当前代码,它会回显“好的:已登录”。但现在,这就是我所得到的——其他一切都是空白的。

在这方面的任何帮助都会很棒!

顺便说一句,我正在尝试使用此处指示的方法:http://kevin.vanzonneveld.net/techblog/article/make_ssh_connections_with_php/

【问题讨论】:

  • 这可能是终端类型问题。查看常量 SSH2_DEFAULT_TERMINAL 设置为什么。

标签: php ssh


【解决方案1】:

ssh2_exec() 经常过早返回。我发现的唯一真正的解决方案是使用 phpseclib - 一个纯 PHP SSH2 实现:

http://phpseclib.sourceforge.net/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-04
    • 1970-01-01
    • 1970-01-01
    • 2013-07-16
    • 2016-06-22
    相关资源
    最近更新 更多