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:[email protected]")
     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/bash
  2 #########################
  3 #File name:demo01.sh
  4 #Version:v1.0
  5 #Email:[email protected]
  6 #Created time:2023-04-10 19:29:35
  7 #Description:
  8 #########################
#查看进程的方式判断该程序是否运行
  9 num=`ps -ef | grep httpd | grep -v grep | wc -l`
 10 if [ $num -ge 1 ];then
 11        echo httpd is running
 12 else
 13        echo httpd is not running
 14        systemctl restart httpd
 15        systemctl stop firewalld                                                                                                                                                        
 16 fi

#查看端口的方式判断程序是否运行
 17 num=`netstat -lntup | grep -w 80 | wc -l`
 18 if [ $num -eq 1 ];then
 19         echo httpd is running                                                                                                                                                          
 20 else
 21         echo httpd is not running
 22        systemctl restart httpd
 23         systemctl stop firewalld                                                                                                                                                           
 24 fi
 25 



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

[root@localhost chap01]# vim demo02.sh #编写shell脚本
  1 #!/bin/bash
  2 #########################
  3 #File name:demo02.sh
  4 #Version:v1.0
  5 #Email:[email protected]
  6 #Created time:2023-04-10 20:01:58
  7 #Description:
  8 #########################
  9 curl -s 192.168.126.130 &> /dev/null
 10 if [ "$?" -eq 0 ];then
 11         echo "web server is running"
 12 else
 13         exit 12
 14 fi    

[root@localhost chap01]# chmod a+x demo02.sh #修改脚本文件的权限为可运行

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

[root@localhost chap01]# vim demo03.sh

  1 #!/bin/bash
  2 #########################
  3 #File name:demo03.sh
  4 #Version:v1.0
  5 #Email:[email protected]
  6 #Created time:2023-04-10 23:05:43
  7 #Description:
  8 #########################

  9 read -p "please input username:" username
 10 read -p "please input initial password:" password
 11 for user in {01..20}
 12 do
 13         id $username$user &>/dev/null && echo "user already exists" || useradd $username$user                  #使用id命令来判定用户是否存在    
 14         echo $password | passwd --stdin $username$user &> /dev/null
 15 done
 
[root@localhost chap01]# chmod a+x demo03.sh #修改权限         

三.结果测试 

1.

2.

3. 

猜你喜欢

转载自blog.csdn.net/qq_63099085/article/details/130067456