簡體   English   中英

與每個主機的自定義參數並行SSH

[英]Parallel SSH with Custom Parameters to Each Host

有很多關於並行ssh的線程和文檔,但是我找不到任何關於將自定義參數傳遞給每個主機的信息。 pssh為例,hosts文件定義為:

111.111.111.111
222.222.222.222

但是,我想通過shell腳本將自定義參數傳遞給每個主機,如下所示:

111.111.111.111 param1a param1b ...
222.222.222.222 param2a param2b ...

或者,更好的是,主機和參數將分為2個文件。

因為這不常見,是否誤用了並行ssh? 我應該從我的腳本中創建許多ssh進程嗎? 我該怎么做呢?

你可以使用GNU parallel

假設你有一個文件argfile

111.111.111.111 param1a param1b ...
222.222.222.222 param2a param2b ...

然后跑

parallel --colsep ' ' ssh {1} prog {2} {3} ... :::: argfile

將使用相應的參數在每個主機上運行prog 重要的是每個主機的參數數量相同。

在根據您的需求定制之后,您可以使用以下解決方案:

#!/bin/bash
#filename: example.sh
#usage: ./example.sh <par1> <par2> <par3> ... <par6>

#set your ip addresses
$traf1=1.1.1.1
$traf2=2.2.2.2
$traf3=3.3.3.3

#set some custom parameters for your scripts and use them as you wish.
#In this example, I use the first 6 command line parameters passed when run the example.sh


ssh -T $traf1 -l username "/export/home/path/to/script.sh $1  $2" 1>traf1.txt 2>/dev/null &
echo "Fetching data from traffic server 2..."
ssh -T $traf2 -l username "/export/home/path/to/script.sh $3  $4" 1> traf2.txt 2>/dev/null &
echo "Fetching data from traffic server 3..."
ssh -T $traf3 -l username "/export/home/path/to/script.sh $5  $6" 1> traf3.txt 2>/dev/null &

#your application will block on this line, and will only continue if all 
#3 remotely executed scripts will complete
wait

請記住,上述要求您在機器之間設置無密碼登錄 ,否則解決方案將中斷以請求輸入密碼。

如果你可以使用Perl:

use Net::OpenSSH::Parallel;
use Data::Dumper;

my $pssh = Net::OpenSSH::Parallel->new;

$pssh->add_host('111.111.111.111');
$pssh->add_host('222.222.222.222');

$pssh->push('111.111.111.111', $cmd, $param11, $param12);
$pssh->push('222.222.222.222', $cmd, $param21, $param22);

$pssh->run;

if (my %errors = $ssh->get_errors) {
  print STDERR "ssh errors:\n", Dumper \%errors;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM