将单个文件scp到多个位置


28

您可以scp使用同一命令将文件存储到多个位置吗?

例如:

scp file.txt user@ip-1.com:, user@ip2.com:

还是创建一个已经包含所有主机并且仅将文件作为参数的bash脚本更实用?

Answers:


24

假设您有一个destfile.txt带有user@host-values 的文件(),每行一个。然后您可以这样做:

while IFS= read -r dest; do
  scp ourfile.txt "$dest:remote/path/"
done <destfile.txt

这可行!虽然仍然很少。也许有一种方法可以简化。
安德鲁

2
@Andrew,如果将for-loop 更改为while read dest; do,它将从标准输入读取。将其放在脚本中,然后将其destfile.txt输入(例如./thescript.sh <destfile.txt)。
Kusalananda 2011年

我正在做前者,但是使用rsync和我的某些节点未更新。知道为什么吗?
Soubriquet


4
cat file.txt | tee >(ssh user@ip1.com "cat > file.txt") \
                   >(ssh user@ip2.com "cat > file.txt")

tar cz file1 file2 file3 | tee >(ssh user@ip1.com "tar xz") \
                               >( ... )

0

另一种选择(也是一种方法)是改为使用pdsh连接到每个目标节点并从那里触发获取:

pdsh -w^destfile.txt scp hostname:/path/to/file /path/to/destfile

当然,这需要更多信息(本地主机)和不同的用户权限,但是您可以避免在bash中循环并使用间接读取文件。


0

这是另一种选择,带有一个命令行脚本。

cscp.sh 20337.patch < hosts.txt

它使用两个文件,一个用于循环,一个用于服务器主机列表。它$1从CLI 读取第一个参数作为SCP的文件名

cscp.sh

#!/bin/bash
while read host; do
  scp $1 ${host}:
done

hosts.txt

project-prod-web1
project-prod-web2
project-prod-web3

用法

将文件复制到多个主机:

cscp.sh file < hosts

0

如果对多个服务器使用一致的命名约定,则可以执行以下整洁的操作:

for x in st1-abc-{11..20}.acme.com; do scp filez.tgz user@$x; done
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.