shell练习题

1、判断web服务是否运行(1、查看进程的方式判断该程序是否运行,2、通过查看端口的方式判断该程序是否运行),如果没有运行,则启动该服务并配置防火墙规则。

#1. 基于服务状态配置
#!/bin/bash
#########################
#File name:1.sh
#Version:v1.0
#Email:admin@test.com
#Created time:2023-04-06 19:53:53
#Description:
#########################
systemctl status httpd &> /dev/null
if [ "$?" -ne 0 ];then
	systemctl start httpd
	firewall-cmd --permanent --add--service="http"
	echo "httpd start runing"
else
	echo "httpd is runing"
fi

#2. 基于服务进程配置
#!/bin/bash
  2 #########################
  3 #File name:1.sh
  4 #Version:v1.0
  5 #Email:admin@test.com
  6 #Created time:2023-04-06 19:53:53
  7 #Description:
  8 #########################
  9 num=`ps -ef | grep httpd | wc -l`
 10 if [ "$num" -le 1 ];then
 11         systemctl start httpd                                                                                            
 12         firewall-cmd --permanent --add--service="http"
 13         echo "httpd start runing"
 14 else    
 15         echo "httpd is runing"
 16 fi      

#3. 基于端口配置
#!/bin/bash
  2 #########################
  3 #File name:1.sh
  4 #Version:v1.0
  5 #Email:admin@test.com
  6 #Created time:2023-04-06 19:53:53
  7 #Description:
  8 #########################
  9 num=`ss -lntup | grep 80 | wc -l`
 10 if [ "$num" -eq 0 ];then
 11         systemctl start httpd                                                                                            
 12         firewall-cmd --permanent --add--service="http"
 13         echo "httpd start runing"
 14 else
 15         echo "httpd is runing"
 16 fi

#三种配置测试一致
[root@localhost shell]# curl www.openlab.com 192.168.181.100
curl: (6) Could not resolve host: www.openlab.com
wellcom openlab

2、使用curl命令访问第二题的web服务,看能否正常访问,如果能正常访问,则返回web server is

running;如果不能正常访问,返回12状态码。

#配置如下
[root@localhost shell]# cat 2.sh 
#!/bin/bash
#########################
#File name:2.sh
#Version:v1.0
#Email:admin@test.com
#Created time:2023-04-06 21:31:18
#Description:
#########################
curl www.openlab.com 192.168.181.100 &> /dev/null
if [ "$?" -ne 0 ];then
	exit 12
else
	echo "web server is running"
fi 

#测试如下
[root@localhost shell]# ./2.sh 
web server is running

 3、for创建20用户
用户前缀由用户输入
用户初始密码由用户输入
例如:test01,test10

#配置如下
[root@localhost shell]# cat 3.sh 
#!/bin/bash
#########################
#File name:3.sh
#Version:v1.0
#Email:admin@test.com
#Created time:2023-04-06 21:36:54
#Description:
#########################

for name in `seq -f %01g 20`
do
	read -p "enter one user name:" username
	user_name=$username$name
	useradd $user_name
	passwd $user_name 
		
done

#测试如下
[root@localhost shell]# ./3.sh 
enter one user name:chenbo
更改用户 chenbo1 的密码 。
新的密码: 
无效的密码: 密码少于 8 个字符
重新输入新的密码: 
passwd:所有的身份验证令牌已经成功更新。
enter one user name:^C
[root@localhost shell]# ls /home
chenbo  chenbo1 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

North433

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值