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

风格不统一 提交于 2019-12-10 14:16:46

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/15518049/how-do-i-use-phpseclib-to-upload-file-from-my-php-server-someother-server

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!