当前位置: 首页 > article >正文

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/bash
  2 #########################
  3 #File name:demo01.sh
  4 #Version:v1.0
  5 #Email:admin@test.com
  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:admin@test.com
  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:admin@test.com
  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. 


http://www.kler.cn/news/9947.html

相关文章:

  • DC 使用记录
  • 一次性搞懂dBSPL、dBm、dBu、dBV、dBFS的区别!
  • 谈ChatGPT基本信息
  • Mac平台上有哪些好用的常用软件?
  • 软件重构方法
  • Nacos 性能报告
  • 2023-04-14 lua + C动态库交叉debug
  • 逆向入门--何为OEP
  • 故障注入的方法与工具
  • 【GITLab】docker部署GitLab
  • 如何在ubuntu上搭建minio
  • 灌区量测水系统
  • C++ Primer第五版_第十一章习题答案(31~38)
  • 程序员必用的6个代码对比神器附下载地址
  • Linux嵌入式学习之Ubuntu入门(二)磁盘文件介绍及分区、格式化等
  • NumPy 初学者指南中文第三版:1~5
  • 【三十天精通Vue 3】 第三天 Vue 3的组件详解
  • 一位腾讯在职7年测试工程师的心声...
  • 为什么会有JMM?从0到1一次性说清楚
  • Adaptive AUTOSAR——State Management(VRTE 3.0 R21-11)
  • 笔记 | python蓝桥算法复习(预习)基础知识
  • 快排非递归 归并排序
  • spring(七):事务操作
  • docker 安装nocas
  • 亚马逊云科技Amazon Linux 2023正式发布,将为您提供长期支持
  • 使用Xftp连接Windows7虚拟机
  • webgl-图形非矩阵旋转
  • GooglePlay马甲包过审详细流程
  • Java基础(六)面向对象编程(进阶)
  • 2023-04-14 算法面试中常见的查找表问题