2

I am new in LFTP and trying to automate my file delivery through perl code using LFTP. I am able to set the proxy server and connect to remote host and also able to successfully transfer the file to remote host using perl code. But I am not able to get the response from lftp transfer which can let my perl job know that transfer is successfully completed due to this my perl job is failing considering there is some issue with the transfer. I have tried verbose option as well but it is also not displaying any response while transferring the file using lftp.

open(FTP,"lftp -vvv <command_file |")

command_file has below lines of code :

set ftp:proxy http://proxy_server:port
open ftp://remote_server_name
user user_name password
cd /remote_server_dir
put /local_server_file_name -o remote_server_file_name

Now after calling this code "FTP", file is successfully transferred to remote host but to validate it is successful I am using below code :

while(<FTP>)
{
   print $_;
   if ((/transferred/))
   {
      # successful
   }
   else
   {
      # not successful
   }
}

LFTP while executing manually gives response like 300 bytes transferred. And therefore I am trying to use keyword transferred as a sign of successful transfer. But it seems while running this LFTP through perl I am not getting any response at all.

Please let me know how to get the response from remote server after successful transfer of the file through automated job. Thanks!

Stefan Becker
  • 5,695
  • 9
  • 20
  • 30

2 Answers2

1

I had to use "set cmd:verbose yes" in command file to get the desired functionality working.

Majak
  • 11
  • 1
0

Three issues:

  1. <command_file doesn't work, -f command_file does,
  2. some messages go to STDERR, so we need a redirect to catch all, and
  3. as pointed out by @lav you need to enable interactive mode manually if STDOUT isn't a tty

Updated complete code example:

#!/usr/bin/perl
use strict;
use warnings;

my $successful;

# redirect STDERR to STDOUT: some messages go to STDERR!
if (open(my $ftp_fh, "lftp -vvv -f dummy.txt 2>&1 |")) {
    while(<$ftp_fh>) {
        print;
        $successful++ if / transferred$/;
    }

    close($ftp_fh)
        or die "lftp command failed: $!\n";

} else {
    die "lftp execution failed\n";
}

print "File transfer ", ($successful ? "" : "not "), "successful!\n";

exit 0;

Here is my command file:

set interactive yes
set xfer:clobber yes
open ftp://ftp.funet.fi
user Anonynmous test@does.not.exist.com
cd /pub/linux/kernel/v4.x
get sha256sums.asc

Test run:

$ perl dummy.pl
cd ok, cwd=/pub/linux/kernel/v4.x
288261 bytes transferred
File transfer successful!
Stefan Becker
  • 5,695
  • 9
  • 20
  • 30