共享主机上ssh2_connect的解决方法

时间:2012-10-23 13:45:24

标签: php ssh

我正在尝试连接以与PHP建立SSH连接我的托管公司说我们不允许在共享服务器上进行SSH连接..所以有任何解决方法,我可以用来连接的其他一些功能。

 <?php


    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("server5500.asd.com", 22))){
        echo "fail: unable to establish connection\n";
    } else {
        // try to authenticate with username root, password secretpassword
        if(!ssh2_auth_password($con, "asd", "gh45@asd")) {
            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;
                }
                fclose($stream);
            }
        }
    }
    ?>

1 个答案:

答案 0 :(得分:4)

试试phpseclib, a pure PHP SSH implementation。例如

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

$ssh = new Net_SSH2("server5500.asd.com");
if (!$ssh->login("asd", "gh45@asd")) {
    exit('Login Failed');
}

echo $ssh->exec('ls -la');
?>