shell总结【第一篇】:检测主机存活

  运维工作中检查主机存活是最为常见的工作,如何快速有效的检查大量主机是否正常就变的非常有必要。因此特写此片博文记录该知识点。

方法1:shell脚本之for循环检查hosts主机存活

[root@saltstack shell]# cat ping_for.sh
#########################################################################
# File Name: ping_for.sh
# Author: mads
# Mail: 1455975151@qq.com
# Created Time: 2016年07月21日 星期四 21时33分03秒
# Description : this is scripts use to
# Version : v1.0
#########################################################################
#!/bin/bash

. /etc/init.d/functions

RED_COLOR='\E[1;31m'
GREEN_COLOR='\E[1;32m'
YELLOW_COLOR='\E[1;33m'
BLUE_COLOR='\E[1;34m'
RES='\E[0m'

array=$(sed '/^#/d' hosts|grep -v "::"|awk '{print $1}')

for host in ${array[@]}
do
        ping $host -c 10 -w 1 -i 0.01 -q >/dev/null 2>&1
        ret=$?
        if [ $ret -eq 0 ];then
                echo "$host is ok"
        else 
                echo "$host is bad"
        fi
done

方法2:shell脚本之while循环检查hosts主机存活

[root@saltstack shell]# cat ping_while.sh
#########################################################################
# File Name: ping_while.sh
# Author: mads
# Mail: 1455975151@qq.com
# Created Time: 2016年07月21日 星期四 21时33分03秒
# Description : this is scripts use to
# Version : v1.0
#########################################################################
#!/bin/bash

. /etc/init.d/functions

RED_COLOR='\E[1;31m'
GREEN_COLOR='\E[1;32m'
YELLOW_COLOR='\E[1;33m'
BLUE_COLOR='\E[1;34m'
RES='\E[0m'

cat hosts| grep -vE "::|#" |while read line
do
        ip=`echo $line|awk '{print $1}'`
        ping -i 0.01 -c 10 -w 1 -q $ip >/dev/null 2>&1
        if [ $? -eq 0 ];then
                echo "$ip is ok"
        else
                echo "$ip is bad"
        fi
done
[root@saltstack shell]# cat hosts
# Do not remove the following line, or various programs
# that require network functionality will fail.
127.0.0.1        master.strong.com master localhost.localdomain localhost
::1        localhost6.localdomain6 localhost6
192.168.80.1      host1.strong.com
192.168.80.2      host2.strong.com
192.168.80.130    host130.strong.com
192.168.80.4      host4.strong.com
192.168.80.5      host5.strong.com
192.168.80.6      host6.strong.com
192.168.80.7      host7.strong.com
192.168.80.8      host8.strong.com
www.baidu.com     www.baidu.com
www.sina.com.cn      www.sina.com.cn
www.2345.com      www.2345.com
方法1和方法2测试文件

方法3:Python脚本实现

[root@saltstack shell]# cat ping_python.py
#!/usr/bin/env python
#_*_ coding:utf8 _*_
#脚本主要用于做ping测试
 
import re
import subprocess
 
def check_alive(ip, count=10, timeout=1, ttl=0.01):
        '''
        ping网络测试,通过调用ping命令,发送一个icmp包,从结果中通过正则匹配是否有100%关键字,有则表示丢包,无则表示正常
        '''
        cmd = 'ping -c %d -w %d -i %s %s' % (count,timeout,ttl,ip)
 
        p = subprocess.Popen(cmd,
                stdin=subprocess.PIPE,
                stdout=subprocess.PIPE,
                stderr=subprocess.PIPE,
                shell=True
        )
 
        result = p.stdout.read()
        regex = re.findall('100% packet loss',result)
        if len(regex) == 0:
                print("\033[32m%s UP\033[0m" % ip)
        else:
                print("\033[31m%s DOWN\033[0m" % ip)
 
 
if __name__ == "__main__":
    with file('./ip.txt','r') as f:
            for line in f.readlines():
                    ip = line.strip()
                    check_alive(ip)         #执行函数功能调用

 

方法4:shell脚本之模拟多线程

[root@saltstack shell]# cat ping_host.sh.mutli 
#########################################################################
# File Name: ping_host.sh
# Author: mads
# Mail: 1455975151@qq.com
# Created Time: 2015年10月31日 星期六 18时58分33秒
# Description : this is scripts use to
# Version : v1.0
#########################################################################
#!/bin/bash

. /etc/init.d/functions

RED_COLOR='\E[1;31m'
GREEN_COLOR='\E[1;32m'
YELLOW_COLOR='\E[1;33m'
BLUE_COLOR='\E[1;34m'
RES='\E[0m'

tmp_fifofile="/tmp/$.fifo"
mkfifo $tmp_fifofile              # 新建一个fifo类型的文件
exec 6<>$tmp_fifofile          # 将fd6指向fifo类型
rm $tmp_fifofile

thread=5                              # 此处定义线程数
for ((i=0;i<$thread;i++));do
echo
done >&6                                                # 事实上就是在fd6中放置了$thread个回车符

array=$(cat ip.txt)
#echo $array

for host in ${array[@]}
do                                           # 50次循环,可以理解为50个主机,或其他
read -u6             # 一个read -u6命令执行一次,就从fd6中减去一个回车符,然后向下执行,fd6中没有回车符的时候,就停在这了,从而实现了线程数量控制
{                                               # 此处子进程开始执行,被放到后台
        ping $host -c 10 -w 1 -i 0.01 -q >/dev/null 2>&1
    ret=$?
    if [ $ret -eq 0 ];then
            echo "$host is ok"
    else
            echo "$host is bad"
    fi
        echo >&6                                        # 当进程结束以后,再向fd6中加上一个回车符,即补上了read -u6减去的那个
} &

done

wait                           # 等待所有的后台子进程结束
exec 6>&-                 # 关闭df6
exit 0

方法5:将任务放到后台执行

[root@saltstack shell]# cat ping_host.sh.single 
#########################################################################
# File Name: ping_host.sh
# Author: mads
# Mail: 1455975151@qq.com
# Created Time: 2015年10月31日 星期六 18时58分33秒
# Description : this is scripts use to
# Version : v1.0
#########################################################################
#!/bin/bash

. /etc/init.d/functions

RED_COLOR='\E[1;31m'
GREEN_COLOR='\E[1;32m'
YELLOW_COLOR='\E[1;33m'
BLUE_COLOR='\E[1;34m'
RES='\E[0m'

array=$(cat ip.txt)

for host in ${array[@]}
do
        {
                ping $host -c 10 -w 1 -i 0.01 -q >/dev/null 2>&1
                ret=$?
                if [ $ret -eq 0 ];then
                        echo "$host is ok"
                else
                        echo "$host is bad"
                fi
        }&
done

 

[root@saltstack shell]# cat ip.txt 
192.168.0.24
192.168.0.25
192.168.0.26
192.168.0.27
192.168.0.28
192.168.0.29
192.168.0.40
192.168.0.41
192.168.0.42
192.168.0.43
192.168.0.81
192.168.0.82
192.168.0.91
192.168.0.92
192.168.0.93
192.168.0.96
192.168.0.97
192.168.0.101
192.168.0.102
192.168.0.110
192.168.0.111
192.168.0.210
192.168.0.211
192.168.0.230
192.168.0.231
192.168.0.232
192.168.0.247
192.168.0.248
192.168.0.250
117.79.235.226
117.79.235.227
117.79.235.230
117.79.235.238
210.14.130.130
210.14.156.210
方法3、4、5的测试文件

 

posted @ 2016-07-21 22:12  每天进步一点点!!!  阅读(1280)  评论(0编辑  收藏  举报