作业要求
1、判断web服务是否运行(1、查看进程的方式判断该程序是否运行,2、通过查看端口的方式判断该程序是否运行),如果没有运行,则启动该服务并配置防火墙规则。
2、使用curl命令访问第二题的web服务,看能否正常访问,如果能正常访问,则返回web server is running;如果不能正常访问,返回12状态码。
3、for创建20用户
用户前缀由用户输入
用户初始密码由用户输入
例如:test01,test10
1.查看Web服务器
启动httpd
[root@localhost_1 ~]# systemctl restart httpd
查看进程
[root@localhost_1 ~]# ps -ef | grep httpd
查看端口
[root@localhost_1 ~]# ss -lntup | grep httpd
配置防火墙:可以让防火墙通过80,443端口,还可以关闭防火墙
写shell脚本
#!/bin/bash
#########################
#File name:5.sh
#Version:v1.0
#Email:admin@test.com
#Created time:2023-04-06 20:17:38
#Description:
#########################
ps_num=`ps -ef | grep httpd | grep -v grep | wc -l`
ss_num=`ss -lntup | grep httpd | wc -l`
if [ $ps_num -gt 0 ] && [ $ss_num -gt 0 ]
then
echo "web is running"
else
systemctl restart httpd;
if [ $? -eq 0 ]
then
echo "web starts successfully"
else
echo "web starts unsuccessfully"
fi
systemctl disable firewalld --now
fi
测试
[root@localhost1 test]# bash 5.sh
web is running
[root@localhost1 test]# systemctl stop httpd
[root@localhost1 test]# bash 5.sh
web starts successfully
2.
写shell脚本
#!/bin/bash
#########################
#File name:6.sh
#Version:v1.0
#Email:admin@test.com
#Created time:2023-04-06 20:56:43
#Description:
#########################
curl 10.10.112.182 > /dev/null 2>&1
if [ $? -eq 0 ]
then
echo "web server is running"
else
echo "web server is not running";
exit 12
fi
测试
[root@localhost1 test]# bash 6.sh
web server is running
[root@localhost1 test]# systemctl stop httpd
[root@localhost1 test]# bash 6.sh
web server is not running
3.
使用for循环创建20个用户
#!/bin/bash
#########################
#File name:7.sh
#Version:v1.0
#Email:admin@test.com
#Created time:2023-04-06 21:22:12
#Description:
#########################
read -p "please input user perfix:" name
read -p "please input password:" passwd
for uname in ${name}{01..20}
do
useradd $uname
echo $passwd | passwd --stdin $uname > /dev/null
echo "$uname user is added"
done
测试
[root@localhost1 test]# bash 7.sh
please input user perfix:test
please input password:redhat
test01 user is added
test02 user is added
.......
test19 user is added
test20 user is added