phpseclib connect to localhost using sftp - ssh

2019-07-31 11:46发布

问题:

I'm working with phpseclib (https://github.com/phpseclib/phpseclib) and I come across a problem that I don't know how to solve.

So basically I would like to use PHP to connect to the same server that the script is running on via SSH to perform some SFTP transaction.

I tried ssh localhost on command line and it logs me in using my SSH key-pair.

I implemented phpseclib to download/upload files from/to a remote server. Now I am writing unit tests for the code I've written. However, I cannot mock SFTP class since the object needs to be initiated for every connection, is there a way to test the function that I've written, basically just a file download and process data function.

回答1:

My guess: your ~/.ssh/id_rsa file is the private key corresponding to the public key in ~/.ssh/authorized_keys. So when you try to login with ssh localhost it isn't actually logging you without authentication - it's logging you in with a public key but you just don't realize it.

A few other less likely possibilities:

  1. You have ssh-agent installed and are using that without realizing it.
  2. You're authentication with host-based authentication (which phpseclib doesn't support)

Doing ssh -v localhost and then posting what it says might be helpful.