2

I have been tasked with connecting to a windows based SFTP server that requires a key AND password to connect to it.

I have worked out how to connect with a password or Key, but I can't work out how to do it with both.

Connect via Key:

//connect to server
$connection = ssh2_connect($SSH_ipadd, 22, array('hostkey'=>'ssh-rsa,ssh-dss'));
if(ssh2_auth_pubkey_file($connection, $SSH_user, $SSH_public_key, $SSH_private_key)){

    echo "connected successfully.";

}else{

    echo "did not connect correctly.";

}

Connect with user/pass:

//connect to server via ssh in preperation for sftp
$connection = ssh2_connect($SSH_ipadd, 22);

//check connectivity
if(ssh2_auth_password($connection, $SSH_user, SSH_Password)){

        echo "connected successfully.";

    }else{

        echo "did not connect correctly.";

    }

But i am unsure how to do both at once.

When i attempt to connect to the server with the CLI

ssh -i private.key USER@ip.address.com

It prompts me, after authenticating the key, for a password. Once this has been authenticated it then logs me into the SFTP server.

How would I use PHP to authenticate against this type of service and then upload files etc.?

Mike
  • 511
  • 3
  • 10
  • 28

1 Answers1

0

phpseclib can do it. Here's an example using v1.0:

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

$rsa = new Crypt_RSA();
$rsa->loadKey(file_get_contents('...'));

$ssh = new Net_SSH2('www.domain.tld');
if (!$ssh->login('username', 'password', $rsa)) {
    exit('Login Failed');
}

echo $ssh->exec('pwd');
echo $ssh->exec('ls -la');
?>
neubert
  • 15,947
  • 24
  • 120
  • 212