How to create a Zip file on a remote server in perl - perl

actualy im using Net::FTP::Recursive to download a directory structure , it works nice for what is required. But since some folders have more than 100/ files, downloading then can take ages. Since a zip file is faster do download, how i could, using perl connect to a remote server via ftp and create a zip file from the remote server/folder to download ?
use Net::Config;
use Net::FTP::Recursive;
$ftp = Net::FTP::Recursive->new("$hostname:$ftp_port", Debug => 0)
or die "Cannot connect to $hostname: $#";
$ftp->login($iLogin,$iPass)
or die "failed ", $ftp->message;
$ftp->binary()
or die "Cannot set to Binary";
$ftp->cwd("/admin/packages/$fileName")
or die "Cannot change working directory ", $ftp->message;
$ftp->rget( $fileName );
#or die "Download Failed ", $ftp->message;
$ftp->quit;
Thank you all for your time

The site(ARGS) method is designed for that. You can send a shell command and make it runs on remote server.
http://perldoc.perl.org/Net/FTP.html#METHODS
However, most of the FTP servers I know disabled that permission, so, good luck

I think you'd need to have SSH access to the system to run a ZIP command. But if that's the case, you could also use SCP to transfer your files more securely. FTP does everything in the open.
Thanks,
F.

Related

Perl SSH to remote server and check file exists

I have developed a script which should login into remote server and do certain operation.
In remote server it will check for sample_file.txt file exists or not, if YES then it should create create_user_file.txt file in home path with certain set of data.
The thing here is I am able to do ssh to remote machine though below script but unable to check for the files since I don't have idea how we can open the remote session and do all the operation.
Below is my code:
#!/usr/bin/perl
use Net::OpenSSH;
.
.
.
foreach my $ip( #list_of_ips){
print "Connecting to $ip...\n";
my $ssh = OpenSSH_Connection( $ip, $user, $passwd );
print "Connected!\n";
$dir = "/remote/server/home/path/";
$file = $dir."sample_file.txt";
if (-e $file){ #if sample_file.txt exists then create user file
open(my $fh, '>', $dir."create_user_file.txt") || die "Couldn't open file $!";;
print $fh "ID=123&PW=Passw0rd";
close $fh;
}
last if($ssh); #if connection is success no need to login into another server
$ssh->close();
}
sub OpenSSH_Connection {
my ( $host, $user, $passwd ) = #_;
my $ssh = Net::OpenSSH->new("$user:$passwd\#$host");
$ssh->error and die "Couldn't establish SSH connection: ". $ssh->error;
return $ssh;
}
Here in the code, below part of code should be executed in the remote server. But its been checking for a file from where I am executing this script.
if (-e $file){ #if sample_file.txt exists then create user file
open(my $fh, '>', $dir."create_user_file.txt") || die "Couldn't open file $!";;
print $fh "ID=123&PW=Passw0rd";
close $fh;
}
How can I keep open the remote session of the server and execute above code, or do we have any Perl module which could do this kind of operation from local server. TIA.
Your assumption that Net::SSH will make remote resources available locally is incorrect.
SSH creates encrypted channel to remote system and drops you either into shell, or runs remote command (for example perl/shell script), or allows to copy/retrieve a file.
You can open SSH channel into remote shell and run commands as on local computer, but if you want to check if file exists on remote system then you have to communicate it through remote shell or perl script.
Probably what you are after is SSH FS which may make remote files available locally.
I must make note that users on local and remote system can have different id on system level and it might be not what you want (manipulate files of different user). This approach may work well if your local and remote id is the same and belongs to you.
I guess that most easy way would be write perl script on remote system and pass some arguments to it. The script on remote system will do rest part of the job.
In such situation you could use Net::SSH2 to establish connection into remote shell and then run remote perl script with parameters.
To get a better idea how it works without creating long post I refer you to PerlMonks Net::SSH2 demo.
Other more difficult way would be writing enough perl code to establish connection with remote system over SSH2 protocol to remote perl script with capability to 'translate' your local commands
$chan->command($argument)
into remote perl code blocks to perform particular task.
Net::SSH2

How to read a file which is on server using perl script

I have a file on my perforce client which i can read from the client but i want to read it from the depot instead from my client and i Have to achieve this using a Perl script and my client name is ata and its root directory is /home/ata/hw
The following code is written for the file on my client
my $clients_file="/home/ata/hw/hard/ip/golden_design_map.cfg";
open(READ,"<$clients_file") or die "Couldn't open the file for reading:$!";
But this is what i want to achieve
my $clients_file="//hw/hard/ip/golden_design_map.cfg";
open(READ,"<$clients_file") or die "Couldn't open the file for reading:$!";
Here //hw/hard/ip/golden_design_map.cfg is the file on the depot or the server.
Is there any module which i can use.Any help is truly appreciated.Thanks in advance.
You can use the p4 command line like this:
open( READ, "p4 print -q $clients_file|" ) or die "Couldn't execute p4:$!";
This assumes that your environment is already set up to run p4 commands (p4 executable in the PATH, valid settings for P4PORT and P4USER, valid login ticket acquired by having run "p4 login" previously, etc).
Or you can use the Perforce Perl module: http://www.perforce.com/perforce/doc.current/manuals/p4script/02_perl.html
You could use File::Remote module.
Synopsis:
use File::Remote;
# read from a remote file
open(REMOTE, "host:/remote/file") or die $!;
print while (<REMOTE>);
close(REMOTE);

PERL SFTP using System, Cannot install SFTP module

I'm a bit new to perl and stackoverflow. If I could use a more familiar language I would, unfortunately I cannot due to certain circumstances. Thanks in advance for the help.
Modules Not Installed: Net::SFTP, WWW::CURL, Net::SSH2, Net::SFTP::Foriegn
Modules Installed: Net::FTP
I am unable to install modules.
Unable to use Net::FTP Tried Default port and port 22, with a username and password. All I get back from the other box's log when trying to connect is "Did not receive identification string from xx.xx.xx.xx" Also unable to use FTP in command line, times out.
$ftp = Net::FTP->new($box,Port=>22, Debug => 0)
or die print "Error: Cannot connect";
$ftp->login($userBox,$passBox)
or die print "Error: Cannot login";
$ftp->cwd()
or die print "Error: Cannot change to Root";
$ftp->cwd($dir)
or die print "Error: Cannot change to selected directory";
if($copyfile ne "" && $dir ne "")
{
$ftp->put($copyfile, $copyfile);
}
$ftp->quit();
I can manually use SFTP through the linux command line, not FTP, so I have been trying to use the system command to SFTP into the other box. The other box's logs just say "Connection closed by xx.xx.xx.xx"
system('sftp '.$userBox.'#'.$box.' ENDOFINPUT'
.$passBox.'ENDOFINPUT
cd ../../../
put '.$filename.' '.$dir.'
exit
ENDOFINPUT');
If anyone knows how to help me with my problem that'd be great :)
Let's approach this from another direction... when you say you're "unable to install modules", is that just because you don't have root permission? If that's the case, you can install them locally under a user account instead.
If the machine doesn't have an internet connection to even install them locally, you can use the same technique to install them on a different box, then gzip the entire local directory where you have them installed and copy them to the target machine, and add a "use lib" statement to get at them from your script.
Are you sure that the $passBox equivalent worked on the shell?
You should be able to set-up passwordless connectivity using key-pairs, which would make THAT problem go away quite quickly.

Fetching files from remote server - Perl/Unix

In regards to the question I previously posted, I have written the following script to load a specific file:
#!/bin/perl
use strict;
use Net::FTP;
my $ftpOb = Net::FTP->new("X")
or die ("Could not connect to the FTP Server: $#");
print "Connected \n";
$ftpOb->login("A", "B")
or die ("Incorrect server credentials");
print "Logged in \n";
print " Current folder is: " . $ftpOb->pwd();
$ftpOb->cwd("root/Folder") or die ("Cannot connect to the folder on Server");
print "Transferred to folder \n";
$ftpOb->get("621418185-006249189002-5383.txt")
or die ("Error occured while fetching the file");
$ftpOb->quit;
However, the code seems to fail to change the working directory and I get the following output:
Connected
Logged in
Cannot connect to the folder on Server at ./GetUploadFile.pl line 16.
Current folder is: /
Can anyone please help me debug the issue here?
Include $ftpOb->message() in your error messages (except for the connect, where $# is the correct error message).
Also do a dir() and list what files/directories are available.

How can I read a file's contents directly with Perl's Net::FTP?

I want to get the file from one host to another host. We can get the file using the NET::FTP module. In that module we can use the get method to get the file. But I want the file contents instead of the file. I know that using the read method we can read the file contents. But how do I call the read function and how do I get the file contents?
From the Net::FTP documentation:
get ( REMOTE_FILE [, LOCAL_FILE [, WHERE]] )
Get REMOTE_FILE from the server and store locally. LOCAL_FILE may be a filename or a filehandle.
So just store the file directly into a variable attached to a filehandle.
use Net::FTP ();
my $ftp = Net::FTP->new('ftp.kde.org', Debug => 0)
or die "Cannot connect to some.host.name: $#";
$ftp->login('anonymous', '-anonymous#')
or die 'Cannot login ', $ftp->message;
$ftp->cwd('/pub/kde')
or die 'Cannot change working directory ', $ftp->message;
my ($remote_file_content, $remote_file_handle);
open($remote_file_handle, '>', \$remote_file_content);
$ftp->get('README', $remote_file_handle)
or die "get failed ", $ftp->message;
$ftp->quit;
print $remote_file_content;
USE File::Remote for Read/write/edit remote files transparently