尝试连接 sftp 时出错

error when trying to connect with sftp

我正在尝试使用 sftp 建立连接 class

我的代码:

$host = 'some ip address';
$username = 'username';
$pass = 'password';

$sftp = new SFTPConnection($host, 22);
$sftp->login($username, $pass);

当我 运行 这个脚本时,我收到消息 Could not authenticate with username and password

当我使用相同的用户并通过 sftp 传入 fileZilla 时,就可以了,我可以建立连接,但是使用脚本我不能

有人知道问题出在哪里吗?

尝试将此代码添加到您的 sftp.php in login(){} 函数中。然后告诉它给出了什么错误:

if (!extension_loaded('ssh2'))
{
    throw new Exception("Extension ssh2 has to be loaded to use this class. Please enable in php.ini.");
}
if (! @ssh2_auth_password($this->connection, $username, $password))
    throw new Exception("Could not authenticate with username $username " . "and password $password.");
$this->sftp = @ssh2_sftp($this->connection);
if (! $this->sftp)
    throw new Exception("Could not initialize SFTP subsystem.");

编辑您的代码以执行以下操作[正如有些人所说,我们应该通过 username_password 和 hostKeyAuthentication 进行身份验证]
--------------------------------
以上描述表明:
问题在于 php 扩展,假设您要使用密码或 public 密钥进行身份验证。由于该假设,使用密码进行身份验证将使您失败,并可能使您误入歧途(link:Must Read 中有简要说明)。

$connection = ssh2_connect($host, $port);
ssh2_auth_password($connection, $username, $password);
if (ssh2_auth_pubkey_file($connection, $username, $pubkey, $privatekey)) {
    echo "Public Key Authentication Successful";
}
else {
    echo 'Public Key Authentication Failed' ;
    return false;
}

好的,这是什么问题...

FTP 服务器不允许我的 IP 地址?!现在,当他们将我的 IP 列入白名单时,一切正常。

谢谢你们的努力。