简体   繁体   English

使用phpseclib SSH进入服务器时遇到问题

[英]Having trouble using phpseclib to ssh into a server

This is my code for upload.php . 这是我的upload.php代码。 All my files are in the htdocs directory of MAMP. 我所有的文件都在MAMP的htdocs目录中。 I can go to the url http://localhost:8888/webpage.html so I am assuming MAMP is working as it should. 我可以转到URL http://localhost:8888/webpage.html因此我假设MAMP可以正常工作。 This is my code for upload.php . 这是我的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');
?>

When I submit my form and it gets redirected to upload.php , the upload.php displays text saying 当我提交表单并将其重定向到upload.phpupload.php显示的文本为

begin now connecting...

So I'm assuming it's not getting past the line 所以我假设它没有超出界限

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

But I can't figure out why. 但我不知道为什么。

I am using a mac. 我正在使用Mac。 I can ssh into the server and scp files to it via the terminal using ssh username@servername.com and scp ./filename.txt username@servername.com:~/test/ . 我可以使用ssh username@servername.comscp ./filename.txt username@servername.com:~/test/通过ssh进入服务器并通过终端将文件scp ./filename.txt username@servername.com:~/test/到它。 And obviously it prompts me for the password. 显然,它会提示我输入密码。

Maybe I have the wrong server name? 也许我的服务器名称错误? Does anyone have any idea why it's not getting past that line? 有谁知道为什么它没有超过那条线?

It's like you're mixing the phpseclib API and the libssh2 API. 就像您混用了phpseclib API和libssh2 API。 Like where is the $ssh variable being defined? 就像在哪里定义$ ssh变量? You set the result of ssh2_connect to $connection but then later use $ssh which has never been defined. 您将ssh2_connect的结果设置为$ connection,但随后使用从未定义的$ ssh。

Anyway, try this: 无论如何,请尝试以下操作:

<?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');
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM