我们的生活是多么美好呀!!!!
全部博文(225)
分类: LINUX
2012-11-22 14:08:32
1、添加sudo的脚本
#cat addsudo.sh
#!/bin/bash
# Owner by Only.liu
# date 2012/11/01
HOSTLIST="/opt/qunar/tools/etc/adduser_hosts.cf"
SUDOFILE="/etc/sudoers"
#
for HOST in `cat ${HOSTLIST}`
do
ssh only.liu@${HOST} 'sudo grep "'$1'" '${SUDOFILE}'' > /dev/null
if [ "$?" != 0 ]
then
echo "User $1 on ${HOST} not have sudo!"
ssh only.liu@${HOST} 'echo "'$1' ALL=(ALL) NOPASSWD:ALL,!/bin/su"' >> ${SUD
OFILE} && echo "User $1 add sudo success!"
else
echo "User $1 already have sudo!"
fi
done
~
2、删除sudo的脚本
#cat delsudo.sh
#!/bin/bash
# Owner by Only.liu
# date 2012/11/01
HOSTLIST="/opt/qunar/tools/etc/adduser_hosts.cf"
SUDOFILE="/etc/sudoers"
#
for HOST in `cat ${HOSTLIST}`
do
ssh only.liu@${HOST} 'sudo grep "'$1'" '${SUDOFILE}'' > /dev/null
if [ "$?" = 0 ]
then
echo "User $1 on ${HOST} have sudo!"
ssh only.liu@${HOST} 'sudo sed -i '/$1/d' '${SUDOFILE}'' && echo "User $1 o
n ${HOST} delete success!"
else
echo "User $1 on ${HOST} already delete sudo!"
fi
done
示例:
若要添加tom的sudo权限:
#sudo ./addsudo.sh tom
若要删除tom的sudo权限:
#sudo ./delsudo.sh tom
注解:
先通过我的帐号only.liu ssh所要加sudo权限的机器,通过grep 命令查看机器的/etc/sudoers文件中是否有同事的帐号,通过退出状态来判断,如果有刚输出User on host have sudo!
;若没有,通过sed命令在最后追加一条,最后输出add success!