%connection_details = ( host => '', user => '', pass => '', port => '', ); $SFTP = &SFTP_connection(%connection_details); exit (0); sub SFTP_connection { my (%connection_info) = @_; use Net::OpenSSH::Compat::SSH2 qw(:supplant); use Net::SSH2; eval { require Net::SSH2; }; if ($@) { print "The Net::SSH2 or Net::OpenSSH::Compat::SSH2 Perl module is not installed on this server, which is required for this feature.\n Please contact your server administrator and ask them to install this module.\n"; return 0; } ## Connect to the sFTP server my $SFTP = Net::SSH2->new(); $SFTP->connect($connection_info{'host'},22) or die "Unable to connect to the remote sFTP server \n\n $@"; ## Login to sFTP server $SFTP->auth_password($connection_info{'user'},$connection_info{'pass'}) or die "Unable to login Check username and password. \n\n $@\n"; return $SFTP; }