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"?

5
  • 1
    Possible duplicate of How can I call a shell command in my Perl script?, Call a bash script from a perl script, etc. Commented Jul 25, 2019 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. Commented Jul 25, 2019 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 Commented Jul 25, 2019 at 12:17
  • 1
    Can you show the $stderr and $exit values for $ssh->cmd(' ./monitor status ') ? Commented Jul 25, 2019 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. Commented Jul 25, 2019 at 12:51

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

@AmarendhiranAmar - if you like the answer would you consider up voting and accepting it please? stackoverflow.com/help/someone-answers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.