awk 运行 并行吗?
Does awk run parallelly?
TASK - SSH 到 650 台服务器并从中获取一些详细信息,然后将完整的服务器名称写入不同的文件。如何以更快的方式做到这一点?如果我使用普通 ssh,则需要 7 分钟。因此,我阅读了有关 awk 的内容并编写了以下 2 段代码。
能否请您解释一下以下代码的区别?
代码 1 -
awk 'BEGIN{done_file="/home/sarafa/AWK_FASTER/done_status.txt"}
{
print "blah"|"ssh -o StrictHostKeyChecking=no -o BatchMode=yes -o ConnectTimeout=1 -o ConnectionAttempts=1 "[=11=]" uname >/dev/null 2>&1";
print "[=11=]" >> done_file
}' /tmp/linux
代码 2 -
awk 'BEGIN{done_file="/home/sarafa/AWK_FASTER/done_status.txt"}
{
"ssh -o StrictHostKeyChecking=no -o BatchMode=yes -o ConnectTimeout=1 -o ConnectionAttempts=1 "[=12=]" uname 2>/dev/null"|getline output;
print output >> done_file
}' /tmp/linux
当我 运行 这些代码用于 650 个服务器时,代码 1 需要 - 30 秒而代码 2 需要 7 分钟?
为什么会有这么大的时差?
文件- /tmp/linux是650台服务器的列表
更新答案 - 感谢@OleTange
这种形式比我的建议更可取:
parallel -j 0 --tag --slf /tmp/linux --nonall 'hostname;ls'
--tag Tag lines with arguments. Each output line will be prepended
with the arguments and TAB (\t). When combined with --onall or
--nonall the lines will be prepended with the sshlogin
instead.
--nonall --onall with no arguments. Run the command on all computers
given with --sshlogin but take no arguments. GNU parallel will
log into --jobs number of computers in parallel and run the
job on the computer. -j adjusts how many computers to log into
in parallel.
This is useful for running the same command (e.g. uptime) on a
list of servers.
原答案
我建议使用 GNU Parallel
完成此任务,如下所示:
parallel -j 64 -k -a /tmp/linux 'echo ssh user@{} "hostname; ls"'
这将并行 ssh 到 64 台主机(你可以更改数量),运行 hostname
和 ls
在每台主机上,然后按顺序给你所有结果(-k
转)。
当你看到它是如何工作时,显然删除 echo
。
TASK - SSH 到 650 台服务器并从中获取一些详细信息,然后将完整的服务器名称写入不同的文件。如何以更快的方式做到这一点?如果我使用普通 ssh,则需要 7 分钟。因此,我阅读了有关 awk 的内容并编写了以下 2 段代码。
能否请您解释一下以下代码的区别?
代码 1 -
awk 'BEGIN{done_file="/home/sarafa/AWK_FASTER/done_status.txt"}
{
print "blah"|"ssh -o StrictHostKeyChecking=no -o BatchMode=yes -o ConnectTimeout=1 -o ConnectionAttempts=1 "[=11=]" uname >/dev/null 2>&1";
print "[=11=]" >> done_file
}' /tmp/linux
代码 2 -
awk 'BEGIN{done_file="/home/sarafa/AWK_FASTER/done_status.txt"}
{
"ssh -o StrictHostKeyChecking=no -o BatchMode=yes -o ConnectTimeout=1 -o ConnectionAttempts=1 "[=12=]" uname 2>/dev/null"|getline output;
print output >> done_file
}' /tmp/linux
当我 运行 这些代码用于 650 个服务器时,代码 1 需要 - 30 秒而代码 2 需要 7 分钟? 为什么会有这么大的时差?
文件- /tmp/linux是650台服务器的列表
更新答案 - 感谢@OleTange
这种形式比我的建议更可取:
parallel -j 0 --tag --slf /tmp/linux --nonall 'hostname;ls'
--tag Tag lines with arguments. Each output line will be prepended with the arguments and TAB (\t). When combined with --onall or --nonall the lines will be prepended with the sshlogin instead.
--nonall --onall with no arguments. Run the command on all computers given with --sshlogin but take no arguments. GNU parallel will log into --jobs number of computers in parallel and run the job on the computer. -j adjusts how many computers to log into in parallel. This is useful for running the same command (e.g. uptime) on a list of servers.
原答案
我建议使用 GNU Parallel
完成此任务,如下所示:
parallel -j 64 -k -a /tmp/linux 'echo ssh user@{} "hostname; ls"'
这将并行 ssh 到 64 台主机(你可以更改数量),运行 hostname
和 ls
在每台主机上,然后按顺序给你所有结果(-k
转)。
当你看到它是如何工作时,显然删除 echo
。