#!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;