如何同时将单个文件发送到多个远程站点?

Naw*_*que 5 ssh scp rsync

可以scp用于将单个文件同时发送到多个远程服务器吗?如果是这样,如何?如果没有,有什么替代方案?

cas*_*cas 10

pdcppdsh包是一种选择。 pdsh是为了帮助管理 HPC 集群而编写的 - 我已经用它来解决这个问题,我也用它来管理多台非集群机器。

pdshpdcp使用性别来定义主机和主机组(“组”是您选择分配给主机的任意标签,主机可以拥有任意数量的标签。)

例如,如果您有一个名为“webservers”的组/etc/genders,其中包括 hostA、hostB、hostC,则将pdcp -g webservers myscript.sh /usr/local/binmyscript.sh 复制到所有三个主机上的 /usr/local/bin/ 中。

类似地,pdsh -g all uname -runame -r在 /etc/genders 中标有“all”的每个主机上运行,每个主机的输出都带有主机名的前缀。

$ pdsh -g all uname -r
indra: 3.2.0-3-amd64
kali: 3.2.0-3-amd64
ganesh: 3.2.0-3-amd64
hanuman: 3.2.0-2-686-pae
Run Code Online (Sandbox Code Playgroud)

pdsh 命令和 pdcp 副本并行执行(具有限制和超时以防止原始系统过载)。

当正在运行的命令产生多行输出时,它可能会使引用混淆阅读。pdsh包中的另一个程序dshbak可以按主机名对输出进行分组以便于阅读。


在看到您的所有评论后,pdsh 和 pdcp 可能会满足您的需求……它确实被设计为系统管理员的工具,而不是普通的非 root 用户的工具。

可能围绕 scp 编写一个简单的 shell 脚本包装器可能对您来说已经足够了。例如,这是这种包装脚本的一个极其简单、极简的版本。

#! /bin/bash 

# a better version would use a command line arg (e.g. -h) to get a
# comma-separated list of hostnames, but hard-coding it here illustrates
# the concept well enough.
HOSTS="mindfreak@asper.comgrid.ac mindfreak@looper.comgrid.ac"

# last argument is the target directory on the remote hosts
target_dir="$BASH_ARGV"

# all but the last arg are the files to copy
files=${@:1:$((${#@} - 1))}

for h in $HOSTS; do
    scp $files "$h:$target_dir"
done
Run Code Online (Sandbox Code Playgroud)