Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi All,

How to get the SFTP return value from shell to perl, while i print the return value, it shows as like 'ftp> sftp>'.

kindly help me how to resolve this.

my $sftp_result = `sftp $host <<EOF put $gz_filename $dest_path bye EOF`; print "$sftp_results";


Thanks in advance
Shanmugam A.

Replies are listed 'Best First'.
Re: SFTP return value from shell to perl
by salva (Canon) on Apr 05, 2012 at 23:32 UTC
Re: SFTP return value from shell to perl
by bulk88 (Priest) on Apr 05, 2012 at 16:21 UTC
    Maybe you want system, which runs a program and return the exit code. But unless you can get sftp non-interactive (you can't have new lines in the string given to system), that wont help you. If you want to "type" to the program while it is running, you need to use open or one of its module descendants like Open3 or read perlipc. Timeouts of the app your type into and read the screen from will be a very large pain.

      Hi,
      Thanks for the reply, yes it is non interative ie., it don't ask pwd to enter. now i want the return value of that runned sftp command
      Thanks
      Shanmugam A.

        In that case, you can use system by putting your commands into a batchfile for sftp. Untested:

        my $batchfile = "/tmp/batchfile.$$"; open my $bfd, '>', $batchfile or die $!; print $bfd <<EOF; put $gz_filename $dest_path bye EOF close $bfd; my $return_value = system('sftp', "-b $batchfile", $host); unlink $batchfile;

        Aaron B.
        My Woefully Neglected Blog, where I occasionally mention Perl.