ufw
манипулирует iptables, поэтому, поскольку вы переходите с ufw, я думаю, вы можете захватить свои iptables и посмотреть, что там делает ufw.
Я могу предложить два способа сделать это.
Предполагая, что у вас есть файл, содержащий список имен хостов, разделенных символами новой строки, и что user
и port
для всех подключений, вы можете использовать xargs
.
xargs -I '{}' -P <max-procs> --arg-file <INPUTFILE> bash -c "ssh -o StrictHostKeyChecking=no -p $connectivity_port $user@{} 'bash -s' < $file $scriptargs > $OUT_FOLDER/{}.log 2>&1"
or
cat <INPUTFILE> | xargs -I '{}' -P <max-procs> bash -c "ssh -o StrictHostKeyChecking=no -p $connectivity_port $user@{} 'bash -s' < $file $scriptargs > $OUT_FOLDER/{}.log 2>&1"
Параллелизм можно настроить с помощью флага -P
.
--max-procs=max-procs
-P max-procs
Run up to max-procs processes at a time; the default is 1. If
max-procs is 0, xargs will run as many processes as possible at
a time. Use the -n option with -P; otherwise chances are that
only one exec will be done.
Вывод каждой команды будет записан в $OUT_FOLDER/$HOST.log
.
Если у вас разные user
и port
для каждой машины, вы все равно можете использовать xargs
, но это будет немного сложнее.
Другой вариант — использовать pdsh
, который может «параллельно выдавать команды группам хостов».
pdsh -R exec -w^<INPUT FILE> -f <max-procs> bash -c "ssh -o StrictHostKeyChecking=no -p $connectivity_port %u@%h 'bash -s' < $file $scriptargs 2>&1"
Здесь флаг -f
подобен флагу -P
в xargs.
exec Executes an arbitrary command for each target host. The first of the pdsh remote arguments is the local command
to execute, followed by any further arguments. Some simple parameters are substitued on the command line,
including %h for the target hostname, %u for the remote username, and %n for the remote rank [0-n] (To get a
literal % use %%). For example, the following would duplicate using the ssh module to run hostname(1) across
the hosts foo[0-10]:
pdsh -R exec -w foo[0-10] ssh -x -l %u %h hostname
and this command line would run grep(1) in parallel across the files console.foo[0-10]:
pdsh -R exec -w foo[0-10] grep BUG console.%h
-f number
Set the maximum number of simultaneous remote commands to number. The default is 32.
If выводит вывод команд с префиксомHOSTNAME:
Вот пример.
$ pdsh -R exec -w host1,host2 bash -c "ssh -o StrictHostKeyChecking=no -p 22 %u@%h 'bash -s' <<< 'echo Running script on %h with arguments: \${@}' arg1 arg2 arg3"
host1: Running script on host1 with arguments: arg1 arg2 arg3
host2: Running script on host2 with arguments: arg1 arg2 arg3
Вы можете использовать Perl с Parallel ::ForkManager и IPC ::Open2 .
Использование:
cat list_of_servers.txt | perl para.pl /path/to/script.sh ARG1 ARG2
Кодексpara.pl
:
#!/usr/bin/env perl
use v5.20;
use IPC::Open2 qw(open2);
use Parallel::ForkManager qw();
sub run_script_on_server {
my ( $server, $script, @args ) = @_;
say "$$ running script: $script on server: $server with args: @args";
# TODO: replace with ssh invocation
my $pid = open2( my $chld_out, my $chld_in, "bash", $script, @args );
local $/ = undef;
return <$chld_out>;
}
my $pm = Parallel::ForkManager->new(10);
while ( my $server = <STDIN> ) {
$pm->start and next;
chomp $server;
my $result = run_script_on_server( $server, @ARGV );
say "$$ result from $server: $result";
$pm->finish;
}