Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Net::Sftp::Foreign writes empty files to FTP server

by Bloehdian (Beadle)
on Feb 05, 2020 at 02:34 UTC ( [id://11112405]=perlquestion: print w/replies, xml ) Need Help??

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

The following code is intended to simply transfer a file stored on hard disk to a remote sftp server:

sub sftp_transfer { my ( $ftp_url, $ftp_user, $ftp_passwd, $ftp_dir, $output_file_path +) = @_; if ( $debug ) { print "&sftp_transfer: "; print "Host: $ftp_url\n"; print "User: $ftp_user\n"; print "Password: $ftp_passwd\n"; print "Dir: $ftp_dir\n"; } my %conn = ( "user" => $ftp_user, "password" => $ftp_passwd, "port" => 2222, "autoflush" => 1 ); my $ftp=Net::SFTP::Foreign->new( $ftp_url, %conn ); if ( $ftp->error ) { $err_occured = 1; $error_msg ="SFTP: Cannot connect to sftp-server $ftp_url: " . $ftp->err +or; die ( "\n" ) }; $ftp->setcwd( $ftp_dir ) or do { $err_occured = 1; $error_msg = "SFTP: Cannot cd into $ftp_dir: " . $ftp->error; die ( "\n" ) }; my $output_file_name = basename( $output_file_path ); $ftp->put( $output_file_path, $output_file_name ) or do { $err_occured = 1; $error_msg = "SFTP: Cannot upload file $output_file_path: " . $f +tp->error; die ( "\n" ) }; }

Output (with $debug == 1)

&sftp_transfer: Host: example.com User: user Password: *** Dir: /POS/21/2100139 Password Authentication

On the command line, file transfer works flawlessly. When applying the above code, the files transferred have zero Byte size, although not being empty prior to transfer, The sending client is a Linux box, I not know about the server. Any ideas whatis going wrong here?

Replies are listed 'Best First'.
Re: Net::Sftp::Foreign writes empty files to FTP server
by salva (Canon) on Feb 08, 2020 at 10:11 UTC
    Net::SFTP::Foreign has a debug mode you can enable adding the following line at the beginning of the script:
    $Net::SFTP::Foreign::debug = -1;
    Enable it and then post here the output (though, you should take care to remove passwords or any other sensible information from the output)
Re: Net::Sftp::Foreign writes empty files to FTP server
by soonix (Canon) on Feb 07, 2020 at 16:39 UTC
    Is the file readable for your script? (see -X)
    Does your input path point to the correct location and why do you name your input path $output_file_path?
Re: Net::Sftp::Foreign writes empty files to FTP server
by Naren (Initiate) on Jan 06, 2022 at 18:13 UTC
    It must be due to put method trying to copy the time and permission of the file. Try passing copy_time => 0, copy_perm => 0 params to Put method.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11112405]
Approved by herveus
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (2)
As of 2024-04-19 20:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found