运维中阶shell脚本的相关练习

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

[root@localhost ~]# yum install httpd -y #环境配置
[root@dyh work]# vim 1.sh

 1 ########################
  2 ps_httpd=`ps -ef | grep httpd | grep -v grep | wc -l`
  3 if [[ $ps_httpd > 0 ]];then   4          echo "httpd.service is running."
  5 else
  6         systemctl start httpd
  7         systemctl stop firewalld
  8 fi

测试:
[root@localhost ~]# chmod a+rx 1.sh  #提权
[root@localhost ~]# systemctl stop httpd
[root@localhost ~]# ./1.sh

查看端口方式:

[root@localhost ~]# vim 1.2.sh

  1  ########################
  2  ps_httpd=`ss -lntup | grep -w 80 | wc -l`
  3  if [[ $port80 > 0 ]];then
  4          echo "httpd.service is running."
  5  else
  6          systemctl start httpd
  7          systemctl stop firewalld
  8  fi

[root@localhost ~]# chmod a+rx 1.2.sh
[root@localhost ~]# systemctl stop httpd
[root@localhost ~]# ./1.2.sh


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

[root@localhost ~]# vim 2.sh
 1 ###############################
  2 curl -s 192.168.248.128 > /dev/null
  3 if [[ $? = 0 ]];then
  4         echo "web server is running."
  5         else
  6                 exit 12
  7 fi
[root@localhost ~]# chmod a+rx 2.sh
[root@localhost ~]# systemctl stop httpd
[root@localhost ~]# ./2.sh
[root@localhost ~]# echo $?
12


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

[root@localhost ~]# vim user.sh

 1 #########################
  2 read -p "please input suffix:" pre
  3 read -p "please input your password:" mima
  4 for username in {01..20}
  5 do
  6   id $pre$username &>/dev/null || useradd $pre$username
  7   echo $mima | passwd --stdin $pre$username &> /dev/null
  8 done
[root@localhost ~]# chmod a+rx user.sh
[root@localhost ~]# ./user.sh

please input suffix:jcl
please input your password:123
[root@localhost ~]# cat /etc/passwd | grep jcl*
jcl01:x:1024:1024::/home/jcl01:/bin/bash
jcl02:x:1025:1025::/home/jcl02:/bin/bash
jcl03:x:1026:1026::/home/jcl03:/bin/bash
jcl04:x:1027:1027::/home/jcl04:/bin/bash
jcl05:x:1028:1028::/home/jcl05:/bin/bash
jcl06:x:1029:1029::/home/jcl06:/bin/bash
jcl07:x:1030:1030::/home/jcl07:/bin/bash
jcl08:x:1031:1031::/home/jcl08:/bin/bash
jcl09:x:1032:1032::/home/jcl09:/bin/bash
jcl10:x:1033:1033::/home/jcl10:/bin/bash
jcl11:x:1034:1034::/home/jcl11:/bin/bash
jcl12:x:1035:1035::/home/jcl12:/bin/bash
jcl13:x:1036:1036::/home/jcl13:/bin/bash
jcl14:x:1037:1037::/home/jcl14:/bin/bash
jcl15:x:1038:1038::/home/jcl15:/bin/bash
jcl16:x:1039:1039::/home/jcl16:/bin/bash
jcl17:x:1040:1040::/home/jcl17:/bin/bash
jcl18:x:1041:1041::/home/jcl18:/bin/bash
jcl19:x:1042:1042::/home/jcl19:/bin/bash
jcl20:x:1043:1043::/home/jcl20:/bin/bash

删除所创建的用户:

[root@localhost ~]# vim deluser.sh

 1 ############################
  2 i=1
  3 while [ $i -le 20 ]
  4 do
  5         if [ "$i" -lt 10 ];then
  6                 username=jcl0$i
  7         else
  8                 username=jcl$i
  9         fi
 10         userdel -r $username
 11         let i++
 12 done
[root@localhost ~]# chmod a+rx deluser.sh
[root@localhost ~]# ./deluser.sh
[root@localhost ~]# cat /etc/passwd | grep jcl*
[root@localhost ~]# 
 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值