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

1

6

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:) !

Olmos answered 20/3, 2013 at 7:51 Comment(1)
Could it be that you have to use the 3rd parameter to the put method as described here: https://mcmap.net/q/370116/-uploading-files-with-sftp so that the lib knows it’s a local file you want to upload?Gorlicki
B
3

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")).

Bithia answered 20/3, 2013 at 14:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.