重写的答案
不管ssh root
那里有什么滥用......(我更喜欢curl
为此目的使用,但你必须自己写collectFiles.php
;)
好的,这样做的目的是将主机列表的最后一行作为destination
,从列表的其余部分发送文件到哪里。你可以:
Posix外壳优先:
在dash和busybox下测试
tac $1 | (
srcFile=/home/file.txt i=1
read destHost
while read collectHost ;do
destFile=`printf "root@%s:/home/fileHost_%-12s_%03d.txt" \
$destHost $collectHost $i`
i=$((i+1))
echo ssh $collectHost -x "curl -H 'Filename: $destFile' \
--data-binary '@$srcFile http:/$destHost/collectFiles.php && \
rm $srcFile"
done
)
(echo
将被丢弃)
操作变量提供了一种构建命令的好方法,有一个完整的可用示例
#!/bin/bash
mapfile flist <$1
dstCmdFmt="curl -H 'Filename: %s' --data-binary '@%s' http://%s/%s && rm %s"
dstRcvPhp=collectfiles.php
srcFile=/home/file.txt
for ((i=0;i<${#flist[@]}-1;i++));do
printf -v destFile "fileHost_%-12s_%03d.txt" ${flist[i]} $[1+i]
printf -v cmd "$dstCmdFmt" \
${destFile// /_} $srcFile ${flist[@]:${#flist[@]}-1} $dstRcvPhp $srcFile
echo ssh ${flist[i]} -x "$cmd"
done
尝试使用文件:
cat <<eof > testfile
machineA
OtherMachine
AnotherHost
DestinationHost
eof
./script.sh testfile
ssh machineA -x curl -H 'Filename: fileHost_machineA_____001.txt' --data-binary '@/home/file.txt' http://DestinationHost/collectfiles.php && rm /home/file.txt
ssh OtherMachine -x curl -H 'Filename: fileHost_OtherMachine_002.txt' --data-binary '@/home/file.txt' http://DestinationHost/collectfiles.php && rm /home/file.txt
ssh AnotherHost -x curl -H 'Filename: fileHost_AnotherHost__003.txt' --data-binary '@/home/file.txt' http://DestinationHost/collectfiles.php && rm /home/file.txt
旧答案
代替...
nb_lignes=`wc -l <$1`
machine1=`sed -ne ${nb_lignes}p <$1`
for i in `seq $(($nb_lignes - 1))` ;do
machine=`sed -ne ${i}p <$1`
ssh root@$machine -x " scp /home/file.txt root@$machine1:/home && rm -r /home/file.txt"
done
但...
如果从每个machine
,您确实发送不同file.txt
(但名称相同)到同一目标目录中的相同唯一machine
文件,您每次都将覆盖先前发送的文件。