1、判断web服务是否运行(1、查看进程的方式判断该程序是否运行,2、通过查看端口的方式判断该程序是否运行),如果没有运行,则启动该服务并配置防火墙规则
<1>用查看进程
vim test1.sh
#!/bin/bash
num=`ps -ef | grep -c httpd`
if [ $num -ge 1 ];then
echo "httpd is running"
else
echo "httpd not running"
systemctl start httpd &> /dev/null
fi
chmod +x test1.sh
[root@localhost shtest]# ./test1.sh
httpd is running
[root@localhost shtest]# ./test1.sh
httpd not running
<2>用查看端口
vim test2.sh
#!/bin/bash
num=`ss -lntup | grep -cw 80`
if [ $num -ge 1 ];then
echo "httpd is running"
else
echo "httpd not running"
systemctl start httpd &> /dev/null
fi
chmod +x test2.sh
[root@localhost shtest]# ./test2.sh
httpd is running
[root@localhost shtest]# ./test2.sh
httpd not running
2、使用curl命令访问第二题的web服务,看能否正常访问,如果能正常访问,则返回web server is running;如果不能正常访问,返回12状态码
vim test3.sh
curl http://192.168.17.128 &> /dev/null
num=`echo $?`
if [ $num -eq 0 ];then
echo "web server is running"
else
echo "web server not running"
exit 12
fi
chmod +x test2.sh
./test3.sh
[root@localhost shtest]# ./test3.sh
web server not running
[root@localhost shtest]# echo $?
12
[root@localhost shtest]# ./test3.sh
web server is running
3、for创建20用户
用户前缀由用户输入
用户初始密码由用户输入
例如:test01,test10
[root@localhost shtest]# vim test4.sh
#!/bin/bash
read -p "please input name:" a
read -p "please input sercert:" b
for i in {01..20}
do
if [ $i -lt 10 ]
then
user="$a""$i"
else
user="$a""$i"
fi
if ! id -u $user &> /dev/null
then
useradd $user
echo "$b" | passwd --stdin $user &> /dev/null
else
echo "$user is exists..."
fi
done
[root@localhost shtest]# ./test4.sh
please input name:ll
please input sercert:123