1.vim ip.sh(获取/etc/hosts配置下的所有主机名称/ip)
#!/bin/bash
# 循环读取/etc/hosts的每一行,用数组存主机名
hosts=()
i=0
while read ipline;
do
ip=$(echo $ipline | awk '{print $1}')
devname=$(echo $ipline | awk '{print $2}')
#devname=$(awk '{print $2}')
#if [[ $hosts ]];then
#hosts=$hosts" "$devname
#else
#hosts=$devname
#fi
hosts[$i]=$devname
let i++
#echo ${hosts[*]}, $i
#echo "$devname"
# sleep 1
done < <(cat /etc/hosts|grep -v '^#'|grep '\<[0-9][0-9][0-9]\>')
echo ${hosts[*]}
2.vim xsync,xsync脚本如下,若想全局使用可考虑将xsync放在/usr/local/bin目录下
#!/bin/bash
#1 获取输入参数个数,如果没有参数,直接退出
pcount=$#
if((pcount==0)); then
echo no args;
exit;
fi
#2 获取文件名称
p1=$1
fname=`basename $p1`
echo fname=$fname
#3 获取上级目录到绝对路径
pdir=`cd -P $(dirname $p1); pwd`
echo pdir=$pdir
#4 获取当前用户名称
user=`whoami`
. /usr/local/bin/ip.sh>hosts
echo ${hosts[*]
#5 循环
for host in ${hosts[*]}; do
echo -------------------$pdir/$fname $user@$host$pdir--------------
rsync -rvl $pdir/$fname $user@$host:$pdir
done