Skip to content Skip to sidebar Skip to footer

Running Bash Command Using SSH In PERL

Please be patient. I'm very new to perl. I'm passing a HTML variable from html form to a PERL/CGI script. Pls see the following #!/usr/bin/perl use strict; use warnings; use CGI:

Solution 1:

Please: Never, never, never use user input in a call to system, without at least trying to sanitize them! It is a terrible mistake to assume that the user won't break your system by entering strings that can somehow escape what you're trying to do and do something else. In this case, something like 192.168.0.1 rm -rf / would be sufficient to delete all files from your ssh server. Please heed the standard way of doing things, which is never using user input in a executed command.

There's plenty of modules, and even a standard one, Net::SSH, which can do SSH for you.


Solution 2:

As Jens suggested you should use Net::SSH, that'll make your task easy and reliable.

Sample:

#always use the below in your Perl script
use strict;
use warnings;
#load the Net::SSH module and import sshopen2 from it
use Net::SSH qw(sshopen2); 
#type credentitals and the command you want to execute (this would be your bash script)
my $user = "username";
my $host = "hostname";
my $cmd = "command";
#use sshopen2 to run your command 
sshopen2("$user\@$host", *READER, *WRITER, "$cmd") || die "ssh: $!";
#read the result 
while (<READER>) { #loop through line by line of result
    chomp(); #delete \n (new line) from each line
    print "$_\n"; #print the line from result
}     
#close the handles
close(READER);
close(WRITER);

Post a Comment for "Running Bash Command Using SSH In PERL"