끊어진 인터넷 연결을 기록하는 방법

끊어진 인터넷 연결을 기록하는 방법

라즈베리 파이가 인터넷에 연결되자마자 스크립트를 실행해야 합니다. 하지만 매분마다 Google을 핑하는 것보다 더 좋은 방법이 있는지 궁금합니다.

내 문제는 하루 동안 인터넷 연결이 1-2번 끊어진다는 것입니다. 그래서 그러한 이벤트를 기록할 방법이 필요합니다.
이것은 낮에 ADSL이 떨어지는 경우에 불과하며, 내가 눈치채지 못하더라도 이러한 일이 발생했음을 기록할 수 있는 방법을 찾고 있었습니다. 제안한대로 스크립트를 설정하겠습니다.

답변1

당신은 확인할 수 있습니다:

cat /sys/class/net/wlan0/carrier

wlan0은 내 인터넷 인터페이스입니다. eth0, eth1, wlan0 등 사용 중인 인터페이스를 사용하여 인터넷에 연결할 수 있습니다. 이 명령의 출력이 1이면 연결된 것입니다. 그렇지 않으면 작동하지 않습니다. 따라서 다음과 같은 스크립트를 작성할 수 있습니다.

#!/bin/bash
# Test for network conection
for interface in $(ls /sys/class/net/ | grep -v lo);
do
if [[ $(cat /sys/class/net/$interface/carrier) = 1 ]]; then ; echo "online"; fi
done

다음 명령을 사용할 수도 있습니다.

#hwdetect --show-net

이 스크립트도 잘 실행됩니다.

#!/bin/bash

WGET="/usr/bin/wget"

$WGET -q --tries=20 --timeout=10 http://www.google.com -O   /tmp/google.idx &> /dev/null
if [ ! -s /tmp/google.idx ]
then
  echo "Not Connected..!"
else
  echo "Connected..!"
fi

답변2

시스템에 연결이 있다고 생각하는지 묻는 것은 "내 ISP가 데이터를 삭제하고 있습니까?"에 대한 프록시 측정입니다. 정의에 따르면 대리 측정값은 관심 시스템의 단순화된 모델이며 정보를 유지하지 않습니다. 이 질문은 관심 있는 정보를 실제로 얻어야만 답을 얻을 수 있습니다.

ping실제로는 종종 특별한 대우를 받는 ICMP 프로토콜이기 때문에 테스트하기에 좋지 않은 선택입니다. 예를 들어, HTTP 연결에 관심이 있는 경우

curl --head http://www.example.com

실제로 페이지를 얻을 수 있는지 여부를 보여줍니다. 설문조사를 할 경우에는 예의바르게 행동하고 통화 사이에 최소 60초의 수면 시간을 허용하십시오. 1분 미만의 ISP 중단은 "비정지"로 간주될 수 있습니다.

답변3

요구 사항이 상대적으로 간단하다는 점을 고려하면 간단한 ping만으로도 충분합니다. Google을 테스트 호스트로 사용할 필요는 없습니다. 귀하의 ISP가 공개 웹사이트를 가지고 있다면(요즘에는 그럴 것입니다), 그 웹사이트를 사용하십시오.

이것은 오래되고 일시 중지된 내 프로젝트에 사용하려는 [bash] 스크립트입니다. 단지 ping호스트 역할을 하며 통계를 수집하여 필요에 따라 시스템 로그에 인쇄합니다. 유일한 종속성 pingbc. 너무 오래되지 않은 버전인 pingLinux-Capability-aware가 필요하다는 점에 유의하세요 ping.

스크립트는 미리 정의된 간격과 서명 값을 사용하며 대화식으로 또는 백그라운드에서 실행되도록 설계되었습니다. 예를 들어 일부 로깅이 필요한 경우 외부 명령을 제공해야 합니다. 동일한 가격으로 지터 감지 및 링크 업/다운 감지 기능을 사용할 수 있습니다 ;-). 필요할 경우를 대비해 도움이 내장되어 있습니다. 궁금한 점이 있으시면 언제든지 문의해 주세요.

나는 메모리 사용량을 가능한 한 낮게 유지하면서 연습으로 이것을 썼습니다. 대시로 실행시키려고 계획을 세웠던 기억이 나지만 아직까지는 집착을 버릴 수가 없습니다. 이 내용이 도움이 되셨기를 바랍니다. 내용은 다음과 같습니다.

#!/bin/bash
#
#  ping.sh
#
#  Copyright 2014 VinzC <[email protected]>
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#  MA 02110-1301, USA.
#
# Runs in the background ping'ing given addresses. May send
# notifications through an external routine that is given as
# an argument.
#
# Uses environment variables:
#   * LOG_FILE (log file for debug purposes, silent otherwise)
#
# Symlinked as /usr/bin/anemon-ping

VERSION=1.0.3
AUTHOR="VinzC <[email protected]>"
DATE="Jan 2014"

# Background ping, keep only relevant information from the
# response, add timestamp in seconds. For use with graphing.

# Default padding for ICMP packets
ICMP_PADDING_DEFAULT=ffeab16b00b1e2

# Default number of seconds between ICMP probes
ICMP_INTERVAL_DEFAULT=300

# Number of samples to declare a host state steady
ICMP_STEADY_THRESHOLD=3

# Number of samples in which a host is allowed to go down
# 3 times before being flagged as flapping. (Why 3? 2 is
# not enough to make it a habbit, 3 is...)
ICMP_FLAPPING_THRESHOLD=24

# Number of samples after which dupe and damaged packets
# are gone for good. Default matches 24 hours.
ICMP_NET_ERROR_THRESHOLD=288

# Host state change command. The command is called with two or
# three arguments: host name and state, plus optional arguments.
# See usage for events.
host_cmd=false

# True if results shall be written to the standard output
verbose=false

# State variables
host_state=1            # Bit0: 1=up, 0=down
                        # Bit1: 1=flapping, 0=normal
host_unavail_state=0    # State counters
host_flap_state=0
host_warning_state=0

rtt_min=                # Ping statistics
rtt_max=
rtt_avg=

icmp_seq=0              # Number of requests
icmp_lost=0             # Count of losses

icmp_dupes=             # Network errors (counters)
icmp_damaged=

usage()
{
    cat <<EOF

USAGE
    ${0##*/} [-i interval] [-p pattern] [-P string] [-I interface]
        [-s packetsize] [-W timeout] [-v] [--host-command=CMD]
        [--flapping-threshold=N] [--error-threshold=N] destination
    ${0##*/} -h

ARGUMENTS
    Short options are a subset of standard ping arguments with the
    exception of -P and -v. Both -p and -P define a custom padding.
    Unlike ping's -p, option -P accepts any alphanumeric string,
    which will be converted to a hexadecimal string. The resulting
    pattern is eventually truncated to 16 bytes.

    Option -v switches to verbose mode. In this mode echo replies
    are sent to the standard output. This is useful for testing.

    -h  prints this help page.

    --host-command=CMD

        Run CMD on events. The first argument is the destination
        parameter, the second is the event name. Some events may
        have additional parameters.

        All events except the echo reply are sent to the system
        log. Events are:

        "start"     The monitoring process has been started. The
                external script can initialize its working
                context, e.g. create a round-robin database
                to store echo reply TTL values. An additional
                argument is passed with the interval value if
                option -i was specified. The default interval
                matches RRD default step, i.e. 300 seconds.
        "stop"      The monitoring process is stopping.
        "icmp"      Echo reply. Additional arguments are timestamp
                and roudtrip time. This event occurs every 5
                minutes by default and can be changed with -i.
        "up"        Host is up, steadily.
        "down"      Host is down, steadily.
        "flapping"  Host state is unstable.
        "damaged"   Damaged packets were detected.
        "dupes"     Duplicate packets were detected.

    --flapping-threshold=N

        The script attempts to detect flapping interfaces. It uses
        a discrete formula to keep a reasonable maximum delay in
        cases flapping occurs frequently, which must be considered
        a critical situation. Flapping detection is done as soon
        as no response from the remote host is received. No echo
        reply for more than 3 samples is a steady "down" state.

        An interface that has been flapping will take longer to be
        considered steady again. The value of --flapping-threshold
        sets that delay to the same amount of samples.

        The default value for N is 24. A bigger value will make an
        flapping interface wait proportionally longer before it is
        marked "up" again.

    --error-threshold=N

        Linux only: the background ping process informs whenever
        duplicated or damaged packets are received from a remote
        host. Such network errors are notified no more than once
        in N samples.

INTERACTIVE CONTROL
    The script reacts to signal SIGHUP to print statistics on the
    PING process running in the background. If verbose mode is
    enabled (-v) statistics are sent to the standard output. They
    are sent to the system log otherwise.

    Example:
        ${0##*/} --host-command=... host &
        PID=$!
        ...
        kill -HUP $PID

    PING statistics include min, avg and max roundtrip times and
    percentage of lost echo replies. Individual figures are also
    shown for damaged and duplicate packets if there are any.

    Note that the average value is a running average that uses a
    discrete averaging algorithm, i.e.:

        avg(i) = [ avg(i-1) + x(i-1) ] / 2

SEE ALSO
    man ping, ping(8)
EOF
    exit 0
}

help()
{
    cat <<EOF
${0##*/} version $VERSION, © $DATE by $AUTHOR

Monitors a remote host periodically sending ICMP packets from a ping
process running in the background. The script can execute a custom
command each time an important network condition occurs. The main
purpose is to record echo reply times in a round-robin database for
further graphing.
EOF
    usage
}

bc()
{
    # Workaround to print the leading zero for values <1
    /usr/bin/bc | sed 's/^\./0./g'
}

# Parse command line arguments. Only parse new or overriden arguments.
# Used to determine the remote host, mainly.
parse_args()
{
    # Need a F@!#^}G temporary variable to check getopt return code!
    args="$(getopt \
        -o i:I:p:P:s:W:hv \
        -l host-command:,flapping-threshold:,error-threshold \
        -- "$@")" && eval set -- "$args" && unset args || return $?

    # Now check the remaining arguments
    while [ -n "$1" ]; do
        [ "$1" = "--" ] || case $1 in

            --host-command)
                host_cmd=$2; shift;;

            --flapping-threshold)
                ICMP_FLAPPING_THRESHOLD=$2; shift;;

            --error-threshold)
                ICMP_NET_ERROR_THRESHOLD=$2; shift;;

            -i)
                ICMP_INTERVAL=$2; shift;;
            -p)
                ICMP_PADDING=$2; shift;;
            -P)
                ICMP_PADDING=$(printf "$2" | od -A n -t x1 | \
                    sed -r -e 's:\s+::g' -e 's:.::33g'); shift;;
            -I)
                ICMP_IFACE=$2; shift;;
            -s)
                ICMP_PKTSIZE=$2; shift;;
            -W)
                ICMP_TIMEOUT=$2; shift;;
            -v)
                verbose=true;;
            -h)
                help;;
            *)
                ICMP_HOST="$1";;
        esac; shift
    done
    [ -n "$ICMP_HOST" ] || usage 1>&2
}

logger()
{
    # Write to standard output in verbose mode, to syslog otherwise
    $verbose && echo "$@" || \
        /usr/bin/logger -t "${0##*/}[$$]" "$@"
}

set_response_time()
{
    # Call external command or prompt to the console in verbose mode
    $host_cmd $ICMP_HOST "icmp" $1 $3 && ! $verbose || \
        printf "%d: seq=%d, time=%s\n" "$1" "$2" "$3"
    return 0
}

set_state_up()
{
    # Clear flapping state ans set (steady) up flag
    host_state=0x01

    # Call the external notification function and log host state
    $host_cmd $ICMP_HOST "up"
    logger "Host interface or host @ $ICMP_HOST is now up."
}

set_state_down()
{
    # Clear up flag only
    host_state=$(( host_state & 0xFE ))

    # Call the external notification function and log host state
    $host_cmd $ICMP_HOST "down"
    logger "Host interface or host @ $ICMP_HOST is down!"
}

set_state_flapping()
{
    # Set flapping and down flags
    host_state=2

    # Call the external notification function and log host state
    $host_cmd $ICMP_HOST "flapping"
    logger "Host interface or host @ $ICMP_HOST is unstable!"
}

set_host_message()
{
    # Reset error counter to the maximum
    host_warning_state=$ICMP_NET_ERROR_THRESHOLD

    # Call the external notification function and log host state
    $host_cmd $ICMP_HOST $1
    logger "Errors received from host interface or host @ $ICMP_HOST ($1)!"
}

print_stats()
{
    if [ $icmp_seq -eq 0 ]; then
        logger "PING $ICMP_HOST: no packet sent"
    else
        local icmp_received=$(( icmp_seq - icmp_lost ))
        local icmp_losses=$( echo "scale=2; 100 * $icmp_lost / $icmp_seq" | bc )
        logger "PING $ICMP_HOST: $icmp_seq packets sent, $icmp_lost lost, ${icmp_losses}% loss${rtt_min:+; rtt min/avg/max = $rtt_min/$rtt_avg/$rtt_max ms}${icmp_dupes:+, $icmp_dupes dupes}${icmp_damaged:+, $icmp_damaged bad CRC}"
    fi
}

echo_reply()
{
    # First argument is time in seconds (icmp_seq is global)
    local TM=$1 ttl time msg; shift

    # Evaluate the remaining arguments as expressions
    eval "$@"

    # No time variable means host is not responding
    [ -z "$time" ] && return 1

    # Update statistics: average, minimum and maximum RTT
    rtt_avg=$( echo "scale=3; (${rtt_avg:-$time} + $time)/2" | bc )
    rtt_min=$( echo "scale=3; rtt_min=${rtt_min:-$time}; if ($time < rtt_min) $time else rtt_min" | bc )
    rtt_max=$( echo "scale=3; rtt_max=${rtt_max:-$time}; if ($time > rtt_max) $time else rtt_max" | bc )

    # Decrement the state counter if greater than zero
    [ $host_unavail_state -ne 0 ] && \
        host_unavail_state=$(( host_unavail_state - 1 ))

    # The host is not up if:
    # - it is flapping (bit 1 of the state flag) and the flapping
    #   counter is greater than 0  OR
    # - the state counter is greater than 0 (non flapping case).
    # As long as one of these condition is true, the state flag
    # will not be set to UP (bit 0 set, bit 1 cleared).
    #
    # Once the state counter reaches zero (steady "up" state) and
    # the host state is no longer flapping, change the state flag.
    # Also don't change the state flag if bit 0 was already set.
    [ $(( host_state & 0x02 )) -ne 0  ] && [ $host_flap_state -ne 0 ] || \
    [ $host_unavail_state -ne 0 ] || \
    [ $(( host_state & 0x01 )) -ne 0 ] || \
        set_state_up

    # Warn if damaged or duplicate packets. Don't warn
    # again until the warning counter reaches zero. Treat
    # damaged and dupe packets alike for both are very
    # unlikely to occur at the same time.
    if [ -n "$msg" ]; then
        eval rtt_$msg=$(( rtt_$msg + 1 ))
        [ $host_warning_state -eq 0 ] && set_host_message $msg
    fi

    # Run external command to store response time
    set_response_time $TM $icmp_seq $time
    return 0
}

no_response()
{
    # Store the number of lost replies
    icmp_lost=$(( icmp_lost + 1 ))

    # FLAPPING DETECTION
    # ------------------
    # Increment flapping state using a discrete low-pass formula
    # to prevent excessive values. Handle flapping only if host
    # has just come down, don't wait for a steady "down" state.
    [ $host_unavail_state -eq 0 ] && \
    [ $(( host_flap_state=(3*host_flap_state + 7*ICMP_FLAPPING_THRESHOLD) / 8 )) -gt $ICMP_FLAPPING_THRESHOLD ] && \
        set_state_flapping

    # Increment host state until it reaches the threshold, which
    # marks the steady "down" state. Only then call the external
    # command to allow notifying the host is "down". Just don't
    # call the command more than once if the host is still down
    # by the next time.
    [ $host_unavail_state -lt $ICMP_STEADY_THRESHOLD ] && \
    [ $(( host_unavail_state=host_unavail_state + 1 )) -eq $ICMP_STEADY_THRESHOLD ] && \
    [ $(( host_state & 0x03 )) -eq 1 ] && \
        set_state_down
}

# Parse command-line arguments and set globals
parse_args "$@" || exit $?

# Redirect stderr to LOG_FILE if defined
[ -z "$LOG_FILE" ] || exec 2>$LOG_FILE

# Print PING statistics upon receiving SIGUSR1
trap print_stats HUP

# Send even "stop" upon terminating
trap "printf '\n'; $host_cmd $ICMP_HOST stop; print_stats" INT QUIT TERM ABRT


# Notify monitoring starts
$host_cmd $ICMP_HOST "start"

# 1. filter out lines keeping only those that include response
#    times and those about non responding hosts.
# 2. Stick units to response times and keep only the multiplier
#    if it's different from "m"
# 3. Keep only the integer part of the timestamp, erase garbage
#    before the relevant information (var=value)
# 4. Warn about damaged and duplicate packets
#
# Make sure sed does NOT buffer output (hence -u)
while read R; do
    echo_reply $R || no_response

    # Decrement other state variables until it reaches zero
    [ $host_flap_state -eq 0 ] || \
        host_flap_state=$(( host_flap_state - 1 ))

    [ $host_warning_state -eq 0 ] || \
        host_warning_state=$(( host_warning_state - 1 ))

# Downside: need bash for process redirection, which is needed
# to access state variables outside the while/read loop...
done < <(LC_ALL=POSIX /bin/ping -OD \
    ${ICMP_IFACE:+-I $ICMP_IFACE} \
    ${ICMP_TIMEOUT:+-W $ICMP_TIMEOUT} \
    ${ICMP_PKTSIZE:+-s $ICMP_PKTSIZE} \
    -i ${ICMP_INTERVAL:-$ICMP_INTERVAL_DEFAULT} \
    -p ${ICMP_PADDING:-$ICMP_PADDING_DEFAULT} $ICMP_HOST |
sed -rnu \
    -e '/no answer|[0-9]+ bytes from/!d' \
    -e 's@(time=[0-9.]+)\s+m?(\w*)s@\1\2@g' \
    -e 's@\(DUP\!\)@msg="dupes"@g' \
    -e 's@\(BAD CHECKSUM\!\)@msg="damaged"@g' \
    -e 's@\[(\w+)\.\w+\][a-zA-Z0-9():. \-]+\s+@\1 @gp')

답변4

여기에 설명된 쉘 스크립트를 설치하십시오. 쿠폰 쉘 스크립트. <ad>이것은ping이 더욱 간편해지고 똑똑해졌습니다..</ad>

일단 설치되면 ISP(예: 최상위 연결에 있어야 하는 DNS 서버 중 하나 사용) 및 ISP 뒤의 인터넷(예: Google 서버).

이를 병렬로 실행하면 ISP 액세스 또는 인터넷에 문제가 있는지 알 수 있습니다.

관련 정보