Thejas Thez
Thejas Thez

Reputation: 1

Net::SSH::Perl module: Executing multiple commands

I want to execute two commands in a single line using Net::SSH::Perl module..

Actually first command sets some env variable, second runs some command which needs the env variable to be set for that shell.

How to make the below piece of code work

Eg:

use Net::SSH::Perl;
my $hostname = "<<hostname>>";
my $username = "<<username>>";
my $password = "<<password>>";

my $cmd = 'export PATH= *** ; java -version';

my $ssh = Net::SSH::Perl->new("$hostname", debug=>0);
$ssh->login("$username","$password");

my ($stdout,$stderr,$exit) = $ssh->cmd("$cmd");
print $stdout;

This question is already asked in this site, but there are no answers.

Calling $ssh->cmd several times won't work, because the two calls will not be invoked in the same session.

Upvotes: 0

Views: 3950

Answers (3)

Mak_Thareja
Mak_Thareja

Reputation: 177

use Net::SSH::Perl;
my $hostname = "<<hostname>>";
my $username = "<<username>>";
my $password = "<<password>>";

my $cmd1 = "source ~ur_env_paths";
my $cmd2 = "your next command";

my $ssh = Net::SSH::Perl->new("$hostname", debug=>0);
$ssh->login("$username","$password");

my ($stdout,$stderr,$exit) = $ssh->cmd("$cmd1 && $cmd2"); ## just enter ur commands and they will execute in that sequence.

print $stdout;

Upvotes: -1

salva
salva

Reputation: 10242

The easiest way to call some program with some variables set is to put the variable definitions right before the command:

VAR1=VALUE1 VAR2=VALUE2 ... cmd arg1 arg2 arg3 ...

In your case where it seems you already have the variable definitions in some file do as follows:

source /path/to/scripts/that/sets/vars.sh && cmd arg1 arg2 arg3 ...

That works both when running commands locally (i.e. via system) or remotely via ssh.

Also note that you may need to quote shell metacharacters in the variable names and values and on the command name and arguments.

Upvotes: 1

akluth
akluth

Reputation: 8583

To cite the comprehensive Perl archive network:

($out, $err, $exit) = $ssh->cmd($cmd, [ $stdin ])

Runs the command $cmd on the remote server and returns the stdout, stderr, and exit status of that command.

If $stdin is provided, it's supplied to the remote command $cmd on standard input.

NOTE: the SSH-1 protocol does not support running multiple commands per connection, unless those commands are chained together so that the remote shell can evaluate them. Because of this, a new socket connection is created each time you call cmd, and disposed of afterwards. In other words, this code:

my $ssh = Net::SSH::Perl->new("host1");
$ssh->login("user1", "pass1");

$ssh->cmd("foo");
$ssh->cmd("bar");

will actually connect to the sshd on the first invocation of cmd, then disconnect; then connect again on the second invocation of cmd, then disconnect again.

Note that this does not apply to the SSH-2 protocol. SSH-2 fully supports running more than one command over the same connection.

So you've got two options:

  1. Chaining your commands with two ampersands ("cmd1 && cmd2 && cmd3 && ...")
  2. Using SSHv2

Upvotes: 3

Related Questions