PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Sat Jun 06, 2020 6:05 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 1 post ] 
Author Message
PostPosted: Sun Aug 14, 2011 12:12 am 
Offline
Forum Newbie

Joined: Sun Aug 14, 2011 12:09 am
Posts: 1
I need to set up SFTP using a public key without a password from within PHP on Ubuntu, but I can't get the script to work.

To make sure that sftp is even possible, I set it up to work from the bash prompt and it works well, so that's out of the way.

I installed the phpseclib Net_SFTP and Crypt_RSA packages following the phpseclib documentation, and below is my script. To simplify testing, the public key file "id_rsa.pub" and private key file "id_rsa" (generated when I set up sftp from the shell) reside in the same directory as my test script:

////////////////////////////////////////////////

set_include_path(get_include_path() . PATH_SEPARATOR . 'phpseclib');
include_once('Net/SSH2.php');
include_once('Net/SFTP.php');
include_once('Crypt/RSA.php');
include_once('Crypt/Hash.php');
include_once('Math/BigInteger.php');

$rsa = new Crypt_RSA();
//$pubKey = file_get_contents('id_rsa.pub');
//$rsa->setPublicKey($pubKey);
$priKey = file_get_contents('id_rsa');
$rsa->loadKey($priKey);

$sftp = new Net_SFTP('myremoteserver');
//if (!$sftp->login('myusername', $password)) {
if (!$sftp->login('myusername', $rsa)) {
exit("Login Failed\n");
}
echo $sftp->pwd() . "\r\n";
$sftp->put('filename.ext', 'Hello, world!');

////////////////////////////////////////////////

The script works if I use a string password (commented out above), but when I use the $rsa object, the script fails. I tried setting the public key (commented out above) but that fails, too.

Any thoughts on what I'm missing or doing wrong? Thanks!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group