0

I want to run a perl script on a remote server, it should login to a linux server via ssh and run a shell script with an argument, then print the results, please help......

I have a small script which will login and execute the ls command on the remote host, but i want to run a bash script instead.

use Net::SSH::Perl;

my $host = "xxxxx";
my $usr = "xxx";
my $pwd = "xxx";
my $ssh = Net::SSH::Perl->new($host);
$ssh->login($usr,$pwd);
my($stdout, $stderr, $exit) = $ssh->cmd("ls -l");
print "$stdout\n";

How can I execute the remote shell script instead of "ls -l"?

Rob Sweet
  • 184
  • 8
  • 1
    Possible duplicate of [How can I call a shell command in my Perl script?](https://stackoverflow.com/q/3200801/608639), [Call a bash script from a perl script](https://stackoverflow.com/q/11636721/608639), etc. – jww Jul 25 '19 at 10:53
  • 2
    You run any remote command the same way... `$ssh->cmd('whatever');` Doesn't matter what language whatever is implemented in. You might have to include a path if it's not in the remote computer's normal one. – Shawn Jul 25 '19 at 12:04
  • $ssh->cmd(' ./monitor status '); if run this , i am getting only empty output, i am not getting status output , its a bash script with argument – Amarendhiran Amar Jul 25 '19 at 12:17
  • 1
    Can you show the `$stderr` and `$exit` values for `$ssh->cmd(' ./monitor status ')` ? – Håkon Hægland Jul 25 '19 at 12:30
  • 1
    Well there you go. You have to get your program running correctly on the remote computer. Installing whatever package provides that missing library is a good first step. – Shawn Jul 25 '19 at 12:51

1 Answers1

0

Your script is fine. Just replace

"ls -l"

with

"/path/to/my/script arg1 arg2"

Note 1 - It's a good idea to use a fully qualified path to your script on the remote.

Note 2 - arg1 and/or arg2 can be variables of course. $arg1 $arg2. If you're not using variables then there is no need for double quotes and you should use single quotes ' instead.

Rob Sweet
  • 184
  • 8
  • @AmarendhiranAmar - if you like the answer would you consider up voting and accepting it please? https://stackoverflow.com/help/someone-answers – Rob Sweet Jul 26 '19 at 16:13