利用shell脚本函数实现网络连通性的检测 您所在的位置:网站首页 netcheck网络测速 利用shell脚本函数实现网络连通性的检测

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

2023-08-06 10:31| 来源: 网络整理| 查看: 265

通常情况下,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

 



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有