Linux学习第八周


一、显示统计占用系统内存最多的进程,并排序;

##第一种方式
[root@localhost ~]# ps aux --sort=-%mem | head -5
USER        PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root       6531  0.5  1.0 573820 19152 ?        Ssl  13:16   0:00 /usr/bin/python2 -Es /usr/sbin/tuned -l -P
root       6215  0.3  0.5 554064 10944 ?        Ssl  13:16   0:00 /usr/sbin/NetworkManager --no-daemon
polkitd    6223  0.1  0.5 612228 10080 ?        Ssl  13:16   0:00 /usr/lib/polkit-1/polkitd --no-debug
root      16296  0.2  0.3 161692  6628 ?        Ss   13:17   0:00 sshd: root@pts/0
##第二种方式
[root@localhost ~]# ps aux --sort=-rss | head -5
USER        PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root       6531  0.4  1.0 573820 19152 ?        Ssl  13:16   0:00 /usr/bin/python2 -Es /usr/sbin/tuned -l -P
root       6215  0.2  0.5 554064 10944 ?        Ssl  13:16   0:00 /usr/sbin/NetworkManager --no-daemon
polkitd    6223  0.0  0.5 612228 10080 ?        Ssl  13:16   0:00 /usr/lib/polkit-1/polkitd --no-debug
root      16296  0.1  0.3 161692  6692 ?        Ss   13:17   0:00 sshd: root@pts/0
##第三种方式
[root@localhost ~]# ps aux --sort=-rssize | head -5
USER        PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root       6531  0.3  1.0 573820 19152 ?        Ssl  13:16   0:00 /usr/bin/python2 -Es /usr/sbin/tuned -l -P
root       6215  0.2  0.5 554064 10944 ?        Ssl  13:16   0:00 /usr/sbin/NetworkManager --no-daemon
polkitd    6223  0.0  0.5 612228 10080 ?        Ssl  13:16   0:00 /usr/lib/polkit-1/polkitd --no-debug
root      16296  0.1  0.3 161692  6692 ?        Ss   13:17   0:00 sshd: root@pts/0
##第四种方式
[root@localhost ~]# ps aux --sort=-rsz | head -5
USER        PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root       6531  0.3  1.0 573820 19152 ?        Ssl  13:16   0:00 /usr/bin/python2 -Es /usr/sbin/tuned -l -P
root       6215  0.1  0.5 554064 10944 ?        Ssl  13:16   0:00 /usr/sbin/NetworkManager --no-daemon
polkitd    6223  0.0  0.5 612228 10080 ?        Ssl  13:16   0:00 /usr/lib/polkit-1/polkitd --no-debug
root      16296  0.1  0.3 161692  6692 ?        Ss   13:17   0:00 sshd: root@pts/0
##第五种方式
[root@localhost ~]# ps aux | sort -k4nr | head -5
root       6531  0.3  1.0 573820 19152 ?        Ssl  13:16   0:00 /usr/bin/python2 -Es /usr/sbin/tuned -l -P
polkitd    6223  0.0  0.5 612228 10080 ?        Ssl  13:16   0:00 /usr/lib/polkit-1/polkitd --no-debug
root       6215  0.1  0.5 554064 10944 ?        Ssl  13:16   0:00 /usr/sbin/NetworkManager --no-daemon
root      16296  0.1  0.3 161692  6692 ?        Ss   13:17   0:00 sshd: root@pts/0
root      16298  0.0  0.3 161348  6268 ?        Ss   13:17   0:00 sshd: root@notty
##第六种方式
[root@localhost ~]# top    然后敲击M键即可
top - 13:24:21 up 8 min,  1 user,  load average: 0.01, 0.08, 0.06
Tasks: 118 total,   3 running, 115 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.0 us,  0.1 sy,  0.0 ni, 99.9 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  1863252 total,  1572832 free,    98684 used,   191736 buff/cache
KiB Swap:  2097148 total,  2097148 free,        0 used.  1584068 avail Mem

   PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
  6531 root      20   0  573820  19152   6024 S   0.0  1.0   0:00.64 tuned
  6215 root      20   0  554064  10944   6840 S   0.0  0.6   0:00.33 NetworkManager
  6223 polkitd   20   0  612228  10080   4676 S   0.0  0.5   0:00.13 polkitd
 16296 root      20   0  161692   6692   5152 S   0.0  0.4   0:00.17 sshd
 16298 root      20   0  161348   6268   4884 S   0.0  0.3   0:00.05 sshd
  6532 root      20   0  216408   4652   3460 S   0.0  0.2   0:00.43 rsyslogd
  6534 root      20   0  112756   4320   3292 S   0.0  0.2   0:00.02 sshd
  6786 postfix   20   0   89716   4092   3088 S   0.0  0.2   0:00.01 qmgr
  6785 postfix   20   0   89648   4060   3064 S   0.0  0.2   0:00.01 pickup

二、编写脚本,使用for和while分别实现192.168.1.0/24网段内,地址是否能够ping通,若ping通则输出"success!",若ping不通则输出"fail!";

1、使用for循环实现;
[root@localhost ~]# mkdir /usr/sh
[root@localhost ~]# cd /usr/sh
[root@localhost sh]# cat ip_check_for.sh

#!/bin/sh
NETWORKID=192.168.1.
for HOSTID in {1..254};do
{
        if /bin/ping -c1 -W1 $NETWORKID$HOSTID >/dev/null ;then
                echo "$NETWORKID$HOSTID is success!"
        else
                echo "$NETWORKID$HOSTID is fail!"
        fi
} &   ###并发执行
done
wait
[root@localhost sh]# vim ip_check_for.sh
[root@localhost sh]# sh ip_check_for.sh
192.168.1.1 is success!
192.168.1.111 is success!
192.168.1.2 is fail!
192.168.1.6 is fail!
192.168.1.3 is fail!
192.168.1.5 is fail!
192.168.1.7 is fail!
192.168.1.10 is fail!
192.168.1.16 is fail!
192.168.1.9 is fail!
192.168.1.12 is fail!
192.168.1.17 is fail!
192.168.1.18 is fail!
192.168.1.14 is fail!
192.168.1.20 is fail!
192.168.1.27 is fail!
192.168.1.30 is fail!
192.168.1.23 is fail!
192.168.1.11 is fail!
192.168.1.31 is fail!
192.168.1.24 is fail!
192.168.1.35 is fail!
192.168.1.26 is fail!
192.168.1.39 is fail!
192.168.1.29 is fail!
192.168.1.37 is fail!
192.168.1.32 is fail!
192.168.1.43 is fail!
192.168.1.19 is fail!
192.168.1.21 is fail!
192.168.1.38 is fail!
192.168.1.8 is fail!
192.168.1.44 is fail!
192.168.1.4 is fail!
192.168.1.41 is fail!
192.168.1.13 is fail!
192.168.1.36 is fail!
192.168.1.22 is fail!
192.168.1.47 is fail!
192.168.1.49 is fail!
192.168.1.48 is fail!
192.168.1.15 is fail!
192.168.1.28 is fail!
192.168.1.34 is fail!
192.168.1.25 is fail!
192.168.1.40 is fail!
192.168.1.50 is fail!
192.168.1.33 is fail!
192.168.1.46 is fail!
192.168.1.51 is fail!
192.168.1.54 is fail!
192.168.1.53 is fail!
192.168.1.57 is fail!
192.168.1.59 is fail!
192.168.1.66 is fail!
192.168.1.73 is fail!
192.168.1.76 is fail!
192.168.1.64 is fail!
192.168.1.60 is fail!
192.168.1.67 is fail!
192.168.1.80 is fail!
192.168.1.63 is fail!
192.168.1.61 is fail!
192.168.1.71 is fail!
192.168.1.81 is fail!
192.168.1.56 is fail!
192.168.1.68 is fail!
192.168.1.65 is fail!
192.168.1.88 is fail!
192.168.1.52 is fail!
192.168.1.42 is fail!
192.168.1.90 is fail!
192.168.1.69 is fail!
192.168.1.72 is fail!
192.168.1.55 is fail!
192.168.1.62 is fail!
192.168.1.45 is fail!
192.168.1.84 is fail!
192.168.1.95 is fail!
192.168.1.99 is fail!
192.168.1.77 is fail!
192.168.1.58 is fail!
192.168.1.75 is fail!
192.168.1.79 is fail!
192.168.1.85 is fail!
192.168.1.82 is fail!
192.168.1.70 is fail!
192.168.1.86 is fail!
192.168.1.91 is fail!
192.168.1.93 is fail!
192.168.1.94 is fail!
192.168.1.112 is fail!
192.168.1.83 is fail!
192.168.1.104 is fail!
192.168.1.115 is fail!
192.168.1.96 is fail!
192.168.1.87 is fail!
192.168.1.100 is fail!
192.168.1.78 is fail!
192.168.1.74 is fail!
192.168.1.116 is fail!
192.168.1.106 is fail!
192.168.1.89 is fail!
192.168.1.131 is fail!
192.168.1.119 is fail!
192.168.1.97 is fail!
192.168.1.105 is fail!
192.168.1.101 is fail!
192.168.1.121 is fail!
192.168.1.113 is fail!
192.168.1.103 is fail!
192.168.1.92 is fail!
192.168.1.120 is fail!
192.168.1.98 is fail!
192.168.1.107 is fail!
192.168.1.130 is fail!
192.168.1.122 is fail!
192.168.1.109 is fail!
192.168.1.117 is fail!
192.168.1.114 is fail!
192.168.1.108 is fail!
192.168.1.124 is fail!
192.168.1.118 is fail!
192.168.1.136 is fail!
192.168.1.133 is fail!
192.168.1.127 is fail!
192.168.1.123 is fail!
192.168.1.137 is fail!
192.168.1.134 is fail!
192.168.1.139 is fail!
192.168.1.129 is fail!
192.168.1.102 is fail!
192.168.1.125 is fail!
192.168.1.110 is fail!
192.168.1.140 is fail!
192.168.1.132 is fail!
192.168.1.128 is fail!
192.168.1.147 is fail!
192.168.1.142 is fail!
192.168.1.150 is fail!
192.168.1.143 is fail!
192.168.1.146 is fail!
192.168.1.144 is fail!
192.168.1.141 is fail!
192.168.1.152 is fail!
192.168.1.154 is fail!
192.168.1.145 is fail!
192.168.1.156 is fail!
192.168.1.155 is fail!
192.168.1.135 is fail!
192.168.1.126 is fail!
192.168.1.149 is fail!
192.168.1.158 is fail!
192.168.1.138 is fail!
192.168.1.153 is fail!
192.168.1.148 is fail!
192.168.1.151 is fail!
192.168.1.159 is fail!
192.168.1.157 is fail!
192.168.1.166 is fail!
192.168.1.165 is fail!
192.168.1.186 is fail!
192.168.1.163 is fail!
192.168.1.161 is fail!
192.168.1.188 is fail!
192.168.1.164 is fail!
192.168.1.171 is fail!
192.168.1.189 is fail!
192.168.1.172 is fail!
192.168.1.168 is fail!
192.168.1.167 is fail!
192.168.1.179 is fail!
192.168.1.183 is fail!
192.168.1.170 is fail!
192.168.1.185 is fail!
192.168.1.173 is fail!
192.168.1.177 is fail!
192.168.1.192 is fail!
192.168.1.169 is fail!
192.168.1.180 is fail!
192.168.1.209 is fail!
192.168.1.176 is fail!
192.168.1.181 is fail!
192.168.1.190 is fail!
192.168.1.162 is fail!
192.168.1.178 is fail!
192.168.1.200 is fail!
192.168.1.182 is fail!
192.168.1.174 is fail!
192.168.1.184 is fail!
192.168.1.201 is fail!
192.168.1.187 is fail!
192.168.1.205 is fail!
192.168.1.196 is fail!
192.168.1.191 is fail!
192.168.1.203 is fail!
192.168.1.213 is fail!
192.168.1.160 is fail!
192.168.1.193 is fail!
192.168.1.175 is fail!
192.168.1.195 is fail!
192.168.1.206 is fail!
192.168.1.212 is fail!
192.168.1.204 is fail!
192.168.1.210 is fail!
192.168.1.207 is fail!
192.168.1.199 is fail!
192.168.1.216 is fail!
192.168.1.208 is fail!
192.168.1.194 is fail!
192.168.1.198 is fail!
192.168.1.197 is fail!
192.168.1.223 is fail!
192.168.1.214 is fail!
192.168.1.234 is fail!
192.168.1.225 is fail!
192.168.1.211 is fail!
192.168.1.215 is fail!
192.168.1.247 is fail!
192.168.1.202 is fail!
192.168.1.226 is fail!
192.168.1.217 is fail!
192.168.1.219 is fail!
192.168.1.233 is fail!
192.168.1.237 is fail!
192.168.1.218 is fail!
192.168.1.241 is fail!
192.168.1.220 is fail!
192.168.1.243 is fail!
192.168.1.227 is fail!
192.168.1.229 is fail!
192.168.1.231 is fail!
192.168.1.242 is fail!
192.168.1.244 is fail!
192.168.1.238 is fail!
192.168.1.228 is fail!
192.168.1.235 is fail!
192.168.1.248 is fail!
192.168.1.246 is fail!
192.168.1.232 is fail!
192.168.1.254 is fail!
192.168.1.221 is fail!
192.168.1.230 is fail!
192.168.1.249 is fail!
192.168.1.252 is fail!
192.168.1.224 is fail!
192.168.1.222 is fail!
192.168.1.245 is fail!
192.168.1.240 is fail!
192.168.1.251 is fail!
192.168.1.250 is fail!
192.168.1.253 is fail!
192.168.1.236 is fail!
192.168.1.239 is fail!
2、使用while循环实现;
##生成要扫描的IP列表
[root@localhost sh]# echo 192.168.1.{1..254} | tr -s " " "\n" > /usr/sh/iplist.txt
[root@localhost sh]# cat iplist.txt
192.168.1.1
192.168.1.2
192.168.1.3
192.168.1.4
192.168.1.5
192.168.1.6
192.168.1.7
192.168.1.8
192.168.1.9
192.168.1.10
192.168.1.11
192.168.1.12
192.168.1.13
192.168.1.14
192.168.1.15
192.168.1.16
192.168.1.17
192.168.1.18
192.168.1.19
192.168.1.20
192.168.1.21
192.168.1.22
192.168.1.23
192.168.1.24
192.168.1.25
192.168.1.26
192.168.1.27
192.168.1.28
192.168.1.29
192.168.1.30
192.168.1.31
192.168.1.32
192.168.1.33
192.168.1.34
192.168.1.35
192.168.1.36
192.168.1.37
192.168.1.38
192.168.1.39
192.168.1.40
192.168.1.41
192.168.1.42
192.168.1.43
192.168.1.44
192.168.1.45
192.168.1.46
192.168.1.47
192.168.1.48
192.168.1.49
192.168.1.50
192.168.1.51
192.168.1.52
192.168.1.53
192.168.1.54
192.168.1.55
192.168.1.56
192.168.1.57
192.168.1.58
192.168.1.59
192.168.1.60
192.168.1.61
192.168.1.62
192.168.1.63
192.168.1.64
192.168.1.65
192.168.1.66
192.168.1.67
192.168.1.68
192.168.1.69
192.168.1.70
192.168.1.71
192.168.1.72
192.168.1.73
192.168.1.74
192.168.1.75
192.168.1.76
192.168.1.77
192.168.1.78
192.168.1.79
192.168.1.80
192.168.1.81
192.168.1.82
192.168.1.83
192.168.1.84
192.168.1.85
192.168.1.86
192.168.1.87
192.168.1.88
192.168.1.89
192.168.1.90
192.168.1.91
192.168.1.92
192.168.1.93
192.168.1.94
192.168.1.95
192.168.1.96
192.168.1.97
192.168.1.98
192.168.1.99
192.168.1.100
192.168.1.101
192.168.1.102
192.168.1.103
192.168.1.104
192.168.1.105
192.168.1.106
192.168.1.107
192.168.1.108
192.168.1.109
192.168.1.110
192.168.1.111
192.168.1.112
192.168.1.113
192.168.1.114
192.168.1.115
192.168.1.116
192.168.1.117
192.168.1.118
192.168.1.119
192.168.1.120
192.168.1.121
192.168.1.122
192.168.1.123
192.168.1.124
192.168.1.125
192.168.1.126
192.168.1.127
192.168.1.128
192.168.1.129
192.168.1.130
192.168.1.131
192.168.1.132
192.168.1.133
192.168.1.134
192.168.1.135
192.168.1.136
192.168.1.137
192.168.1.138
192.168.1.139
192.168.1.140
192.168.1.141
192.168.1.142
192.168.1.143
192.168.1.144
192.168.1.145
192.168.1.146
192.168.1.147
192.168.1.148
192.168.1.149
192.168.1.150
192.168.1.151
192.168.1.152
192.168.1.153
192.168.1.154
192.168.1.155
192.168.1.156
192.168.1.157
192.168.1.158
192.168.1.159
192.168.1.160
192.168.1.161
192.168.1.162
192.168.1.163
192.168.1.164
192.168.1.165
192.168.1.166
192.168.1.167
192.168.1.168
192.168.1.169
192.168.1.170
192.168.1.171
192.168.1.172
192.168.1.173
192.168.1.174
192.168.1.175
192.168.1.176
192.168.1.177
192.168.1.178
192.168.1.179
192.168.1.180
192.168.1.181
192.168.1.182
192.168.1.183
192.168.1.184
192.168.1.185
192.168.1.186
192.168.1.187
192.168.1.188
192.168.1.189
192.168.1.190
192.168.1.191
192.168.1.192
192.168.1.193
192.168.1.194
192.168.1.195
192.168.1.196
192.168.1.197
192.168.1.198
192.168.1.199
192.168.1.200
192.168.1.201
192.168.1.202
192.168.1.203
192.168.1.204
192.168.1.205
192.168.1.206
192.168.1.207
192.168.1.208
192.168.1.209
192.168.1.210
192.168.1.211
192.168.1.212
192.168.1.213
192.168.1.214
192.168.1.215
192.168.1.216
192.168.1.217
192.168.1.218
192.168.1.219
192.168.1.220
192.168.1.221
192.168.1.222
192.168.1.223
192.168.1.224
192.168.1.225
192.168.1.226
192.168.1.227
192.168.1.228
192.168.1.229
192.168.1.230
192.168.1.231
192.168.1.232
192.168.1.233
192.168.1.234
192.168.1.235
192.168.1.236
192.168.1.237
192.168.1.238
192.168.1.239
192.168.1.240
192.168.1.241
192.168.1.242
192.168.1.243
192.168.1.244
192.168.1.245
192.168.1.246
192.168.1.247
192.168.1.248
192.168.1.249
192.168.1.250
192.168.1.251
192.168.1.252
192.168.1.253
192.168.1.254

[root@localhost sh]# chmod +x ip_check_while.sh
[root@localhost sh]# sh ip_check_while.sh
192.168.1.1 is success!
192.168.1.111 is success!
192.168.1.20 is fail!
192.168.1.8 is fail!
192.168.1.23 is fail!
192.168.1.9 is fail!
192.168.1.11 is fail!
192.168.1.10 is fail!
192.168.1.12 is fail!
192.168.1.14 is fail!
192.168.1.13 is fail!
192.168.1.21 is fail!
192.168.1.7 is fail!
192.168.1.17 is fail!
192.168.1.22 is fail!
192.168.1.2 is fail!
192.168.1.5 is fail!
192.168.1.28 is fail!
192.168.1.3 is fail!
192.168.1.4 is fail!
192.168.1.29 is fail!
192.168.1.19 is fail!
192.168.1.66 is fail!
192.168.1.27 is fail!
192.168.1.39 is fail!
192.168.1.32 is fail!
192.168.1.25 is fail!
192.168.1.40 is fail!
192.168.1.16 is fail!
192.168.1.31 is fail!
192.168.1.68 is fail!
192.168.1.33 is fail!
192.168.1.15 is fail!
192.168.1.43 is fail!
192.168.1.58 is fail!
192.168.1.62 is fail!
192.168.1.35 is fail!
192.168.1.53 is fail!
192.168.1.44 is fail!
192.168.1.36 is fail!
192.168.1.54 is fail!
192.168.1.45 is fail!
192.168.1.37 is fail!
192.168.1.47 is fail!
192.168.1.67 is fail!
192.168.1.46 is fail!
192.168.1.18 is fail!
192.168.1.63 is fail!
192.168.1.30 is fail!
192.168.1.24 is fail!
192.168.1.42 is fail!
192.168.1.38 is fail!
192.168.1.80 is fail!
192.168.1.60 is fail!
192.168.1.52 is fail!
192.168.1.41 is fail!
192.168.1.76 is fail!
192.168.1.56 is fail!
192.168.1.49 is fail!
192.168.1.51 is fail!
192.168.1.48 is fail!
192.168.1.72 is fail!
192.168.1.70 is fail!
192.168.1.59 is fail!
192.168.1.75 is fail!
192.168.1.78 is fail!
192.168.1.6 is fail!
192.168.1.61 is fail!
192.168.1.77 is fail!
192.168.1.65 is fail!
192.168.1.73 is fail!
192.168.1.74 is fail!
192.168.1.79 is fail!
192.168.1.83 is fail!
192.168.1.55 is fail!
192.168.1.64 is fail!
192.168.1.85 is fail!
192.168.1.34 is fail!
192.168.1.71 is fail!
192.168.1.81 is fail!
192.168.1.69 is fail!
192.168.1.84 is fail!
192.168.1.57 is fail!
192.168.1.50 is fail!
192.168.1.82 is fail!
192.168.1.87 is fail!
192.168.1.26 is fail!
192.168.1.88 is fail!
192.168.1.91 is fail!
192.168.1.92 is fail!
192.168.1.86 is fail!
192.168.1.93 is fail!
192.168.1.89 is fail!
192.168.1.95 is fail!
192.168.1.94 is fail!
192.168.1.98 is fail!
192.168.1.90 is fail!
192.168.1.96 is fail!
192.168.1.102 is fail!
192.168.1.99 is fail!
192.168.1.104 is fail!
192.168.1.105 is fail!
192.168.1.103 is fail!
192.168.1.97 is fail!
192.168.1.100 is fail!
192.168.1.108 is fail!
192.168.1.110 is fail!
192.168.1.101 is fail!
192.168.1.107 is fail!
192.168.1.106 is fail!
192.168.1.112 is fail!
192.168.1.113 is fail!
192.168.1.109 is fail!
192.168.1.115 is fail!
192.168.1.119 is fail!
192.168.1.114 is fail!
192.168.1.116 is fail!
192.168.1.117 is fail!
192.168.1.122 is fail!
192.168.1.118 is fail!
192.168.1.121 is fail!
192.168.1.123 is fail!
192.168.1.126 is fail!
192.168.1.120 is fail!
192.168.1.129 is fail!
192.168.1.127 is fail!
192.168.1.124 is fail!
192.168.1.130 is fail!
192.168.1.128 is fail!
192.168.1.131 is fail!
192.168.1.133 is fail!
192.168.1.125 is fail!
192.168.1.132 is fail!
192.168.1.135 is fail!
192.168.1.134 is fail!
192.168.1.136 is fail!
192.168.1.141 is fail!
192.168.1.139 is fail!
192.168.1.140 is fail!
192.168.1.138 is fail!
192.168.1.142 is fail!
192.168.1.137 is fail!
192.168.1.145 is fail!
192.168.1.143 is fail!
192.168.1.146 is fail!
192.168.1.149 is fail!
192.168.1.144 is fail!
192.168.1.148 is fail!
192.168.1.150 is fail!
192.168.1.154 is fail!
192.168.1.151 is fail!
192.168.1.155 is fail!
192.168.1.147 is fail!
192.168.1.153 is fail!
192.168.1.156 is fail!
192.168.1.160 is fail!
192.168.1.152 is fail!
192.168.1.157 is fail!
192.168.1.158 is fail!
192.168.1.163 is fail!
192.168.1.161 is fail!
192.168.1.164 is fail!
192.168.1.169 is fail!
192.168.1.159 is fail!
192.168.1.168 is fail!
192.168.1.167 is fail!
192.168.1.166 is fail!
192.168.1.162 is fail!
192.168.1.165 is fail!
192.168.1.172 is fail!
192.168.1.174 is fail!
192.168.1.175 is fail!
192.168.1.173 is fail!
192.168.1.177 is fail!
192.168.1.171 is fail!
192.168.1.176 is fail!
192.168.1.178 is fail!
192.168.1.179 is fail!
192.168.1.170 is fail!
192.168.1.181 is fail!
192.168.1.180 is fail!
192.168.1.183 is fail!
192.168.1.184 is fail!
192.168.1.182 is fail!
192.168.1.185 is fail!
192.168.1.186 is fail!
192.168.1.189 is fail!
192.168.1.192 is fail!
192.168.1.188 is fail!
192.168.1.191 is fail!
192.168.1.190 is fail!
192.168.1.187 is fail!
192.168.1.194 is fail!
192.168.1.193 is fail!
192.168.1.195 is fail!
192.168.1.199 is fail!
192.168.1.198 is fail!
192.168.1.196 is fail!
192.168.1.201 is fail!
192.168.1.204 is fail!
192.168.1.200 is fail!
192.168.1.205 is fail!
192.168.1.203 is fail!
192.168.1.197 is fail!
192.168.1.207 is fail!
192.168.1.202 is fail!
192.168.1.209 is fail!
192.168.1.206 is fail!
192.168.1.208 is fail!
192.168.1.211 is fail!
192.168.1.213 is fail!
192.168.1.212 is fail!
192.168.1.216 is fail!
192.168.1.214 is fail!
192.168.1.217 is fail!
192.168.1.210 is fail!
192.168.1.222 is fail!
192.168.1.219 is fail!
192.168.1.215 is fail!
192.168.1.218 is fail!
192.168.1.223 is fail!
192.168.1.221 is fail!
192.168.1.220 is fail!
192.168.1.224 is fail!
192.168.1.228 is fail!
192.168.1.225 is fail!
192.168.1.226 is fail!
192.168.1.227 is fail!
192.168.1.231 is fail!
192.168.1.229 is fail!
192.168.1.232 is fail!
192.168.1.230 is fail!
192.168.1.234 is fail!
192.168.1.238 is fail!
192.168.1.233 is fail!
192.168.1.239 is fail!
192.168.1.235 is fail!
192.168.1.236 is fail!
192.168.1.241 is fail!
192.168.1.242 is fail!
192.168.1.237 is fail!
192.168.1.245 is fail!
192.168.1.240 is fail!
192.168.1.246 is fail!
192.168.1.243 is fail!
192.168.1.244 is fail!
192.168.1.247 is fail!
192.168.1.248 is fail!
192.168.1.250 is fail!
192.168.1.251 is fail!
192.168.1.249 is fail!
192.168.1.252 is fail!
192.168.1.254 is fail!
192.168.1.253 is fail!

三、每周的工作日1:30,将/etc备份至/backup目录中,保存的文件名称格式 为“etcbak-yyyy-mm-dd-HH.tar.xz”,其中日期是前一天的时间;

1、编写脚本和定时计划;
##创建脚本
[root@localhost sh]# pwd
/usr/sh
[root@localhost sh]# cat etc_backup.sh
#!/bin/bash
DATE_TIME=`date -d "-1 day" +%F-%H`
tar -Jcf /backup/etcbak-$DATE_TIME.tar.xz /etc/ > /dev/null

##脚本添加执行权限
[root@localhost sh]# chmod +x etc_backup.sh

##添加定时任务
[root@localhost sh]# cat /etc/crontab 
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
# 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
30 1 * * 1-5 root sh /usr/sh/etc_backup.sh &> /dev/null

--

测试:

1、确认脚本没被执行过;

[root@localhost sh]# date
2020年 07月 30日 星期四 16:57:53 CST
[root@localhost sh]# ll /backup
总用量 0

2、修改系统时间触发执行条件;

[root@localhost sh]# date -s "2020-07-31 01:29:50"
2020年 07月 31日 星期五 01:29:50 CST
[root@localhost sh]# ll /backup
总用量 192
-rw-r--r-- 1 root root 180224 7月  31 01:30 etcbak-2020-07-30-01.tar.xz

四、工作日时间,每10分钟执行一次磁盘空间检查,一旦发现任何分区利用率高 于80%,就发送邮件报警。


[root@localhost ~]# cd /usr/sh  #没有此目录则创建或者在自己想创建的目录也可以放脚本
[root@localhost sh]# cat disk_check.sh
#!/bin/bash
#取分区使用率的最大值
#sed方法
#Disk_Used=`df -h | sed -nr "/\/dev\/sda/s#.* +([0-9]+)% .*#\1#p" |sort -nr |head -1`

#awk方法
Disk_Used=`df -h | awk -F '[ %]+' 'BEGIN{max = 0}{if ($5+0 > max) max=$5} END {print max}'`

#取使用率最大分区名
Partition_Name=`df -h | grep "$Disk_Used%" |cut -d" " -f1`

if [ $Disk_Used -gt 80 ];then
        echo -e "Warning!! \n $Partition_Name's used space is more than 80%" | mail -s Warning root
fi

[root@localhost ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   17G  1.2G   16G   7% /
devtmpfs                 475M     0  475M   0% /dev
tmpfs                    487M     0  487M   0% /dev/shm
tmpfs                    487M  7.7M  479M   2% /run
tmpfs                    487M     0  487M   0% /sys/fs/cgroup
/dev/sda1               1014M  133M  882M  14% /boot
tmpfs                     98M     0   98M   0% /run/user/0

[root@localhost ~]# df -h | sed -nr "/\/dev\/sda/s#.* +([0-9]+)% .*#\1#p" |sort -nr |head -1
14

[root@localhost ~]# df -h | awk -F '[ %]+' 'BEGIN{max = 0}{if ($5+0 > max) max=$5} END {print max}'
14

[root@localhost ~]# df -h | grep "14%" |cut -d" " -f1
/dev/sda1


[root@localhost sh]# cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/

# 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
*/10 * * * 1-5 root sh /usr/sh/disk_check.sh &>/dev/null
2、测试;
1)选择测试分区
[root@localhost sh]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   17G  1.2G   16G   7% /
devtmpfs                 475M     0  475M   0% /dev
tmpfs                    487M     0  487M   0% /dev/shm
tmpfs                    487M  7.7M  479M   2% /run
tmpfs                    487M     0  487M   0% /sys/fs/cgroup
/dev/sda1               1014M  133M  882M  14% /boot
tmpfs                     98M     0   98M   0% /run/user/0

此处我们在/boot分区进行测试。

2)将测试文件写入/boot分区中;

[root@localhost sh]# dd if=/dev/zero of=/boot/test.txt bs=1M count=680
680+0 records in
680+0 records out
713031680 bytes (713 MB) copied, 14.1995 s, 50.2 MB/s

[root@localhost sh]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   17G  1.2G   16G   7% /
devtmpfs                 475M     0  475M   0% /dev
tmpfs                    487M     0  487M   0% /dev/shm
tmpfs                    487M  7.7M  479M   2% /run
tmpfs                    487M     0  487M   0% /sys/fs/cgroup
/dev/sda1               1014M  813M  202M  81% /boot
tmpfs                     98M     0   98M   0% /run/user/0
3)更改定时计划任务为每天每分钟执行一次;
[root@localhost sh]# cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/

# 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
*/1 * * * * root sh /usr/sh/disk_check.sh &>/dev/null
4)检查计划任务日志,检查邮件;
[root@localhost ~]# tail /var/log/cron
Aug  2 00:01:01 localhost run-parts(/etc/cron.hourly)[6846]: starting 0anacron
Aug  2 00:01:01 localhost anacron[6855]: Anacron started on 2020-08-02
Aug  2 00:01:02 localhost anacron[6855]: Will run job `cron.daily' in 33 min.
Aug  2 00:01:02 localhost anacron[6855]: Will run job `cron.weekly' in 53 min.
Aug  2 00:01:02 localhost anacron[6855]: Will run job `cron.monthly' in 73 min.
Aug  2 00:01:02 localhost anacron[6855]: Jobs will be executed sequentially
Aug  2 00:01:02 localhost run-parts(/etc/cron.hourly)[6857]: finished 0anacron
Aug  1 16:08:01 localhost crond[6197]: (*system*) RELOAD (/etc/crontab)
Aug  1 16:08:01 localhost CROND[6933]: (root) CMD (sh /usr/sh/disk_check.sh &>/dev/null)
Aug  1 16:09:01 localhost CROND[6953]: (root) CMD (sh /usr/sh/disk_check.sh &>/dev/null)

[root@localhost mail]# mail
Heirloom Mail version 12.5 7/5/10.  Type ? for help.
"/var/spool/mail/root": 1 message 1 new
>N  1 root                  Sat Aug  1 16:08  19/654   "Warning"
&
#####或者直接查看root的邮件
[root@localhost mail]# cat /var/spool/mail/root
From root@localhost.localdomain  Sat Aug  1 16:08:01 2020
Return-Path: <root@localhost.localdomain>
X-Original-To: root
Delivered-To: root@localhost.localdomain
Received: by localhost.localdomain (Postfix, from userid 0)
        id 86DADAEEF0; Sat,  1 Aug 2020 16:08:01 +0800 (CST)
Date: Sat, 01 Aug 2020 16:08:01 +0800
To: root@localhost.localdomain
Subject: Warning
User-Agent: Heirloom mailx 12.5 7/5/10
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Message-Id: <20200801080801.86DADAEEF0@localhost.localdomain>
From: root@localhost.localdomain (root)

Warning!!
 /dev/sda1's used space is more than 80%
经测试磁盘空间利用率检查成功,然后将定时任务时间修改回去,测试的文件删除,以后其它分区的检查对应做修改即可。

!!!时间一定要同步,方便观察
同步方式:

[root@localhost ~]# yum install ntpdate -y
[root@localhost ~]# ntpdate -u cn.pool.ntp.org

!!!出现报错 -bash: mail: command not found
解决办法:

root@localhost mail]#  yum -y install mailx

就可以了。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 205,236评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 87,867评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,715评论 0 340
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,899评论 1 278
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,895评论 5 368
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,733评论 1 283
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,085评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,722评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,025评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,696评论 2 323
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,816评论 1 333
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,447评论 4 322
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,057评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,009评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,254评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,204评论 2 352
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,561评论 2 343