• 搜索
  • 夜间模式
    ©2025  Ethan's Blog Theme by OneBlog
    搜索
    标签
    # Amule # Gargoyle # LUCI # VIM # Python # Nginx # 反代 # Ansible # Apache # LNMP
  • 首页>
  • 运维>
  • 正文
  • 几种检测网络连接性的方式

    2020年08月31日 1 阅读 0 评论 3399 字

    Linux篇

    ping检测

    使用系统自带的ping命令,开启ping后同时输出时间戳保存到日志文件

    ping 223.5.5.5 | awk '{ print strftime("%Y.%m.%d %H:%M:%S",systime())"\t" $0 }'>> ping.log

    优点是不用安装额外的程序,不用写复杂的脚本,缺点是丢包的时候在日志中不会有失败提示,日志记录会从最后一次ping成功的时间戳跳到下一次ping成功的时间戳,检查的时候需要慢慢看时间才能发现掉包时间点。如果要统计发包数及丢包数及百分比,需要手动kill掉ping的进程

    fping检测

    需要安装fping程序,提供几个例子:

    fping -Alesu 223.5.5.5 >> fping1.log 2>&1

    | 缺点:没有时间戳,只能看到丢包率

    fping -AlDuQ 1 223.5.5.5 >> fping3.log 2>&1

    | 优点:可以看到时间戳及丢包率

    fping参数详解:

    -a         show targets that are alive
       -A         show targets by address
       -b n       amount of ping data to send, in bytes (default 56)
       -B f       set exponential backoff factor to f
       -c n       count of pings to send to each target (default 1)
       -C n       same as -c, report results in verbose format
       -D         print timestamp before each output line
       -e         show elapsed time on return packets
       -f file    read list of targets from a file ( - means stdin) (only if no -g specified)
       -g         generate target list (only if no -f specified)
                    (specify the start and end IP in the target list, or supply a IP netmask)
                    (ex. fping -g 192.168.1.0 192.168.1.255 or fping -g 192.168.1.0/24)
       -H n       Set the IP TTL value (Time To Live hops)
       -i n       interval between sending ping packets (in millisec) (default 25)
       -I if      bind to a particular interface
       -l         loop sending pings forever
       -m         ping multiple interfaces on target host
       -n         show targets by name (-d is equivalent)
       -O n       set the type of service (tos) flag on the ICMP packets
       -p n       interval between ping packets to one target (in millisec)
                    (in looping and counting modes, default 1000)
       -q         quiet (don't show per-target/per-ping results)
       -Q n       same as -q, but show summary every n seconds
       -r n       number of retries (default 3)
       -s         print final stats
       -S addr    set source address
       -t n       individual target initial timeout (in millisec) (default 500)
       -T n       ignored (for compatibility with fping 2.4)
       -u         show targets that are unreachable
       -v         show version
       targets    list of targets to check (if no -f specified)

    编写脚本记录(推荐)

    通过定时任务配合自带的ping程序进行检测并记录到日志文件,分为定时任务脚本及ping脚本
    定时任务脚本:

    for i in $(seq 1 60)
    do 
            echo  "*/1 * * * *  root sleep ${i} && sh /root/internetcheck.sh >> /tmp/wan.log 2>&1" >> /etc/crontab
    done

    ping脚本:

    #!/bin/bash
    
    time=`date +%m.%d-%H:%M:%S:`
    
    ping -c 3 223.5.5.5 > /dev/null
    if [ $? -eq 0 ];then
            echo -e "$time \e[1;32m Network connection normal \e[0m"
    else
            echo -e "$time \e[1;31m No network connection \e[0m"
    fi

    将ping脚本保存为internetcheck.sh并添加执行权限,将定时任务脚本保存后,直接执行一边脚本即可。

    Windows篇

    本来不大想记录Windows的脚本的,毕竟很容易出现丢包不记录的情况,但是鉴于很多时候维护都用windows,还是记录一个

    set host=223.5.5.5
    set logfile=Log_%host%.log
    echo Target Host = %host% >%logfile%
    for /f "tokens=*" %%A in ('ping %host% -n 1 ') do (echo %%A>>%logfile% && GOTO Ping)
    :Ping
    for /f "tokens=* skip=2" %%A in ('ping %host% -n 1 ') do (
        echo %date% %time:~0,2%:%time:~3,2%:%time:~6,2% %%A>>%logfile%
        echo %date% %time:~0,2%:%time:~3,2%:%time:~6,2% %%A
        timeout 1 >NUL 
        GOTO Ping)

    将上述脚本保存为bat文件直接运行即可

    本文著作权归作者 [ Ethan ] 享有,未经作者书面授权,禁止转载,封面图片来源于 [ 互联网 ] ,本文仅供个人学习、研究和欣赏使用。如有异议,请联系博主及时处理。
    — END —
    Copyright©2025  All Rights Reserved.  Load:0.018 s
    Theme by OneBlog V3.6.3
    夜间模式

    开源不易,请尊重作者版权,保留基本的版权信息。