最近迁移机房,迁移后手下的服务器会达到153台之多,这要是不写个批量的整合接口,让老夫如何是好,废话不多说,直接上脚本
#!/bin/bash
#作者闫超
#可以理解为运维整合工具,加了函数可以理解为一个功能强大的运维整合命令
#hlep
if [ "$1" == help ]; then
cat /opt/duoyongtu/help.txt
fi
#if [ "$1" == youhua ]; then
# sh /opt/duoyongtu/youhua.sh
#fi
#添加新认证主机
if [ "$1" == addnewrenzheng ]; then
sh /opt/duoyongtu/expect.sh
fi
#针对与web集群批量操作
if [ "$1" == jiquncaozuo ]; then
cat /opt/duoyongtu/fuwuzu.txt|grep $2
if [ "$?" == 0 ]; then
a=`cat /opt/duoyongtu/$2.txt`
for i in $a
do
ssh $i "hostname"
ssh $i $3
done
fi
fi
#添加新组
if [ "$1" == addgroup ]; then
read -p "添加组名称:" b
echo $b
echo $b >> /opt/duoyongtu/fuwuzu.txt
x=""
while true
do
read -p "添加组成员ip地址:" c
echo $c
if [ "$c" == q ]; then
break
else
echo $c |grep -oP '\d'
if [ "$?" -eq 0 ]; then
echo $c >> /opt/duoyongtu/$b.txt
else
echo $c不符合ip规则
fi
fi
done
fi
#删除组
if [ "$1" == delgroup ]; then
read -p "需要删除的组:" d
cat /opt/duoyongtu/fuwuzu.txt|grep $d
if [ "$?" -eq 0 ]; then
sed -i '/'$d'/d' /opt/duoyongtu/fuwuzu.txt
rm -f /opt/duoyongtu/$d.txt
else
echo 没有这个$d组
fi
fi
#删除组中的ip地址
if [ "$1" == delip ]; then
read -p "删除哪个组中的ip:" e
read -p "需要删除的ip地址:" f
sed -i '/'$f'/d' /opt/duoyongtu/$e.txt
if [ "$?" -eq 0 ]; then
echo 成功$f移除从$e组中
fi
fi
#添加组中的ip
if [ "$1" == addip ]; then
read -p "针对于哪个组添加ip:" g
cat /opt/duoyongtu/fuwuzu.txt|grep $g
if [ "$?" -eq 0 ]; then
x=""
while true
do
read -p "添加ip为:" h
if [ "$h" == q ]; then
break
else
echo $h |grep -oP '\d'
if [ "$?" -eq 0 ]; then
echo $h >> /opt/duoyongtu/$g.txt
if [ "$?" -eq 0 ]; then
echo $h添加到$g组中
fi
else
echo ip地址不符合规则,无法添加到$g组中
fi
fi
done
else
echo 没有$g这个组
fi
fi
#
#针对于单台服务器操作
if [ "$1" == one ]; then
ssh $2 $3
fi
#上传
if [ "$1" == upload ]; then
sh /opt/duoyongtu/upload.sh
fi
批量执行命令
sh version1.sh jiquncaozuo 组名 "命令"
批量传输命令
sh version1.sh upload
没有全部贴出 只贴出了核心脚本,本人还是非常推荐大家使用的,如果有需求的朋友们可以找我单聊