[英]Bash script to check the network status - linux
I wrote a bash script: 我写了一个bash脚本:
RRR=$(ifconfig eth0 | grep 'inet addr:' | cut -d: -f2)
if [[ ${RRR} == null ]]; then
`zenity --error --text "NO NETWORK"`
else
`zenity --error --text "NETWORK IS ON"`
fi
but its not working fine - when i cut off the network the error message doesn't show on 但它不能正常工作-当我切断网络时,错误消息不会显示
any suggestions? 有什么建议么?
thanks' ahead 谢谢'
I think ping is help you as alternative But you already solved it interesting to popup message window with network status. 我认为ping可以替代您的问题,但是您已经解决了弹出带有网络状态的消息窗口的有趣问题。
ping -q -w 1 -c 1 `ip r | grep default | cut -d ' ' -f 3` > /dev/null && echo "NETWORK IS ON" || echo "NO NETWORK"
or 要么
ROUTER_IP="your router ip"
( ! ping -c1 $ROUTER_IP >/dev/null 2>&1 ) && service network restart >/dev/null 2>&1
try this: 尝试这个:
find /proc/irq/ -name \*eth0\* | fgrep -q eth0 && echo up || echo down
if the interface is loaded it will apear 如果接口被加载,它将出现
root@stormtrooper:/proc# ifconfig eth0 down
root@stormtrooper:/proc# find /proc/irq/ -name \*eth0\* | fgrep -q eth0 && echo up || echo down
down
root@stormtrooper:/proc# ifconfig eth0 up
root@stormtrooper:/proc# find /proc/irq/ -name \*eth0\* | fgrep -q eth0 && echo up || echo down
up
I don't know if it will refear to linkstate... but use /proc is always faster 我不知道它是否会拒绝链接状态...但是使用/ proc总是更快
You can use ping command to analyze connection quality. 您可以使用ping命令分析连接质量。 I use this function to test current interface in linux.
我使用此功能来测试linux中的当前接口。 It's ping destination address 10 times and return 0 - if succeed, 1- otherwise.
对目标地址执行ping操作10次,然后返回0-如果成功,则返回1-。 It just one of possibility.
这只是可能性之一。
param1 - interface name (eth0, tun0...); param1-接口名称(eth0,tun0 ...); param2 - ping destination
param2-ping目标
ping_interface() {
# Max value of losted packages in %
MAX_PACKETS_LOST=80
PACKETS_COUNT=10
PACKETS_LOST=$(ping -c $PACKETS_COUNT -I $1 $2 |grep % | awk '{print $7}')
if ! [ -n "$PACKETS_LOST" ] || [ "$PACKETS_LOST" == "100%" ];
then
# 100% failed
return 1
else
if [ "${PACKETS_LOST}" == "0%" ];
then
#ping is OK
return 0
else
# Real value of losted packets between 0 and 100%
REAL_PACKETS_LOST=$(echo $PACKETS_LOST | sed 's/.$//')
if [[ ${REAL_PACKETS_LOST} -gt ${MAX_PACKETS_LOST} ]];
then
echo "Failed. Lost more then limit"
return 1
else
echo "Connection is ok."
return 0
fi
fi
fi
}
ping_interface eth0 8.8.8.8
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.