Home > database >  Is there a way to remote to a different machine, and run multiple commands there in series (using pe
Is there a way to remote to a different machine, and run multiple commands there in series (using pe

Time:12-10

I have a Perl script. I want to remote to a different host, and run multiple commands (not 5,10 but 100-150 commands) there. Like,

Algorithm of Script1.pl (the script on local machine):

ssh machine10
// Then execute command1 on machine10
// Then execute command2 on machine10
// Then execute command3 on machine10
.
.
.
// Then execute command100 on machine10, and so on

Then exit, from the remote host.
Then, run the commands on the local machine.

How can it be implemented?

I am new to Perl, and after internet browsing, I saw that system command could help [system (ssh remote_host "command1; command2;..")], but this system command seems not to be feasible as 100-150 commands to run can't be written as such.

Can anyone please help? Is there any way like we open an SSH server, and run multiple commands, and then close it? I am actually looking for such type of technique.

Thank you!

CodePudding user response:

One option might be to write your list of commands into a script file (in whatever format is appropriate for your remote system - bash, PowerShell, whatever). You can then use scp to copy the script to the remote server and ssh to run the script remotely.

CodePudding user response:

You could be hitting the limit of the command line length.

my @remote_cmds = ...;

open( my $pipe, "|-", "ssh", "machine10", "sh" );
for my $remote_cmd ( @remote_cmds ) {
   say $pipe $remote_cmd;
}

close( $pipe );

Workaround:

use String::ShellQuote qw( shell_quote );

my @remote_cmds = ...;

my $local_cmd = shell_quote( "ssh", "machine10", "sh" );

open( my $pipe, "|-", $local_cmd );
for my $remote_cmd ( @remote_cmds ) {
   say $pipe $remote_cmd;
}

close( $pipe );
  • Related