Shell脚本磁盘空间判断,服务状态判断,批量创建用户并设置密码练习

题目要求:

一. 判断当前磁盘剩余空间是否有20G,如果小于20G,则将报警邮件发送给管理员,每天检查一次磁盘剩余空间

1.需要的服务sendmail和mailx

2.用到的命令df

3.awk 'NR=行'{print $列}'表示逮你第几行第几列的内容 

4.mail -s 邮件标题 收件人

5.例行性任务crontab

6.代码

二.判断web服务是否运行

1.先判断是否下载http服务和firewall没有就安装,rpm -qa,yum install

2.为httpd设置防火墙策略

3.通过进程和端口两种方式同时满足才能判断http运行正常,用到&&

4.再通过统计进程和端口信息条数来判断是否开启,wc -l

5.代码

6.测试

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

1.将错误输出输出到/dev/null

2.测试

四.for创建20个用户,用户前缀由用户输入,用户初始密码由用户输入

1.代码

 2.检验


题目要求:

1.判断当前磁盘剩余空间是否有20G,如果小于20G,则将报警邮件发送给管理员,每天检查一次磁盘剩余空间。
​ 2、判断web服务是否运行

(1)查看进程的方式判断该程序是否运行。

(2)通过查看端口的方式判断该程序是否运行),如果没有运行,则启动该服务并配置防火墙规则。
​ 3.使用curl命令访问第二题的web服务,看能否正常访问,如果能正常访问,则返回web server is running;如果不能正常访问,返回12状态码。

4.for创建20个用户,用户前缀由用户输入,用户初始密码由用户输入。

一. 判断当前磁盘剩余空间是否有20G,如果小于20G,则将报警邮件发送给管理员,每天检查一次磁盘剩余空间

1.需要的服务sendmail和mailx

2.用到的命令df

df常用参数 含义
-h

使用易读方式输出

Filesystem   Size  Used Avail Use% Mounted on

-H 以1000为单位而不是1024
-k

以KB为单位

Filesystem    1K-blocks    Used Available Use% Mounted on

-m

以MB为单位

Filesystem    1M-blocks  Used Available Use% Mounted on

3.awk 'NR=行'{print $列}'表示逮你第几行第几列的内容 

4.mail -s 邮件标题 收件人

5.例行性任务crontab

6.代码

[root@redhat9 shellwenjian]# vim ade.sh
#!/bin/bash
mb=`df -m / | awk 'NR==2{print $4}'`
gb=$[mb/1024]
if [ "$gb" -lt 20 ]
then
        echo "您最近的磁盘空间已经小于20G,请及时查看!" |  mail -s "WORNING!" root@localhost
fi
[root@redhat9 shellwenjian]# cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed
0 0 * * * root /shellwenjian/ade.sh

二.判断web服务是否运行

1.先判断是否下载http服务和firewall没有就安装,rpm -qa,yum install

2.为httpd设置防火墙策略

3.通过进程和端口两种方式同时满足才能判断http运行正常,用到&&

4.再通过统计进程和端口信息条数来判断是否开启,wc -l

5.代码

#!/bin/bash
rpm_http=`rpm -qa httpd |wc -l`
if [ $rpm_http -ge 1 ]
then
        systemctl start httpd;
else
        yum install -y httpd;
        systemctl start httpd;
fi
rpm_firewall=`rpm -qa firewalld |wc -l`
if [ $rpm_firewall -ge 1 ]
then
        systemctl start firewalld;
        firewall-cmd --add-service=http --permanent;
else
        yum install -y firewalld;
        systemctl start firewalld;
        firewall-cmd --add-service=http --permanent;
fi
ps_ef=`ps -ef | grep "httpd" | wc -l`
port_web=`ss -lntup | grep -w "80" |  wc -l`
if [ $ps_ef -ge 2 ] && [ $port_web -ge 2 ]
then
        echo "web服务已运行!"
else
        echo "稍后为您开启web服务!"
        systemctl start httpd;
fi
##
#ps_ef=`ps -ef | grep "httpd" | grep -v grep  | wc -l`
#port_web=`ss -lntup | grep -w "80" | grep -v grep | wc -l`
#if [ $ps_ef -ge 1 ] && [ $port_web -ge 1 ]
#then
#        echo "web服务已运行!"
#else
#        echo "稍后为您开启web服务!"
#        systemctl start httpd;
#fi

6.测试

[root@redhat9 shellwenjian]# yum remove httpd firewalld
[root@redhat9 shellwenjian]# bash httpdfirewall.sh
Complete!
Warning: ALREADY_ENABLED: http
success
web服务已运行!
[root@redhat9 shellwenjian]# systemctl status httpd firewalld.service          #状态
● httpd.service - The Apache HTTP Server
     Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
     Active: active (running) since Wed 2023-04-05 18:21:11 CST; 1min 7s ago
       Docs: man:httpd.service(8)
   Main PID: 58339 (httpd)
     Status: "Total requests: 0; Idle/Busy workers 100/0;Requests/sec: 0; Bytes served/sec:   0 B/sec"
      Tasks: 213 (limit: 64977)
     Memory: 33.8M
        CPU: 104ms
     CGroup: /system.slice/httpd.service
             ├─58339 /usr/sbin/httpd -DFOREGROUND
             ├─58341 /usr/sbin/httpd -DFOREGROUND
             ├─58342 /usr/sbin/httpd -DFOREGROUND
             ├─58343 /usr/sbin/httpd -DFOREGROUND
             └─58476 /usr/sbin/httpd -DFOREGROUND

Apr 05 18:21:09 redhat9 systemd[1]: Starting The Apache HTTP Server...
Apr 05 18:21:11 redhat9 httpd[58339]: Server configured, listening on: port 443, port 80
Apr 05 18:21:11 redhat9 systemd[1]: Started The Apache HTTP Server.

● firewalld.service - firewalld - dynamic firewall daemon
     Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
     Active: active (running) since Wed 2023-04-05 18:21:14 CST; 1min 5s ago
       Docs: man:firewalld(1)
   Main PID: 59040 (firewalld)
      Tasks: 2 (limit: 64977)
     Memory: 26.0M
        CPU: 357ms
     CGroup: /system.slice/firewalld.service
             └─59040 /usr/bin/python3 -s /usr/sbin/firewalld --nofork --nopid

Apr 05 18:21:14 redhat9 systemd[1]: Starting firewalld - dynamic firewall daemon...
Apr 05 18:21:14 redhat9 systemd[1]: Started firewalld - dynamic firewall daemon.
Apr 05 18:21:14 redhat9 firewalld[59040]: WARNING: ALREADY_ENABLED: http
[root@redhat9 shellwenjian]# firewall-cmd --list-services    #firewalld
cockpit dhcpv6-client http ssh
[root@redhat9 shellwenjian]# curl 192.168.2.135         #httpd


        
                
                
                
        
        
                
welcome to openlab!!!

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

1.将错误输出输出到/dev/null

#!bin/bash
curl 192.168.2.135 > /dev/null 2>&1
if [ $? -eq 0 ]
then
        echo "web server is running!"
else
        exit 12
fi

2.测试

[root@redhat9 shellwenjian]# bash curl.sh
web server is running!
[root@redhat9 shellwenjian]# systemctl stop httpd.service
[root@redhat9 shellwenjian]# bash curl.sh
[root@redhat9 shellwenjian]# echo $?
12

四.for创建20个用户,用户前缀由用户输入,用户初始密码由用户输入

1.代码

#!bin/bash
read -p "请输入用户前缀和密码:" name password
for i in {1..20}
do
        useradd $name$i;
        echo $password | passwd --stdin $name$i;
done

 2.检验

(1)脚本运行成功

[root@redhat9 shellwenjian]# bash user.sh
请输入用户前缀和密码:ade rhce
useradd: user 'ade1' already exists
Changing password for user ade1.
passwd: all authentication tokens updated successfully.
useradd: user 'ade2' already exists
Changing password for user ade2.
......

(2)用户创建成功 

[root@redhat9 shellwenjian]# cat /etc/passwd | tail -20
ade1:x:1002:1002::/home/ade1:/bin/bash
ade2:x:1003:1003::/home/ade2:/bin/bash
ade3:x:1004:1004::/home/ade3:/bin/bash
ade4:x:1005:1005::/home/ade4:/bin/bash
ade5:x:1006:1006::/home/ade5:/bin/bash
ade6:x:1007:1007::/home/ade6:/bin/bash
ade7:x:1008:1008::/home/ade7:/bin/bash
ade8:x:1009:1009::/home/ade8:/bin/bash
ade9:x:1010:1010::/home/ade9:/bin/bash
ade10:x:1011:1011::/home/ade10:/bin/bash
ade11:x:1012:1012::/home/ade11:/bin/bash
ade12:x:1013:1013::/home/ade12:/bin/bash
ade13:x:1014:1014::/home/ade13:/bin/bash
ade14:x:1015:1015::/home/ade14:/bin/bash
ade15:x:1016:1016::/home/ade15:/bin/bash
ade16:x:1017:1017::/home/ade16:/bin/bash
ade17:x:1018:1018::/home/ade17:/bin/bash
ade18:x:1019:1019::/home/ade18:/bin/bash
ade19:x:1020:1020::/home/ade19:/bin/bash
ade20:x:1021:1021::/home/ade20:/bin/bash

(3) 用户密码检验成功

[root@redhat9 shellwenjian]# su - ade2
[ade2@redhat9 ~]$ su - ade1
Password:
[ade1@redhat9 ~]$
[ade1@redhat9 ~]$ su - ade2
Password:
[ade2@redhat9 ~]$

你可能感兴趣的:(Linux,#,Shell,vim,linux,服务器,运维,shell,redhat9)