批量检测服务器存活脚本--Linux bash

安装web服务器并开启

root@instance-zqtg07w6:~# apa-get install apache2
root@instance-zqtg07w6:~# service apache2 start
root@instance-zqtg07w6:~# service apache2 status
● apache2.service - LSB: Apache2 web server
   Loaded: loaded (/etc/init.d/apache2; bad; vendor preset: enabled)
  Drop-In: /lib/systemd/system/apache2.service.d
           └─apache2-systemd.conf
   Active: active (running) since Thu 2019-12-26 20:33:04 CST; 1 weeks 4 days ago
     Docs: man:systemd-sysv-generator(8)
  Process: 25532 ExecStop=/etc/init.d/apache2 stop (code=exited, status=0/SUCCESS)
  Process: 15620 ExecReload=/etc/init.d/apache2 reload (code=exited, status=0/SUCCESS)
  Process: 3627 ExecStart=/etc/init.d/apache2 start (code=exited, status=0/SUCCESS)
    Tasks: 22

检测脚本

majun@instance-zqtg07w6:~/bash_scripts$ cat server_alive.sh
#!/bin/bash
TIMESTAMP=`date +%Y%m%d%H%M%S`
CURRENT_HTML=/var/www/html/server_alive/${TIMESTAMP}.html
CURRENT_INDEX=/var/www/html/server_alive/index.html
LN=/bin/ln
RM=/bin/rm
SERVER_LIST=server_list
cat <<EOF >$CURRENT_HTML
<html>
<head>
<title>Server Alive Monitor </title>
</head>
<body>
<table width="50%" border="1" cellpading="1" cellspaceing="0"
   align="center">
   <caption>
   <h2>
   Server Alive status
   </h2>
   </caption>
   <tr><th>Server IP</th><th>
   Server Status</th></tr>
EOF
while read SERVERS
do
        ping $SERVERS -c 2
        if [ $? -eq 0 ]; then
                STATUS=OK
                COLOR=blue
        else
                STATUS=FALSE
                COLOR=red
        fi

        echo "<tr><td>$SERVERS</td><td><fontcolor=$COLOR>$STATUS</font></td></tr>">> $CURRENT_HTML
done < $SERVER_LIST
cat <<EOF >> $CURRENT_HTML
 </table>
   </body>


      </html>

EOF

$LN -sf $CURRENT_HTML $CURRENT_INDEX
majun@instance-zqtg07w6:~/bash_scripts$

设置检测对象

majun@instance-zqtg07w6:~/bash_scripts$ cat server_list
220.181.38.148
39.156.69.79
140.205.220.96
140.205.94.189

majun@instance-zqtg07w6:~/bash_scripts$

检测实例

majun@instance-zqtg07w6:~/bash_scripts$ vim server_alive.sh
majun@instance-zqtg07w6:~/bash_scripts$ bash server_alive.sh
PING 220.181.38.148 (220.181.38.148) 56(84) bytes of data.
64 bytes from 220.181.38.148: icmp_seq=1 ttl=55 time=2.34 ms
64 bytes from 220.181.38.148: icmp_seq=2 ttl=55 time=2.31 ms

--- 220.181.38.148 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1001ms
rtt min/avg/max/mdev = 2.311/2.329/2.348/0.051 ms
PING 39.156.69.79 (39.156.69.79) 56(84) bytes of data.
64 bytes from 39.156.69.79: icmp_seq=1 ttl=52 time=3.02 ms
64 bytes from 39.156.69.79: icmp_seq=2 ttl=52 time=2.79 ms

--- 39.156.69.79 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1001ms
rtt min/avg/max/mdev = 2.796/2.912/3.029/0.128 ms
PING 140.205.220.96 (140.205.220.96) 56(84) bytes of data.
64 bytes from 140.205.220.96: icmp_seq=1 ttl=45 time=27.1 ms
64 bytes from 140.205.220.96: icmp_seq=2 ttl=45 time=26.7 ms

--- 140.205.220.96 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1001ms
rtt min/avg/max/mdev = 26.777/26.983/27.189/0.206 ms
PING 140.205.94.189 (140.205.94.189) 56(84) bytes of data.
64 bytes from 140.205.94.189: icmp_seq=1 ttl=43 time=32.5 ms
64 bytes from 140.205.94.189: icmp_seq=2 ttl=43 time=32.3 ms

--- 140.205.94.189 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1001ms
rtt min/avg/max/mdev = 32.379/32.439/32.500/0.190 ms
Usage: ping [-aAbBdDfhLnOqrRUvV] [-c count] [-i interval] [-I interface]
            [-m mark] [-M pmtudisc_option] [-l preload] [-p pattern] [-Q tos]
            [-s packetsize] [-S sndbuf] [-t ttl] [-T timestamp_option]
            [-w deadline] [-W timeout] [hop1 ...] destination
majun@instance-zqtg07w6:~/bash_scripts$

majun@instance-zqtg07w6:~/bash_scripts$ ls /var/www/html/server_alive/
20200107165055.html  index.html

web图形化输出

在这里插入图片描述

所有的练习脚本都在:
https://github.com/SaltNego/Learn_linux_bash

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值