phpseclib sftp connect with private key and password

前端 未结 2 1342
悲&欢浪女
悲&欢浪女 2021-01-03 10:23

Is there anyway to connect the sftp with both private key and ftp password by using phpseclib or any other method.

2条回答
  •  既然无缘
    2021-01-03 11:09

    It's kinda rare that SFTP servers use both password and publickey authentication. My guess would be that what you most likely have is a password protected private key. If so you can login thusly:

    setPassword('whatever');
    $key->loadKey(file_get_contents('privatekey'));
    if (!$sftp->login('username', $key)) {
        exit('Login Failed');
    }
    
    print_r($sftp->nlist());
    ?>
    

    If indeed your server truly is doing both the following should work:

    setPassword('whatever');
    $key->loadKey(file_get_contents('privatekey'));
    if (!$sftp->login('username', $key) && !$sftp->login('username', 'password')) {
        exit('Login Failed');
    }
    
    print_r($sftp->nlist());
    ?>
    

提交回复
热议问题