> 文章列表 > RHCE——shell脚本练习

RHCE——shell脚本练习

RHCE——shell脚本练习

一.实验要求

1、判断web服务是否运行(1、查看进程的方式判断该程序是否运行,2、通过查看端口的方式判断该程序是否运行),如果没有运行,则启动该服务并配置防火墙规则。
​2、使用curl命令访问第二题的web服务,看能否正常访问,如果能正常访问,则返回web server is running;如果不能正常访问,返回12状态码。
3、for创建20用户
用户前缀由用户输入
用户初始密码由用户输入
例如:test01,test10

二.实验过程

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

shell脚本的注释:

#编辑.sh文件时自动生成关于脚本文件说明的注释
[root@localhost ~]# vim /root/.vimrc
autocmd BufNewFile *.py,*.cc,*.sh,*.java exec ":call SetTitle()"
func SetTitle()if expand("%:e") == 'sh'call setline(1,"#!/bin/bash")call setline(2,"#")call setline(3,"#File name:".expand("%"))call setline(4,"#Version:v1.0")call setline(5,"#Email:admin@test.com")call setline(6,"#Created time:".strftime("%F %T"))call setline(7,"#Description:")call setline(8,"#")call setline(9,"")endif
endfunc

编写shell脚本

[root@localhost ll]# mkdir /shell //创建shell目录
[root@localhost shell]# mkdir chap01
[root@localhost shell]# vim chap01/demo01.sh //编写shell脚本1 #!/bin/bash2 #3 #File name:demo01.sh4 #Version:v1.05 #Email:admin@test.com6 #Created time:2023-04-10 19:29:357 #Description:8 #
#查看进程的方式判断该程序是否运行9 num=`ps -ef | grep httpd | grep -v grep | wc -l`10 if [ $num -ge 1 ];then11        echo httpd is running12 else13        echo httpd is not running14        systemctl restart httpd15        systemctl stop firewalld                                                                                                                                                        16 fi#查看端口的方式判断程序是否运行17 num=`netstat -lntup | grep -w 80 | wc -l`18 if [ $num -eq 1 ];then19         echo httpd is running                                                                                                                                                          20 else21         echo httpd is not running22        systemctl restart httpd23         systemctl stop firewalld                                                                                                                                                           24 fi25 

2、使用curl命令访问第二题的web服务,看能否正常访问,如果能正常访问,则返回web server is running;如果不能正常访问,返回12状态码。

[root@localhost chap01]# vim demo02.sh #编写shell脚本1 #!/bin/bash2 #3 #File name:demo02.sh4 #Version:v1.05 #Email:admin@test.com6 #Created time:2023-04-10 20:01:587 #Description:8 #9 curl -s 192.168.126.130 &> /dev/null10 if [ "$?" -eq 0 ];then11         echo "web server is running"12 else13         exit 1214 fi    [root@localhost chap01]# chmod a+x demo02.sh #修改脚本文件的权限为可运行

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

[root@localhost chap01]# vim demo03.sh1 #!/bin/bash2 #3 #File name:demo03.sh4 #Version:v1.05 #Email:admin@test.com6 #Created time:2023-04-10 23:05:437 #Description:8 #9 read -p "please input username:" username10 read -p "please input initial password:" password11 for user in {01..20}12 do13         id $username$user &>/dev/null && echo "user already exists" || useradd $username$user                  #使用id命令来判定用户是否存在    14         echo $password | passwd --stdin $username$user &> /dev/null15 done[root@localhost chap01]# chmod a+x demo03.sh #修改权限         

三.结果测试 

1.

 

2.

 

3.