Last updated on March 1st, 2022 at 09:13 am

Transferring or moving a file to a remote server using web user interface is really a great approach. Sometimes programmers want to develop scripts that will help them to transfer file from one system to another on a fly without logging in to a server.

We are using perl module named Net::OpenSSH

How to install perl module? (Two Methods)

Manual Installation

Download the module to the server
For instance here we have Net-OpenSSH-0.60.tar.gz

$ sudo su -
$ sudo cd /home/root
$ sudo gunzip  Net-OpenSSH-0.60.tar.gz
$ sudo tar -xvf Net-OpenSSH-0.60.tar
$ sudo cd  Net-OpenSSH-0.60
$ sudo perl Makefile.PL
$ make
$ make test

One line Installation (Almost like python pip)

Instead of manually installing modules you can also try installing “cpanm” first and then install any modules you like.

$ curl -L http://cpanmin.us | perl - --sudo App::cpanminus

Once CPANM is installed , proceed with installing these dependency modules

$ sudo cpanm IO::Pty
--> Working on IO::Pty
Fetching http://www.cpan.org/authors/id/T/TO/TODDR/IO-Tty-1.16.tar.gz ... OK
Configuring IO-Tty-1.16 ... OK
Building and testing IO-Tty-1.16 ... OK
Successfully installed IO-Tty-1.16
1 distribution installed

$ sudo cpanm Net::OpenSSH
--> Working on Net::OpenSSH
Fetching http://www.cpan.org/authors/id/S/SA/SALVA/Net-OpenSSH-0.80.tar.gz ... OK
Configuring Net-OpenSSH-0.80 ... OK
Building and testing Net-OpenSSH-0.80 ... OK
Successfully installed Net-OpenSSH-0.80
1 distribution installed

$ sudo cpanm IO::Pty
--> Working on IO::Pty
Fetching http://www.cpan.org/authors/id/T/TO/TODDR/IO-Tty-1.16.tar.gz ... OK
Configuring IO-Tty-1.16 ... OK
Building and testing IO-Tty-1.16 ... OK
Successfully installed IO-Tty-1.16
1 distribution installed

Now you have the Openssh perl module along with dependencies in place. All you have to do is create a script that loads this module and execute a scp command in order to copy the file to the remote server.

#!/usr/bin/perl -w
use Net::OpenSSH;
use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser set_message);
set_message("For help, please send mail to the webmaster (<a href='mailto:'></a>), giving this error message and the time and date of the error");
use strict;
print header;
print start_html("File Copy Status");
print <<EndOfHTML;
<style>
body {
        margin: 0;
        padding: 9px;
        background: #f4f4f4;
        font: 14px;
        font-family: Arial, Helvetica, sans-serif; font-size:13px;
}
pre{
    background-color: #EBECE4;
white-space: -moz-pre-wrap; /* Mozilla, supported since 1999 */
    white-space: -pre-wrap; /* Opera */
    white-space: -o-pre-wrap; /* Opera */
    white-space: pre-wrap; /* CSS3 - Text module (Candidate Recommendation) http://www.w3.org/TR/css3-text/#white-space */
    word-wrap: break-word; /* IE 5.5+ */
}
</style>
EndOfHTML
#Provide the name of the remote server
my $host = '';
print "Transferring <strong>script</strong>s to server $host";
#Provide user name
my $user_name = '';
#Provide password
my $password = '';
my $ssh = Net::OpenSSH->new($host,user => $user_name,passwd => $password,master_opts => [-o => "StrictHostKeyChecking=no"]);
#my $ssh = Net::OpenSSH->new($host,user => $user_name,passwd => $password);
$ssh->error and
    die "Please download the files manually and <strong>copy</strong> it to the host $host ". $ssh->error;     
$ssh->scp_put("/usr/local/apache/htdocs/download/Test_Script1.sh", "/home/root/") or die "SCP failed: " . $ssh->error;
$ssh->scp_put("/usr/local/apache/htdocs/download/Test_Script2.sh", "/home/root/") or die "SCP failed: " . $ssh->error;
print "<h2>Required Files Have Been Uploaded Successfully To $host</h2><br>Now please login to $host and switch to id root. Then run the /home/root/Test_Script1.sh";
print end_html;
  • $host, Which you need to provide the remote server name
  • $user_name, The name of the user for example root,apache etc.,
  • $password, The password for the username you are providing.
  • Make sure Perl interpreter location is defined as per your server configuration
  • Update the put location/files to your choice
$ssh->scp_put("/usr/local/apache/htdocs/download/Test_Script1.sh", "/home/root/") or die "SCP failed: " . $ssh->error;
$ssh->scp_put("/usr/local/apache/htdocs/download/Test_Script2.sh", "/home/root/") or die "SCP failed: " . $ssh->error;

That is all. You are good to execute the script through a web browser. We do have tutorial on how to configure Apache to run perl script, check it out!!

If you want to use a form to submit the server name then please check this link How To Use Get Method In Perl which will help you in implementing GET method using perl script.

Leave a Reply

Your email address will not be published. Required fields are marked *