我正在编写一个bash脚本,用于检测何时连接到Internet并尝试重新连接以及在leds上显示状态。
我使用netcat命令连接到端口80上的谷歌以确定网络连接是否正常
当连接互联网时,一切正常,但是,当关闭我的路由器(模拟丢失)时,netcast命令需要30秒才能超时!
应用程序适用于WiFi无线电,
#!/bin/bash
sig_state=0
echo "Network Watchdog ON"
echo "0" > /sys/class/gpio/export
echo "out" > /sys/class/gpio/gpio0/direction
function sig_led_high
{
echo "0" > /sys/class/gpio/gpio0/value
sig_state=1
}
function sig_led_low
{
echo "1" > /sys/class/gpio/gpio0/value
sig_state=0
}
sig_led_low
while true ; do
echo "Testing nwrk..."
if nc -z -w 1 www.google.co.uk 80 > /dev/null 2>&1; then
echo "Network OK. (nwd)"
if ((sig_state)); then
sig_led_low
fi
else
if !((sig_state)); then
sig_led_high
fi
echo "Network connection down! Attempting reconnection."
ifup --force wlan0 > /dev/null 2>&1
if nc -zw1 www.google.co.uk 80 > /dev/null 2>&1; then
mpc play
fi
fi
sleep 1
done