in reply to Generate temporary file and FTP upload

Hello Anonymous Monk,

Although the fellow monks have provided you with knowledge, I would like to add a few comments. Do not use bare words for file handles, read here why Don't Open Files in the old way. Every time you open a file handle or close one use die or warn.

Having said that moving on, why are you using Net::FTP when there is the secure version Net::SFTP? But if I was you I would not use any of both, I would use Net::SFTP::Foreign. Why? I am glad you ask. Simply read here Net::SFTP::Foreign Vs. Net::SFTP Vs. Net::SSH2::SFTP.

Having said that, it comes to another question. Why you want to use File::Temp? It is great for some cases but I assume for the case that you want to use it is not exactly what you should be looking for. Here is a sample of code that is working and producing in /tmp directory the html that you want.

#!usr/bin/perl use strict; use warnings; use Fcntl qw( SEEK_END ); use File::Temp qw( tempfile ); #Creating a temporary html file in /tmp dir my $tmp = File::Temp->new( UNLINK => 0, SUFFIX => '.html' ); print $tmp "Some data\n"; print "Filename is $tmp\n"; $tmp->seek( 0, SEEK_END ); __END__ $ perl test.pl Filename is /tmp/VlCSL2Fs39.html $ cat /tmp/VlCSL2Fs39.html Some data

I tried combining the File::Temp and the Net::SFTP::Foreign but I was getting the following error:

Filename is /tmp/grb8bazGjM.html unable to infer remote file name when a file handler is passed as loca +l at test.pl line 31.

Which tells me (if I am not mistaken) that I can not combine those two modules together. So any alternatives? Of course!!!!! Simply create the file add what ever html tags and document you want in send the file and simply after delete the file. So simple. How do I delete the file? Simply use unlink.

Complete example on creating the file, sending the file through SSH (with ssh keys exchange most secure way instead of passwords etc...) and as soon as the process is complete delete the file. Voila you are done!!!! :D

#!usr/bin/perl use say; use strict; use warnings; use Net::SFTP::Foreign; my $htmlString = "<!DOCTYPE html> <html> <body> <h1>My First Heading</h1> <p>My first paragraph.</p> </body> </html>"; my $file = 'test.html'; #Creating an html file open my $fh, ">", $file or die "Can't open $file: $!"; say $fh $htmlString; close $fh or warn "Can't close $file: $!"; my %args = ( host => "localhost", user => "user", port => "22", # psw => "psw", # uncomment if you are using passwords key_path => "/home/user/.ssh/id_rsa" ); # comment if you are +using passwords my $sftp = Net::SFTP::Foreign->new(%args); $sftp->die_on_error("Unable to establish SFTP connection"); my $path = '/path/path'; $sftp->setcwd($path) or die "unable to change cwd: " . $sftp->error; $sftp->put($file) or die "put failed: " . $sftp->error; unlink $file or warn "Could not unlink $file: $!"; __END__ $ perl test.pl $ cat /tmp/test.html <!DOCTYPE html> <html> <body> <h1>My First Heading</h1> <p>My first paragraph.</p> </body> </html>

Update: I was wrong, you can combine the modules. Working example:

#!usr/bin/perl use say; use strict; use warnings; use Net::SFTP::Foreign; use Fcntl qw( SEEK_END ); use File::Temp qw( tempfile ); #Creating a temporary html file in /tmp dir my $fh = File::Temp->new( UNLINK => 1, SUFFIX => '.html' ); say $fh "Some data"; say "Filename is $fh"; $fh->seek( 0, SEEK_END ); my $file = $fh->filename; my %args = ( host => "localhost", user => "user", port => "22", # psw => "psw", key_path => "/home/user/.ssh/id_rsa" ); my $sftp = Net::SFTP::Foreign->new(%args); $sftp->die_on_error("Unable to establish SFTP connection"); my $path = '/path/path'; $sftp->setcwd($path) or die "unable to change cwd: " . $sftp->error; $sftp->put($file) or die "put failed: " . $sftp->error;

Hope this helps more, BR.

Seeking for Perl wisdom...on the process of learning...not there...yet!

Replies are listed 'Best First'.
Re^2: Generate temporary file and FTP upload
by salva (Canon) on Jul 03, 2017 at 07:53 UTC
    Note that File::Temp generates a file with a name unique on the local computer, but nothing guarantees that name to be also unique on the remote file system.