#!/bin/bash
#描述:批量部署mondo客户端
#处理思路
#1.0 判断root用户是否可以登录
#2.0 判断mondev用户是否存在,若存在则先删除该用户(原因是该用户的有问题,无法正常使用)
#3.0 新增mondev用户
#4.0 复制单机部署脚本(mondo_deploy.sh)到各个客户端
#5.0 在各个客户端用mondev用户执行mondo_deploy.sh脚本
usage(){
echo "sh deployMondoBat.sh ipListFile"
}
#判断第一个参数是否是文件
if [ "$1" == "" ] || [ ! -r $1 ]
then
echo "$1 can not found or can not read...."
usage
echo " "
exit
fi
root_user="root"
root_pass="!13YXdgh"
mondev_user="mondev"
mondev_pass="mondev!@#123"
encrypt_pass='$6$rounds=656000$bl/ZiysB.YmZ8.Ox$bRkoU6f2w46YpBgTF83VVQtk04iCIH5XnfRMgA3n4FqoP4kNpIm8lE9DyO7nQ3oMXeZvW4r9Dc7kG5VyrvKRH.'
#日志路径
errlog="log/addusererror.log"
oklog="log/adduserok.log"
current_ip=''
echo ''>$errlog
echo ''>$oklog
#部署
deployBat(){
echo "begin to deploy mondo"
#复制部署文件到客户端的/data/mondev/目录下
err4=`sshpass -p $mondev_pass scp -r -o StrictHostKeyChecking=no /root/zhangb/mondo/mondo_deploy.sh mondev@$current_ip:/data/mondev/`
if [ "$?" == "0" ]
then
#执行客户端的/data/mondev/mondo_deploy.sh 脚本
err5=`sshpass -p $mondev_pass ssh -t -o StrictHostKeyChecking=no mondev@$current_ip "sh /data/mondev/mondo_deploy.sh"`
if [ "$err5" != "" ]
then
echo "$err5"
#对客户端的输出进行过滤,看是否有"successfully started" 启动成功的字符串
skey=`echo $err5 |grep "successfully started"|wc -l`
if [ "$skey" == "0" ]
then
echo "deploy fail.................."
echo "$current_ip \n deploy fail..................\n $err5" >>$errlog
else
echo "deploy success.................."
echo "$current_ip" >> $oklog
fi
else
echo "deploy fail.................."
echo "$current_ip \n deploy fail..................\n $err5" >>$errlog
fi
else
echo "scp /root/zhangb/mondo/mondo_deploy.sh has error........."
echo "$current_ip \n scp /root/zhangb/mondo/mondo_deploy.sh has error......... \n $err4" >>$errlog
fi
}
#新增mondev用户
addMondevUser(){
echo "add mondev user"
#errm3=`sshpass -p $root_pass ssh -o StrictHostKeychecking=no -o ConnectTimeout=5 $root_user@$current_ip "useradd mondev -d /data/mondev -s /bin/bash -p $encrypt_pass"`
#直接设置mondve用户密码时(-p),特殊字符不好处理,所以这里采用先新增用户,后修改密码的方式
errm3=`sshpass -p $root_pass ssh -o StrictHostKeychecking=no -o ConnectTimeout=5 $root_user@$current_ip "useradd mondev -d /data/mondev -s /bin/bash"`
if [ "$?" != "0" ]
then
echo "add mondev user fail..............."
echo "$current_ip \n add mondev user fail..............................\n $errm3" >>$errlog
else
#修改密码
updateMondevPasswd
#部署
deployBat
fi
}
#修改mondev用户的密码
updateMondevPasswd(){
echo "update mondev's old passwd"
errm5=`sshpass -p $root_pass ssh -o StrictHostKeychecking=no -o ConnectTimeout=5 $root_user@$current_ip "echo $mondev_pass | passwd --stdin $mondev_user"`
if [ "$?" != "0" ]
then
echo "update mondev's passwd has error..............."
echo "$current_ip \n update mondev's passwd has error...............\n $errm5" >>$errlog
fi
}
#删除mondev用户,删除mondev用户之前需要先kill掉该用户下的所有进程
delMondevUser(){
echo "begion to delete exist mondev user"
#判断该用户下是否有进行在运行
errp8=`sshpass -p $root_pass ssh -o StrictHostKeyChecking=no $root_user@$current_ip "ps -ef| grep mondev |grep -v grep|wc -l"`
errp1=`echo $errp8|tr -s " "`
if [ "$errp1" != "0" ]
then
#删除该用户的所有进程
errm7=`sshpass -p $root_pass ssh -t -o StrictHostKeyChecking=no $root_user@$current_ip "ps -ef|grep mondev|grep -v grep|awk '{print \\$2}'|xargs kill"`
if [ "$?" == "0" ]
then
echo "delete mondev user's process sucess"
#删除用户
errm6=`sshpass -p $root_pass ssh -o StrictHostKeychecking=no -o ConnectTimeout=5 $root_user@$current_ip "userdel mondev"`
if [ "$?" == "0" ]
then
echo "delete mondev user seccess1"
#新增用户
addMondevUser
else
echo "delete mondev user has error1...........\n $errm6"
echo "$current_ip \n delete mondev user has error1...........\n $errm6" >>$errlog
fi
else
echo "delete mondev user's process has error1.......\n $errm7"
echo "$current_ip \n delete mondev user's process has error1.......\n $errm7" >>$errlog
fi
else
#删除用户
errm6=`sshpass -p $root_pass ssh -o StrictHostKeychecking=no -o ConnectTimeout=5 $root_user@$current_ip "userdel mondev"`
if [ "$?" == "0" ]
then
echo "delete mondev user seccess2"
addMondevUser
else
echo "delete mondev user has error2...........\n $errm6"
echo "$current_ip \n delete mondev user has error2...........\n $errm6" >>$errlog
fi
fi
}
for ip in `cat $1 |awk '{print $1}'`
do
echo "----------$ip----------"
current_ip=$ip
#1.0 check login
#判断当前的root用户是否可以登录
errm=`sshpass -p $root_pass ssh -o StrictHostKeychecking=no -o ConnectTimeout=5 $root_user@$ip "ls"`
if [ "$?" == "0" ]
then
echo "login success"
#check mondev user has exist
#判断mondev用户是否存在
errm2=`sshpass -p $root_pass ssh -o StrictHostKeychecking=no -o ConnectTimeout=5 $root_user@$ip "cat /etc/passwd|grep mondev"`
if [ "$errm2" != "" ]
then
echo "$errm2"
echo "user mondev has exist"
#updateMondevPasswd
#check dir
#获取mondev用户的家目录
mondev_dir=`echo "$errm2"|awk -F ":" '{print $6}'`
#echo "user mondev's dir:$mondev_dir"
#if [ "$mondev_dir" != "/data/mondev" ]
# then
# echo "$ip user mondev's dir is not /data/mondev..............."
# echo "$ip user mondev's dir is not /data/mondev..............." >>$errlog
#else
# deployBat
#fi
delMondevUser
else
echo "user mondev is not exist..............."
addMondevUser
fi
else
echo "login fail..............."
echo "$ip login fail..............." >> $errlog
fi
sleep 1
done
@dianxinguangchang.43F.zhongshanerlu.yuexiuqu.guangzhoushi.guangdongsheng
2016年10月13日 星期四 17时31分
#描述:批量部署mondo客户端
#处理思路
#1.0 判断root用户是否可以登录
#2.0 判断mondev用户是否存在,若存在则先删除该用户(原因是该用户的有问题,无法正常使用)
#3.0 新增mondev用户
#4.0 复制单机部署脚本(mondo_deploy.sh)到各个客户端
#5.0 在各个客户端用mondev用户执行mondo_deploy.sh脚本
usage(){
echo "sh deployMondoBat.sh ipListFile"
}
#判断第一个参数是否是文件
if [ "$1" == "" ] || [ ! -r $1 ]
then
echo "$1 can not found or can not read...."
usage
echo " "
exit
fi
root_user="root"
root_pass="!13YXdgh"
mondev_user="mondev"
mondev_pass="mondev!@#123"
encrypt_pass='$6$rounds=656000$bl/ZiysB.YmZ8.Ox$bRkoU6f2w46YpBgTF83VVQtk04iCIH5XnfRMgA3n4FqoP4kNpIm8lE9DyO7nQ3oMXeZvW4r9Dc7kG5VyrvKRH.'
#日志路径
errlog="log/addusererror.log"
oklog="log/adduserok.log"
current_ip=''
echo ''>$errlog
echo ''>$oklog
#部署
deployBat(){
echo "begin to deploy mondo"
#复制部署文件到客户端的/data/mondev/目录下
err4=`sshpass -p $mondev_pass scp -r -o StrictHostKeyChecking=no /root/zhangb/mondo/mondo_deploy.sh mondev@$current_ip:/data/mondev/`
if [ "$?" == "0" ]
then
#执行客户端的/data/mondev/mondo_deploy.sh 脚本
err5=`sshpass -p $mondev_pass ssh -t -o StrictHostKeyChecking=no mondev@$current_ip "sh /data/mondev/mondo_deploy.sh"`
if [ "$err5" != "" ]
then
echo "$err5"
#对客户端的输出进行过滤,看是否有"successfully started" 启动成功的字符串
skey=`echo $err5 |grep "successfully started"|wc -l`
if [ "$skey" == "0" ]
then
echo "deploy fail.................."
echo "$current_ip \n deploy fail..................\n $err5" >>$errlog
else
echo "deploy success.................."
echo "$current_ip" >> $oklog
fi
else
echo "deploy fail.................."
echo "$current_ip \n deploy fail..................\n $err5" >>$errlog
fi
else
echo "scp /root/zhangb/mondo/mondo_deploy.sh has error........."
echo "$current_ip \n scp /root/zhangb/mondo/mondo_deploy.sh has error......... \n $err4" >>$errlog
fi
}
#新增mondev用户
addMondevUser(){
echo "add mondev user"
#errm3=`sshpass -p $root_pass ssh -o StrictHostKeychecking=no -o ConnectTimeout=5 $root_user@$current_ip "useradd mondev -d /data/mondev -s /bin/bash -p $encrypt_pass"`
#直接设置mondve用户密码时(-p),特殊字符不好处理,所以这里采用先新增用户,后修改密码的方式
errm3=`sshpass -p $root_pass ssh -o StrictHostKeychecking=no -o ConnectTimeout=5 $root_user@$current_ip "useradd mondev -d /data/mondev -s /bin/bash"`
if [ "$?" != "0" ]
then
echo "add mondev user fail..............."
echo "$current_ip \n add mondev user fail..............................\n $errm3" >>$errlog
else
#修改密码
updateMondevPasswd
#部署
deployBat
fi
}
#修改mondev用户的密码
updateMondevPasswd(){
echo "update mondev's old passwd"
errm5=`sshpass -p $root_pass ssh -o StrictHostKeychecking=no -o ConnectTimeout=5 $root_user@$current_ip "echo $mondev_pass | passwd --stdin $mondev_user"`
if [ "$?" != "0" ]
then
echo "update mondev's passwd has error..............."
echo "$current_ip \n update mondev's passwd has error...............\n $errm5" >>$errlog
fi
}
#删除mondev用户,删除mondev用户之前需要先kill掉该用户下的所有进程
delMondevUser(){
echo "begion to delete exist mondev user"
#判断该用户下是否有进行在运行
errp8=`sshpass -p $root_pass ssh -o StrictHostKeyChecking=no $root_user@$current_ip "ps -ef| grep mondev |grep -v grep|wc -l"`
errp1=`echo $errp8|tr -s " "`
if [ "$errp1" != "0" ]
then
#删除该用户的所有进程
errm7=`sshpass -p $root_pass ssh -t -o StrictHostKeyChecking=no $root_user@$current_ip "ps -ef|grep mondev|grep -v grep|awk '{print \\$2}'|xargs kill"`
if [ "$?" == "0" ]
then
echo "delete mondev user's process sucess"
#删除用户
errm6=`sshpass -p $root_pass ssh -o StrictHostKeychecking=no -o ConnectTimeout=5 $root_user@$current_ip "userdel mondev"`
if [ "$?" == "0" ]
then
echo "delete mondev user seccess1"
#新增用户
addMondevUser
else
echo "delete mondev user has error1...........\n $errm6"
echo "$current_ip \n delete mondev user has error1...........\n $errm6" >>$errlog
fi
else
echo "delete mondev user's process has error1.......\n $errm7"
echo "$current_ip \n delete mondev user's process has error1.......\n $errm7" >>$errlog
fi
else
#删除用户
errm6=`sshpass -p $root_pass ssh -o StrictHostKeychecking=no -o ConnectTimeout=5 $root_user@$current_ip "userdel mondev"`
if [ "$?" == "0" ]
then
echo "delete mondev user seccess2"
addMondevUser
else
echo "delete mondev user has error2...........\n $errm6"
echo "$current_ip \n delete mondev user has error2...........\n $errm6" >>$errlog
fi
fi
}
for ip in `cat $1 |awk '{print $1}'`
do
echo "----------$ip----------"
current_ip=$ip
#1.0 check login
#判断当前的root用户是否可以登录
errm=`sshpass -p $root_pass ssh -o StrictHostKeychecking=no -o ConnectTimeout=5 $root_user@$ip "ls"`
if [ "$?" == "0" ]
then
echo "login success"
#check mondev user has exist
#判断mondev用户是否存在
errm2=`sshpass -p $root_pass ssh -o StrictHostKeychecking=no -o ConnectTimeout=5 $root_user@$ip "cat /etc/passwd|grep mondev"`
if [ "$errm2" != "" ]
then
echo "$errm2"
echo "user mondev has exist"
#updateMondevPasswd
#check dir
#获取mondev用户的家目录
mondev_dir=`echo "$errm2"|awk -F ":" '{print $6}'`
#echo "user mondev's dir:$mondev_dir"
#if [ "$mondev_dir" != "/data/mondev" ]
# then
# echo "$ip user mondev's dir is not /data/mondev..............."
# echo "$ip user mondev's dir is not /data/mondev..............." >>$errlog
#else
# deployBat
#fi
delMondevUser
else
echo "user mondev is not exist..............."
addMondevUser
fi
else
echo "login fail..............."
echo "$ip login fail..............." >> $errlog
fi
sleep 1
done
@dianxinguangchang.43F.zhongshanerlu.yuexiuqu.guangzhoushi.guangdongsheng
2016年10月13日 星期四 17时31分