2010-02-28 169 views
0

我目前使用的是PHP2以上的SSH2。这是我第一次这样做,我希望我能够在这方面得到一些坚实的反馈。PHP2上的SSH2,Echo返回结果

本质上,我试图做的是向服务器(通过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的终端上这样做。最后,我知道它是身份验证,因为在上面的当前代码中,它回显“okay:logged in”。但现在,这就是我所得到的 - 其他一切都是空白的。

任何帮助,这将是伟大的!

BTW,我尝试使用方法,如下所示:http://kevin.vanzonneveld.net/techblog/article/make_ssh_connections_with_php/

+0

这可能是一个终端类型的问题。查看常量SSH2_DEFAULT_TERMINAL设置为什么。 – barrycarter 2010-05-31 22:24:22

回答