phpseclib sftp使用私钥和密码连接

Ron*_*aul 3 sftp key phpseclib

无论如何使用phpseclib或任何其他方法将sftp与私钥和ftp密码连接起来.

neu*_*ert 9

SFTP服务器同时使用密码和公钥认证很少见.我的猜测是你最有可能拥有密码保护的私钥.如果是这样,您可以这样登录:

<?php
include('Net/SFTP.php');
include('Crypt/RSA.php');

$sftp = new Net_SFTP('www.domain.tld');
$key = new Crypt_RSA();
$key->setPassword('whatever');
$key->loadKey(file_get_contents('privatekey'));
if (!$sftp->login('username', $key)) {
    exit('Login Failed');
}

print_r($sftp->nlist());
?>
Run Code Online (Sandbox Code Playgroud)

如果你的服务器确实在做两件事,那么下面应该有效:

<?php
include('Net/SFTP.php');
include('Crypt/RSA.php');

$sftp = new Net_SFTP('www.domain.tld');
$key = new Crypt_RSA();
$key->setPassword('whatever');
$key->loadKey(file_get_contents('privatekey'));
if (!$sftp->login('username', $key) && !$sftp->login('username', 'password')) {
    exit('Login Failed');
}

print_r($sftp->nlist());
?>
Run Code Online (Sandbox Code Playgroud)