利用shell脚本函数实现网络连通性的检测

时间:2016-08-10 21:03:36   收藏:0   阅读:1631

通常情况下,linux系统网络连通性的检测有两种方法:

1、通过“ping指令 + 目标IP” 或者 “ping指令 + 目标IP域名”

注意针对IPv6地址的IP网络检测需要试用ping6,同时ping6中不能使用参数hops,因为IP6源码中已经舍弃了路由,见如下英文解释:

SYNOPSIS
       ping  [-aAbBdDfhLnOqrRUvV] [-c count] [-F flowlabel] [-i interval] [-I interface] [-l preload] [-m mark] [-M pmtudisc_option] [-N nodeinfo_option]
       [-w deadline] [-W timeout] [-p pattern] [-Q tos] [-s packetsize] [-S sndbuf] [-t ttl] [-T timestamp option] [hop ...] destinationt.
Notes:

       ping6  is  IPv6  version of ping, and can also send Node Information Queries (RFC4620).  Intermediate hops may not be allowed, because IPv6 source
       routing was deprecated.

ping指令调用时的常用参数:

# Parameter:
# -c the number of packages to send
# -i internel between two packages
# -W timeout seconds

 

# Check the connect status of internet
function check_ip_status()
{
    ping -c 3 -i 0.2 -W 3 $1 &> /dev/null
    if [ $? -eq 0 ];then
        return 0
    else
        return -1
    fi
}

if ( `$(check_ip_status 10.74.120.104) -ne 0` );then
    echo "cannot connect guestconf server:10.74.120.104"
    exit -1
fi

 

2、使用“curl指令 + IP域名”

    #!/usr/bin/bash  
    #  
    #检测网络链接&&ftp上传数据    
    function networkAndFtp()    
    {    
        #超时时间    
        timeout=5    
            
        #目标域名    
        target=www.baidu.com    
        
        #获取响应状态码    
        ret_code=`curl -I -s --connect-timeout $timeout $target -w %{http_code} | tail -n1`    
        
        if [ "x$ret_code" = "x200" ]; then    
            #网络畅通    
        else    
            #网络不畅通    
        fi    
    } 

 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!