需求描述:
1 编写脚本程序用于监测httpd的运行状态,要求如下:
当服务状态失常时在"/var/log/htmon.log"文件中记入日志信息
自动将状态失常的httpd服务重新启动
若重启httpd失败,则尝试重新启动服务器主机
结合crond计划任务服务,每周一至周五每隔15分钟执行一次监测任务
2 编写脚本程序用于批量添加用户,要求如下:
要求提供交互功能,当管理员执行脚本程序时可以根据提示指定需添加的用户数量(少于100)、用户名前缀,并能够设置这些用户的失效时间、初始密码。
用户名统一使用两位数,如使用“01”、“02”、“03“等形式,而不是”1“、”2“、”3“的形式。
编写对应的批量删除用户的脚本,不需要提供交互功能,但是能通过命令行参数指定用户名前缀,执行脚本后删除所有使用了该前缀的用户帐号,但要防止误删除root用户。
具体实现:
监测httpd
#!/bin/bash
service httpd status &> /dev/null
if [ $? -ne 0 ] ; then
echo "httpd server is down.at time:`date`" >> /var/log/htmon.log
service httpd restart
service httpd status &> /dev/null
if [ $? -ne 0 ] ; then
chkconfig --level 2345 httpd on
shutdown -r now
fi
fi
批量添加用户
#!/bin/bash
read -p "the number of users(1-99):" num
read -p "username prefix is:" pre
read -p "expire time is(yyyy-mm-dd):" etime
read -p "initial password is:" pw
i=1
while [ $i -le $num ]
do
if [ $i -lt 10 ] ; then
un="${pre}0$i"
else
un="${pre}$i"
fi
useradd -e $etime $un
echo $pw|passwd --stdin $un&> /dev/null
let i++
done
批量删除用户
#!/bin/bash
if [ $# -le 0 ] ; then
echo "error: the prefix of user hasnot be specified."
echo "usage: $0 nameprefix"
exit 1
fi
tar czvf /root/user.cnf.tar.gz /etc/passwd /etc/shadow /etc/group &> /dev/null
tobedel=`grep "$1" /etc/passwd | cut -d ":" -f 1 |grep -v "root"`
for u in $tobedel
do
userdel -r $u &> /dev/null
done