我需要在我的树莓派连接到互联网后立即执行脚本。然而,我想知道是否有比每分钟左右 ping 一次 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 秒的睡眠时间。不到一分钟的 ISP 中断可被视为“非中断”。
答案3
鉴于您的要求相对简单,是的,一个简单的 ping 就可以了。您无需使用 Google 作为测试主机。如果您的 ISP 有公共网站(现在很可能如此),那么就使用它。
这是我打算与我的一个旧的、暂停的项目一起使用的 [bash] 脚本。它只是ping
作为主机并收集统计信息,并按需将其打印到系统日志中。唯一的依赖项是ping
和bc
。请注意,您将需要一个不太旧的版本ping
,即 Linux-Capability-aware ping
。
该脚本使用预定义的间隔和签名值,并设计为交互式运行或在后台运行 - 例如,如果您需要一些日志记录;需要给出外部命令。您将以相同的价格获得抖动检测和链路上/下检测;-)。您已经嵌入了帮助,以备不时之需。如果您有任何问题,请随时询问。
我将其作为练习编写,同时保持尽可能低的内存占用。我记得计划让它与 dash 一起运行,但到目前为止我无法摆脱一种困扰。希望您会发现它有用,这里是:
#!/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
安装此处描述的 shell 脚本:
coupure shell 脚本。<ad>
这是一种ping 变得更简洁、更智能。</ad>
安装后,可以并行使用它来检测 ISP 上的丢失(例如,使用其 DNS 服务器之一,该服务器应位于最顶层的连接)以及 ISP 后面的 Internet 上(例如,使用 Google 服务器之一)。
通过这种并行运行,您将能够知道您的 ISP 访问或互联网是否有问题。