CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
HOw to use sftp from from one server to another

 



digioleg54
User

Feb 15, 2017, 12:02 PM

Post #1 of 8 (1683 views)
HOw to use sftp from from one server to another Can't Post

I am learning the old script, which sftp file from one server to remote and found one line:

my $reslt = `sftp -b $xxxx_flag/$xxxx_file.$$ passwd\@servername`;

Then checking:
if ($reslt =~ /Connection closed/) {
print
exit
}
else{
print
}
and so on.
Does it mean that only one line sftp file? Normaly everything is set up so that we can go from one server to another using ssh without password, i.e. SSH_KEY set up


FishMonger
Veteran / Moderator

Feb 15, 2017, 1:36 PM

Post #2 of 8 (1681 views)
Re: [digioleg54] HOw to use sftp from from one server to another [In reply to] Can't Post

Yes, using that method only sends a single file.

That's a very messy way to send files. Instead, you should be using one of the sftp modules.

Net::SFTP::Foreign - SSH File Transfer Protocol client
http://search.cpan.org/~salva/Net-SFTP-Foreign-1.87/lib/Net/SFTP/Foreign.pm

Net::SSH2 - Support for the SSH 2 protocol via libssh2.
http://search.cpan.org/~salva/Net-SSH2-0.63/lib/Net/SSH2.pm


digioleg54
User

Feb 15, 2017, 1:40 PM

Post #3 of 8 (1679 views)
Re: [FishMonger] HOw to use sftp from from one server to another [In reply to] Can't Post

Unfortunately, as I told you earlier, we are using Perl version 5.8.8. I cannot add any new package, my boss said that nobody will give me to make a revolution (add new packages or new version of perl)

Why this method is messy?

Thanks


FishMonger
Veteran / Moderator

Feb 15, 2017, 1:54 PM

Post #4 of 8 (1677 views)
Re: [digioleg54] HOw to use sftp from from one server to another [In reply to] Can't Post

If you only need to transfer 1 file to 1 server, this method is "ok" but far from being "perlish".

If no-one is allowed to install modules, then you're throwing away the vast majority of the reasons to use perl. You might as well just do everything in shell scripts. Perl scripts that do a lot of backtick and system calls instead of using the power of perl are just shell scripts disguised as perl scripts.


ogit
Novice

Feb 16, 2017, 5:49 AM

Post #5 of 8 (1666 views)
Re: [digioleg54] HOw to use sftp from from one server to another [In reply to] Can't Post

Hi

You may already have the Net::FTP module available. If you have then that seems a easy and straight forward way of FTP. I have never used it for SFTP, but the documentation does discus FTPS. Below is a snippet of code for basic FTP (will not compile and as an example only).



Code
use Net::FTP; 

my $ftp_log = "ftp_log.txt";
my $ftp_site = 'test_ftp';
my $ftp_dir = 'dir';
my $ftp_user = 'ogit';
my $ftp_password = 'igotpass';
my $glob = '*.*';
my @remote_files;

my $ftperr = 0;
my $ftp = Net::FTP->new($ftp_site) or $ftperr = 1;
if ($ftperr == 0)
{
$ftp->login($ftp_user, $ftp_password) or $ftperr = 2;
}

if ($ftperr == 1)
{
open (MYFILE, ">>$ftplog");
print MYFILE "Could not connect to $ftp_site: $! \n";
close (MYFILE);
}

if ($ftperr == 2)
{
open (MYFILE, ">>$ftplog");
print MYFILE "Could not login to $ftp_site with user $ftp_user: $! \n";
close (MYFILE);
}

if ($ftperr == 3)
{
open (MYFILE, ">>$ftplog");
print MYFILE "Could not change remote working directory to $ftp_dir on $ftp_site \n";
close (MYFILE);
}

if ($ftperr == 0)
{
my @remote_files = $ftp->ls($glob);
my @local_files = glob("*.tiff *.tif *.pdf");
$ftp->ascii();

foreach my $file (@remote_files)
{
print "Getting FTP File $file\n";
$ftp->get($file) or $fte = 1;
$ftp->delete($file) or $fte = 2;
...
...

foreach my $file (@local_files)
{
print "Sending FTP File $file\n";
$ftp->put($file) or $fte = 1;
...
...



digioleg54
User

Feb 16, 2017, 7:52 AM

Post #6 of 8 (1660 views)
Re: [ogit] HOw to use sftp from from one server to another [In reply to] Can't Post

I understand, but if it was my own company, I will do it. Here they think that they need to have one more guy to install new version of Perl or new module, meaning pay him money. But they don't want top pay. Unfortunately. Is it possible to install separate Perl module and do not re-write old Perl scripts?


FishMonger
Veteran / Moderator

Feb 16, 2017, 9:09 AM

Post #7 of 8 (1655 views)
Re: [digioleg54] HOw to use sftp from from one server to another [In reply to] Can't Post

The Net::FTP module that ogit shows is a core module so it should already be installed on your system unless your admins screwed the system up. However, it doesn't support sftp.


Quote
Is it possible to install separate Perl module and do not re-write old Perl scripts?

Yes, you can install perl modules in any directory that you have write access and it's very common.

http://lmgtfy.com/?q=install+perl+module+in+specific+directory

However, you also nee to add that directory to the @INC array, which can be done several ways.


(This post was edited by FishMonger on Feb 16, 2017, 9:47 AM)


digioleg54
User

Feb 16, 2017, 9:41 AM

Post #8 of 8 (1653 views)
Re: [ogit] HOw to use sftp from from one server to another [In reply to] Can't Post

I cannot use ftp, it is a company policy, but thank you anyway

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives