开发者

SSH2 over PHP, Echo Back Results

I'm currently using SSH2 over PHP. This is my first time doing so, and I'm hoping I might be able to get some solid feedback on this.

Essentially, what I am attempting to do is authenticate to a server (via SSH), run a command, and then echo back the results of that command.

Here's what I am currently using:

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 passwo开发者_高级运维rd
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);
    }
}
}
 ?>

Right now, just for the sake of testing, I am trying to return (echo) back the command "ls -al"

I know this command works, because I am able to do so in Terminal on Ubuntu. Finally, I know that it's authenticating, because, with the current code above, it echos back "okay: logged in". But right now, that's all I am getting-- everything else is blank.

Any help on this would be great!

BTW, I'm attempting to use the method as indicated here: http://kevin.vanzonneveld.net/techblog/article/make_ssh_connections_with_php/


ssh2_exec() frequently returns prematurely. The only real solution I've found is to use phpseclib - a pure PHP SSH2 implementation:

http://phpseclib.sourceforge.net/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜