Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i use phpseclib to upload file from my php server -> someOther server?

Tags:

php

phpseclib

I'm trying to upload a file from my php server to some other server (my workplace is quite lame enough to block ssh traffic)

Anyway, here's what I'm trying to do: (at: /public_html/manage.php

<?php

set_include_path(get_include_path() . PATH_SEPARATOR . 'phpseclib/phpseclib');
include('Net/SSH2.php');
include('Net/SFTP.php');

$ssh = new Net_SSH2('SomeServer',22); //starting the ssh connection to localhost
if (!$ssh->login('root', '12345')) { //if you can't log on...
    exit('ssh Login Failed');
}
$sftp = new Net_SFTP('SomeServer',22);
if (!$sftp->login('root', '12345')) { //if you can't log on...
    exit('sftp Login Failed');
}
echo $sftp->pwd();

$output = $sftp->put('/root/1_yum_stuff.sh', '1_yum_stuff.txt');
echo $output;
$output = $ssh->exec('ls -a');
//$output = $ssh->exec('./1_yum_stuff.sh & 1>1.txt');
echo $output;
$output = $ssh->exec('pwd');
echo $output;
echo("3rd try!");
?>

The thing is, the file "1_yum_stuff.txt" is located at /public_html/ folder on my php server, and sftp just can't move the file.

Anyway, is this the right way to use sftp module of phpseclib? (e.g. put?)

Thanks:) !

like image 742
user1894397 Avatar asked Mar 20 '13 07:03

user1894397


1 Answers

I'd rewrite your code a bit. eg.:

<?php

set_include_path(get_include_path() . PATH_SEPARATOR . 'phpseclib/phpseclib');
include('Net/SSH2.php');
include('Net/SFTP.php');

$sftp = new Net_SFTP('SomeServer',22);
if (!$sftp->login('root', '12345')) { //if you can't log on...
    exit('sftp Login Failed');
}
echo $sftp->pwd();

$output = $sftp->put('/root/1_yum_stuff.sh', '1_yum_stuff.txt');
echo $output;
$output = $sftp->exec('ls -a');
//$output = $sftp->exec('./1_yum_stuff.sh & 1>1.txt');
echo $output;
$output = $sftp->exec('pwd');
echo $output;
echo("3rd try!");
?>

Net_SFTP extends Net_SSH2 so it inherits all the methods. Also, CBroe is right - without that third parameter for $sftp->put() phpseclib will create /root/1_yum_stuff.sh with 15 bytes in it (ie. strlen("1_yum_stuff.txt")).

like image 184
neubert Avatar answered Oct 14 '22 00:10

neubert