目录
一、题目要求
1、判断web服务是否运行(1、查看进程的方式判断该程序是否运行,2、通过查看端口的方式判断该程序是否运行),如果没有运行,则启动该服务并配置防火墙规则。
2、使用curl命令访问第二题的web服务,看能否正常访问,如果能正常访问,则返回web server is running;如果不能正常访问,返回12状态码。
3、for创建20用户
用户前缀由用户输入
用户初始密码由用户输入
例如:test01,test10
二、编写脚本
[root@localhost red]# cd /shell
[root@localhost shell]# mkdir chap02
[root@localhost chap02]# vim dem01.sh
[root@localhost chap02]# cat dem01.sh
ps -ef | grep httpd > /dev/null && ss -lntup | grep 80 > /dev/null
if [[ $? = 0 ]];then
echo "httpd is running"
exit;
else
systemctl start httpd
systemctl stop firewalld
echo "httpd is start"
fi
三、设置权限
[root@localhost chap02]# chmod a+rx dem01.sh
四、测试
[root@localhost chap02]# ./dem01.sh
httpd is running
[root@localhost chap02]# systemctl stop httpd
[root@localhost chap02]# ./dem01.sh
httpd is start
五、编写脚本
curl -s 192.168.40.135 &> /dev/null
if [[ $? = 0 ]];then
echo "web server is running"
else
echo "web server is stop"
exit 12
fi
六、设置权限
[root@localhost chap02]# chmod a+rx dem02.sh
七、测试
[root@localhost chap02]# systemctl stop httpd
[root@localhost chap02]# ./dem02.sh
web server is stop
[root@localhost chap02]# echo $?
12
[root@localhost chap02]# systemctl restart httpd
[root@localhost chap02]# ./dem02.sh
web server is running
八、编写脚本
[root@localhost chap02]# vim dem03.sh
[root@localhost chap02]# cat dem03.sh
read -p "please input user prefix:" per
read -p "please input your password:" mima
for number in {01..20}
do
id $per$number &> /dev/null || useradd $per$number
echo $mima | password --stdin $mima$number &> /dev/null
done
九、设置权限
[root@localhost chap02]# chmod a+rx dem03.sh
十、测试
[root@localhost chap02]# ./dem03.sh
please input user prefix:tt
please input your password:123
[root@localhost chap02]# id tt01
用户id=1041(tt01) 组id=1041(tt01) 组=1041(tt01)