无法使用phpseclib ssh到服务器

时间:2015-05-30 19:51:11

标签: php ssh phpseclib

这是我upload.php的代码。我的所有文件都在MAMP的htdocs目录中。我可以转到网址http://localhost:8888/webpage.html,所以我假设MAMP正在按预期工作。这是我upload.php的代码。

<?php 
    set_include_path(get_include_path() . PATH_SEPARATOR . 'phpseclib');
    include('Net/SFTP.php');
    include('Net/SSH2.php');
    //Send file via sftp to server
    echo "begin";

    echo "now connecting...";

    $connection = ssh2_connect('servername.com', 22); //server redacted
    echo "server made\n";
    ssh2_auth_password($connection, 'username', 'password'); //username and password redacted
    echo "connected successfuly\n";

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

当我提交表单并将其重定向到upload.php时,upload.php会显示文字

begin now connecting...

所以我假设它没有越过界限

$connection = ssh2_connect('servername.com', 22); //server redacted

但我无法弄清楚原因。

我正在使用mac。我可以使用ssh username@servername.comscp ./filename.txt username@servername.com:~/test/通过终端ssh到服务器和scp文件。显然它会提示我输入密码。

也许我的服务器名称错了?有没有人知道为什么它没有超越那条线?

1 个答案:

答案 0 :(得分:3)

就像你在混合phpseclib API和libssh2 API一样。就像在哪里定义$ ssh变量一样?您将ssh2_connect的结果设置为$ connection,然后使用从未定义过的$ ssh。

无论如何,试试这个:

<?php 
    set_include_path(get_include_path() . PATH_SEPARATOR . 'phpseclib');
    include('Net/SFTP.php');
    include('Net/SSH2.php');
    //Send file via sftp to server
    echo "begin";

    echo "now connecting...";

    $ssh = new Net_SSH2('servername.com', 22); //server redacted
    echo "server made\n";
    $ssh->login('username', 'password'); //username and password redacted
    echo "connected successfuly\n";

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