前言:
作为一个运维人员,经常会有开发的同事审请一些服务器的sudo权限,而且有同事离职,也需要将sudo权限移除,(开发同事离职之后并不是马上就要将其帐号删除,至少要保存三个月的时间 ),服务器少了还好说,那如果有几十台的话,那就不好办了,自己写了一个简单的脚本,也方便了运维的工作,贴出来给大家分享下,有不对的地方还请大家多指教!
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!