1. 简单的批量分发脚本

新建一个脚本文件,文件名为file_fenfa.sh,文件内容如下:
scp hosts 172.16.1.8:~
ssh -t 172.16.1.8 sudo rsync ~/hosts /etc/hosts

scp hosts 172.16.1.31:~
ssh -t 172.16.1.31 sudo rsync ~/hosts /etc/hosts

scp hosts 172.16.1.41:~
ssh -t 172.16.1.41 sudo rsync ~/hosts /etc/hosts

执行结果:
最low的方法实现文件批量分发 - 图1
有这样的提示,证明执行成功。

2. 对上面脚本进行升级

!/bin/sh
. /etc/init.d/functions

for n in 8 31 41
do

scp hosts 172.16.1.$n:~ >/dev/null 2>&1&&\
ssh -t 172.16.1.$n sudo rsync ~/hosts /etc/hosts >/dev/null 2>&1

if [ $? -eq 0 ];then
action “fenfa hosts 172.16.1.$n” /bin/true
else
action “fenfa hosts 172.16.1.$n” /bin/false
fi

done
脚本执行结果:
最low的方法实现文件批量分发 - 图2

3. 对脚本进行二次升级

!/bin/sh
if [ $# -ne 2 ];then
echo “USAGE:/bin/sh $0 ARG1 ARG2”
exit 1
fi

. /etc/init.d/functions

for n in 8 31 41
do

scp $1 172.16.1.$n:~ >/dev/null 2>&1&&\
ssh -t 172.16.1.$n sudo rsync $1 $2 >/dev/null 2>&1

if [ $? -eq 0 ];then
action “fenfa hosts 172.16.1.$n” /bin/true
else
action “fenfa hosts 172.16.1.$n” /bin/false
fi

done
在使用上面的脚本的时候,需要提供两个参数,一个是要拷贝的文件 第二个是拷贝到远程服务器的目录
使用例子:
最low的方法实现文件批量分发 - 图3
hosts 是本地文件 /tmp 是远程目录