簡體   English   中英

Bash腳本檢查網絡狀態-Linux

[英]Bash script to check the network status - linux

我寫了一個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

但它不能正常工作-當我切斷網絡時,錯誤消息不會顯示

有什么建議么?

謝謝'

我認為ping可以替代您的問題,但是您已經解決了彈出帶有網絡狀態的消息窗口的有趣問題。

ping -q -w 1 -c 1 `ip r | grep default | cut -d ' ' -f 3` > /dev/null && echo "NETWORK IS ON" || echo "NO NETWORK"

要么

 ROUTER_IP="your router ip"
    ( ! ping -c1 $ROUTER_IP >/dev/null 2>&1 ) && service network restart >/dev/null 2>&1

嘗試這個:

find /proc/irq/ -name \*eth0\* | fgrep -q eth0 && echo up || echo down

如果接口被加載,它將出現

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

我不知道它是否會拒絕鏈接狀態...但是使用/ proc總是更快

您可以使用ping命令分析連接質量。 我使用此功能來測試linux中的當前接口。 對目標地址執行ping操作10次,然后返回0-如果成功,則返回1-。 這只是可能性之一。

param1-接口名稱(eth0,tun0 ...); 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM