用于在循环中打开和关闭界面的 Bash 脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10243845/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 02:05:44  来源:igfitidea点击:

Bash script to bring up and down an interface on loop

bashshellunixnetworkingscripting

提问by nixnotwin

I use Ubuntu server as NAT router. WAN interface is eth1and LAN interface is eth0. I use ucarp virtual ip on LAN side for failover. I am writing a script which will bring down LAN interface eth0if WAN link or default gateway goes down (If LAN interface goes down, then ucarp can release the NAT gateway ip to another router on the network). Also if the WAN ip gets pinged then LAN interface should come up and should remain up until WAN ip can be pinged.

我使用 Ubuntu 服务器作为 NAT 路由器。WAN 接口是eth1,LAN 接口是eth0。我在 LAN 端使用 ucarp 虚拟 ip 进行故障转移。我正在编写一个脚本,eth0如果 WAN 链接或默认网关出现故障,它将关闭 LAN 接口(如果 LAN 接口出现故障,则 ucarp 可以将 NAT 网关 ip 释放到网络上的另一个路由器)。此外,如果 WAN ip 被 ping 则 LAN 接口应该出现并且应该保持直到可以 ping WAN ip。

Bash Script:

Bash 脚本:

#!/bin/bash
t1=$(ifconfig | grep -o eth0)
t2="eth0"
#RMT_IP = "8.8.8.8"
SLEEP_TIME="10"

ping -c 2 8.8.8.8 > /dev/null
   PING_1=$?

if [ $PING_1 = 1 ]; then
    if [ "$t1" != "$t2" ]; then
        ifconfig eth0 up
        echo "Iface brought up"
    else
        echo "Iface is already up"
    fi
else
    if [ "$t1" = "$t2" ]; then
        ifconfig eth0 down
        echo "Iface brought down"
    else
        echo "iface already down"
    fi
fi

sleep $SLEEP_TIME

The script does not work for me. What I want is, if a WAN ip can be pinged then the LAN interface eth0should remain up. If the WAN ip cannot be pinged, then the interface should be brought down. The script should run on loop every 10 seconds. If the WAN ip cannot be pinged for extended period of time then eth0should remain down only and if the WAN ip gets pinged after some time then eth0should be brought up. I also plan to run the script on boot up as an upstart job.

该脚本对我不起作用。我想要的是,如果可以 ping WAN ip,则 LAN 接口eth0应该保持打开状态。如果无法 ping 通 WAN ip,则应关闭接口。该脚本应每 10 秒循环运行一次。如果 WAN ip 无法在很长一段时间内被 ping 通,那么eth0应该只保持关闭状态,如果 WAN ip 在一段时间后被 ping 通,则eth0应该启动。我还计划在启动时运行脚本作为一个新贵的工作。

EDIT 1:My final script:

编辑 1:我的最终脚本:

#!/bin/bash

timeout=5         # delay between checks
pingip='8.8.8.8'   # what to ping
iface="eth0"
LOG_FILE="/var/log/syslog"
isdown=0            # indicate whether the interface is up or down
                   # start assuming interface is up

while true; do
    LOG_TIME=`date +%b' '%d' '%T`
    if ping -q -c 2 "$pingip" >> /dev/null ; then      # ping is good - bring iface up
        if [ "$isdown" -ne 0 ] ; then
            ifup $iface && isdown=0
            printf "$LOG_TIME 
#!/bin/bash

timeout=3               # delay between checks
iface="eth0"            # which interface to bring up/down
pingip='8.8.8.8'        # what to ping
isdown=-1               # indicate whether the interface is up(0) or down(1)
                        # start in unknown state

while true; do
    if ping -q -c 2 "$pingip"; then       # if ping is succeeds bring iface up
        if [ "$isdown" -ne 0 ]; then      # if not already up
            ifconfig "$iface" up && isdown=0
            printf ":: iface brought up: %s\n" "$iface"
        fi
    elif [ "$isdown" -ne 1 ]; then        # if ping failed, bring iface down, if not already down
        ifconfig "$iface" down && isdown=1
        printf ":: iface brought down: %s\n" "$iface"
    fi
    sleep "$timeout"
done
: Interface brought up: %s\n" "$iface" | tee -a $LOG_FILE fi else # ping is bad - bring iface down beep -f 4000 if [ "$isdown" -ne 1 ] ; then ifdown $iface && isdown=1 printf "$LOG_TIME ##代码##: Interface brought down: %s\n" "$iface" | tee -a $LOG_FILE fi fi sleep "$timeout" done

回答by c00kiemon5ter

try this one
if ping succeeds then bring $ifaceup
if ping fails then bring $ifacedown


如果 ping 成功,请尝试此操作$iface
如果 ping 失败则启动$iface,然后关闭

##代码##