書き直された答え
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
する場合は、毎回前に送信したファイルを上書きします。