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

通常情况下,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
}
check_ip_status 10.74.120.104
if [ $? -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    
    } 

 在调用shell函数时需要注意一下两个地方:

1、函数的返回值应采用echo

2、函数的状态码应采用return

 

转载于:https://www.cnblogs.com/noxy/p/5758158.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值