Member_2_5182222
asked on
How to use Perl to connect and upload a file to a SSH ftp site
I have Active Perl version 5.24.3.2404 loaded, and I am trying to connect to a SSH ftp using Net::SFTP::Foreign, but I keep getting an error that I need to load io::PTY. However, that's not available for Windows 64-bit (I am running perl on a server 2016 OS, 64-bit.)
I am a novice at Perl, and borrowed this script from an online source.
Can you provide an alternate solution within the active per version I am using?
My failed code so far is as follows:
# Declare modules to use
use warnings;
use strict;
use Net::SFTP::Foreign;
use File::Copy "cp";
###################################################################################
#
# Program Name: UserNamedScript.txt at R:\Scheduled Reports
#
####################################################################################
print "\nStarting execution of program UserNamedScript.txt...\n\n";
my $hostip = 'ftp.userdefined.com';
my $destuser = 'UserDefined';
my $destpasswd = 'DestUserDefined';
my $portno = '8022';
my $localdir = '//UserDefinedPath/';
my $remotedir = '/';
my $filename = 'UserNamedFile.xls';
my $user = $destuser ;
my $passwd = $destpasswd;
print "Remote sftp User = $user\n";
# Initiate the sftp session
my $sftp = Net::SFTP::Foreign->new(host=>"$hostip",port=>$portno, user=>"$user",password=>"$passwd",timeout=>60, asks_for_username_at_login=>0,stderr_discard => 1);
$sftp->die_on_error("\nUnable to establish SFTP connection: " . $sftp->error);
# Transfer file
$sftp->put("//UserDefinedPath/UserNamedFile.xls");
$sftp->disconnect;
I am a novice at Perl, and borrowed this script from an online source.
Can you provide an alternate solution within the active per version I am using?
My failed code so far is as follows:
# Declare modules to use
use warnings;
use strict;
use Net::SFTP::Foreign;
use File::Copy "cp";
###################################################################################
#
# Program Name: UserNamedScript.txt at R:\Scheduled Reports
#
####################################################################################
print "\nStarting execution of program UserNamedScript.txt...\n\n";
my $hostip = 'ftp.userdefined.com';
my $destuser = 'UserDefined';
my $destpasswd = 'DestUserDefined';
my $portno = '8022';
my $localdir = '//UserDefinedPath/';
my $remotedir = '/';
my $filename = 'UserNamedFile.xls';
my $user = $destuser ;
my $passwd = $destpasswd;
print "Remote sftp User = $user\n";
# Initiate the sftp session
my $sftp = Net::SFTP::Foreign->new(host=>"$hostip",port=>$portno, user=>"$user",password=>"$passwd",timeout=>60, asks_for_username_at_login=>0,stderr_discard => 1);
$sftp->die_on_error("\nUnable to establish SFTP connection: " . $sftp->error);
# Transfer file
$sftp->put("//UserDefinedPath/UserNamedFile.xls");
$sftp->disconnect;
ASKER
Like I said, I'm a novice, so what you're telling me isn't detailed enough. How do I "...call up the sftp command line tool directly..."?
The one of the following statements...
system("scp from-file targetuser@targetsystem:targetfile")
or
system("scp sourceuser@sourcesystem:sourcefile to-file")
And setup private/public key access.
system("scp from-file targetuser@targetsystem:targetfile")
or
system("scp sourceuser@sourcesystem:sourcefile to-file")
And setup private/public key access.
noci's comment answered your question except for the detail of key management... where you'll generate a key pair likely with an empty passphrase, then upload the public key to the server's authorized_keys file (for some user), then pass -i /path-to-private-key in your system() call to run sftp.
ASKER
There is no key provided...it's really an ftp connection, but on port 8022, so only the IP (or DNS name), userID, P/W, and Port specification are required to make the connection. Given this additional information, am I going about this all wrong? Maybe there's a simpler method. Some NET::FTP that allows me to specify a port number?
sftp / scp - IS NOT FTP....., it uses SSH protocl as a tunnel and uses that tunnel to transfer files.
You can generate a key with ssh-keygen... put the public key on the server side. Then if the private key has no password then no password will be asked to transfer data. (The key data needs to be properly protected obviously, on unix/linux systems this is enforced by the ssh tooling. Private key with more that owner only access is NOT used).
FTP is definitely not simpler that sftp / scp.
You can generate a key with ssh-keygen... put the public key on the server side. Then if the private key has no password then no password will be asked to transfer data. (The key data needs to be properly protected obviously, on unix/linux systems this is enforced by the ssh tooling. Private key with more that owner only access is NOT used).
FTP is definitely not simpler that sftp / scp.
ASKER
If I connect with a tool like Filezilla, I use:
1. the host name (ftp.ClientHostName.com)
2. the UserID (the UserID they have assigned to me)
3. The Password (the password they have assigned to me)
4. The Port (the port number, 8022, they have provided for me, which I am under the impression is the "secure" port, or SFTP port, versus using port 21.)
There are no security keys involved that I know of....just host/userID, password, and port. Based on this info, is there a better Perl script choice than the Net::SFTP::Foreign I had been trying to use for automating the connection (and file transfer?)
1. the host name (ftp.ClientHostName.com)
2. the UserID (the UserID they have assigned to me)
3. The Password (the password they have assigned to me)
4. The Port (the port number, 8022, they have provided for me, which I am under the impression is the "secure" port, or SFTP port, versus using port 21.)
There are no security keys involved that I know of....just host/userID, password, and port. Based on this info, is there a better Perl script choice than the Net::SFTP::Foreign I had been trying to use for automating the connection (and file transfer?)
ASKER
FYI... It's labeled as an "SFTP - SSH File Transfer Protocol" in the Site Manager of the FIlezilla tool... which is why i thought this was SFTP via SSH.
SFTP = SSH File transfer protocol. It uses SSH service on the remote host.
FTPS = FTP protocol using SSL. (this one is definitely a headache for Firewalls as you still need 2 ports and coordination across the command link).
FTPS = FTP protocol using SSL. (this one is definitely a headache for Firewalls as you still need 2 ports and coordination across the command link).
This question needs an answer!
Become an EE member today
7 DAY FREE TRIALMembers can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Windows != Linux
So no io::PTY or any module where io::PTY is in the dependency chain, so...
Net::SFTP::Foreign isn't an option.
I wouldn't use Net::SFTP::Foreign anyway.
Just call the sftp command line tool directly.
Easiest automation for sftp will be to create an empty passphrase key pair, then upload the public key to your remote user's authorized_keys file (where ever that might live).