Downloading file from remote to local using SCP in phpseclib - PHP - php

I am trying to download a zip file from remote server to local in PHP using phpseclib, I have found a code that lets me upload but I couldn't find anything to download. Here is the code that I found on stackoverflow to upload :
<?php
$cwd = getcwd();// . "/updater/";
set_include_path(get_include_path() . PATH_SEPARATOR . $cwd . '/phpseclib/');
require_once('Net/SSH2.php');
require_once('Net/SCP.php');
$ssh = new Net_SSH2('remote_ip');
if (!$ssh->login('remote user', 'remote_pwd'))
{
throw new Exception("Failed to login");
}
$scp = new Net_SCP($ssh);
if (!$scp->put('remote_path',
'local_path',
NET_SCP_LOCAL_FILE))
{
throw new Exception("Failed to send file");
}
?>
I also tried to switch between the remote_file and the local_file but that will not work.
Thanks in advance.

Related

phpseclib shows no error and login fails in HEROKU

I am using phpseclib to transfer file via SFTP to a server.
In my local computer the file gets transfered perfectly fine to the server, but on Heroku it does not work at all and login fails each time. I am not sure why this is happening.
Here is the piece of code.
$sftp = new \phpseclib\Net\SFTP(getenv('INSTRUM_SERVER'),22222);
if (!$sftp->login(getenv('INSTRUM_USERNAME'), getenv('INSTRUM_PASSWORD'))) {
var_dump($sftp->getSFTPErrors());
exit('Login Failed');
}
"var_dump" returns an empty array so I am not sure why login fails.
I am using composer for the phpseclib to be loaded from vendor.
I'm using phpseclib like this, and it's working fine:
private $sftp = NULL;
private function create_sftp_object( $host, $port = 22, $timeout = 10 )
{
$path = APPPATH . 'libraries/phpseclib';
set_include_path( get_include_path() . PATH_SEPARATOR . $path );
include_once( APPPATH . 'Net/SFTP.php');
return new Net_SFTP( $host, $port, $timeout );
}
public function index()
{
$this->sftp = create_sftp_object(
getenv('INSTRUM_SERVER'),
22222,
6
);
if( $this->sftp->login( getenv('INSTRUM_USERNAME'), getenv('INSTRUM_PASSWORD') ){
echo 'Login successful';
}else{
echo 'Login not successful';
}
}
It's a little different than your code. As you can see, I have phpseclib in my libraries directory. Try it and see if it works for you.

fopen is not working on elastic beanstalk php

I am using the following code in elastic beanstalk php to generate log file but I am getting this error.But the same code is working on another server. How can I make it work ?
Error:
Error opening file in write mode!
Code :
$path="/var/www/html/aws";
$fileName =$path.'/logs/data'.date('Y-m-d_H-i-s'). '_' . uniqid() . '.txt';
$file = fopen($fileName,'w') or die('Could not create report file: ' . $fileName);
foreach($_POST as $key => $value) {
$reportLine = $key." = ".$value."\n";
fwrite($file, $reportLine) or die ('Could not write to report file ' . $reportLine);
}
fputs($file,"log aws");
fclose($file);
Make sure your php user www-data have permission to write in $path.'/logs/ folder or just chmod that logs folder to 0777 permisson

How to set up phpseclib with composer

I have downloaded phpseclib using composer and it created the path /var/www/html/dom/vendor/phpseclib/phpseclib/phpseclib
Now I am not sure where I include my test.php file to run it. Continuously getting the error " Fatal error: Class 'phpseclib\Net\SSH2' not found in /var/www/html/dom/vendor/phpseclib/phpseclib/phpseclib/Net/SFTP.php on line 50"
My code of test.php is
<?php
set_include_path(get_include_path() . PATH_SEPARATOR . '/phpseclib');
include('Net/SFTP.php');
$sftp = new Net_SFTP('domain');
if (!$sftp->login('user', 'pass')) {
exit('Login Failed');
}
// outputs the contents of filename.remote to the screen
echo $sftp->get('filename.remote');
// copies filename.remote to filename.local from the SFTP server
//$sftp->get('filename.remote', 'filename.local');
?>
Composer generates an autoloader. You don't have to care about including things manually. Just do:
include '/var/www/html/dom/vendor/autoload.php';
Besides the include/require the namespace is also different. Thats the solution for me:
require_once __DIR__ . '/vendor/autoload.php';
$handler = new \phpseclib\Net\SFTP('ftp_host');
if (!$handler->login('ftp_user', 'ftp_password')) {
exit('FTP login failed');
}
print_r($handler->nlist());

Connection from one EC2 instance to another EC2 instance using phpseclib

Hi I am using HANA on one EC2 and using by PHP which is already there in SUSE Linux to fetch the data from other Instance. The same code is working fine when i am trying t fetch data from my local system. But it is not working there neither it is showing any error. Please help. Here is my code:
error_reporting(1);
set_time_limit(0);
$date = date("d/m/Y-h:i:s") . "\n"; //to get today's date and time
$logfile = "file-transfer";
$fpath ="log/";
$filepath = $fpath .$logfile . '-log-' . date('Y-m-d') . '.csv'; //path of error log file
$fh1 = fopen($filepath, 'a'); //opening the error log file
fwrite($fh1, "Process Started#" .$date. "\n");
include('Net/SFTP.php');
include('Crypt/RSA.php');
$key = new Crypt_RSA();
$key->loadKey(file_get_contents('key/private.ppk'));
$local_directory = 'sap/';
$remote_directory = '/var/www/';
$file = 'index.html';
$sftp = new Net_SFTP('ec2-XX-XXX-CCC-XX.ap-southeast-1.compute.amazonaws.com');
if (!$sftp->login('ubuntu', $key)) {
fwrite($fh1, "Login Failed" .$date. "\n");
}
else{
fwrite($fh1, "Login Successful#" .$date. "\n");
}
//$success = $sftp->put($remote_directory . $file,$local_directory . $file,NET_SFTP_LOCAL_FILE);
$success = $sftp->get($remote_directory . $file,$local_directory . $file);
if($success){
fwrite($fh1, "File Transferred Successfully#" .$date. "\n");
}
else{
fwrite($fh1, "Error:" .$date.error_get_last(). "\n");
}
Erroneously when i put wrong host name it throw Login failed message. But When i put the correct host name it is not working.
If you are using VPC for your networks inside AWS you need to open the ports in the Security Group so the EC2 instances in the same security group see and can access each other.
http://docs.aws.amazon.com/AmazonVPC/latest/UserGuide/VPC_SecurityGroups.html

Can I use php to update an outdated script with a ZIP packed script from an external domain?

I am writing a script and I was looking into providing some sort of a tool that updates the script when the client requests.
Project is simple logic.
Client server sends a request to mother server (which stores the updated scripts in ZIP files) and downloads the .ZIP file from the server.
Client server receives the .ZIP file and unpacks it overwriting the outdated script files.
Can someone give me a basic guide or what do I need to do to implement such function ?
I can use pclzip to create/extract archives but I do not know where to start.
Thanks for any help, appreciated.
You will need to use ftp(using php) to upload
example in joomla we can do the following :
jimport('joomla.client.ftp');
$ftp = JFTP::getInstance($server['ip'], 21, null, $server['user'], $server['password']);
try {
foreach($files as $file) {
$fullpath = JPATH_SITE . $file;
$this->createdirs($ftp, $rootdir . $file);
if($ftp->store($fullpath, $rootdir . $file) == false) {
throw new Exception("Cannot transfer file " . $file);
}
}
foreach($adminfiles as $file) {
$fullpath = JPATH_SITE . $file;
$this->createdirs($ftp, $rootdir . $file);
if($ftp->store($fullpath, $rootdir . $file) == false) {
throw new Exception("Cannot transfer file " . $file);
}
}
}
catch(Exception $e) {
$ftp->quit();
die($e->getMessage());
}
$ftp->quit();

Categories