我有一個 Perl 腳本。我想遠程到不同的主機,并在那里運行多個命令(不是 5,10,而是 100-150 個命令)。喜歡,
Script1.pl演算法(本機腳本):
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.
如何實施?
我是 Perl 新手,在瀏覽互聯網后,我看到系統命令可以幫助 [system (ssh remote_host "command1; command2;..")],但是這個系統命令似乎不可行,因為要運行 100-150 個命令不能這樣寫。
有人可以幫忙嗎?有沒有辦法像我們打開一個SSH服務器,運行多個命令,然后關閉它?我實際上正在尋找這種型別的技術。
謝謝!
uj5u.com熱心網友回復:
一種選擇可能是將您的命令串列寫入腳本檔案(以適合您的遠程系統的任何格式 - bash、PowerShell 等)。然后您可以使用scp將腳本復制到遠程服務器并ssh遠程運行腳本。
uj5u.com熱心網友回復:
您可能會達到命令列長度的限制。
my @remote_cmds = ...;
open( my $pipe, "|-", "ssh", "machine10", "sh" );
for my $remote_cmd ( @remote_cmds ) {
say $pipe $remote_cmd;
}
close( $pipe );
演示:
$ hostname
tribble00000006
$ perl -M5.010 -e'
my @remote_cmds = ( "hostname" );
open( my $pipe, "|-", "ssh", "ikegami\@CENSORED.com", "sh" );
for my $remote_cmd ( @remote_cmds ) {
say $pipe $remote_cmd;
}
close( $pipe );
'
springfield
解決方法:
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 );
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/379949.html
