【问题标题】:Bash script to bring up and down an interface on loop用于在循环中打开和关闭界面的 Bash 脚本
【发布时间】:2012-04-20 09:48:08
【问题描述】:

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

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

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

编辑 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 $0: 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 $0: Interface brought down: %s\n" "$iface" | tee -a $LOG_FILE
        fi
    fi
    sleep "$timeout"
done

【问题讨论】:

  • 您可以使用logger 记录到系统日志,例如logger -t ifup "bringing iface $iface up",它会自动将消息转换为系统日志格式。您还应该引用您的变量(ifaceLOG_FILE)并且不要在printf 字符串中使用变量,将它们用作参数,例如printf "time is %s\n" "$time"。此外,大写变量是环境使用的约定,您应该使用小写的。最后,这应该适用于sh,你不需要#!/bin/bash,只需#!/bin/sh

标签: bash shell unix networking scripting


【解决方案1】:

试试这个
如果 ping 成功,则将 $iface 向上
如果 ping 失败,则将 $iface 关闭

#!/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

【讨论】:

  • 如果界面已经启动,10秒后脚本再次执行代码弹出界面,那不会影响性能吗?但是如果再次执行同一行,它不会显示错误。
  • 因为我不知道有一种直接的方法来检查 iface 是否启动,要确定它是否启动,我必须使用类似 ifconfig | grep eth0 的东西,这实际上要糟糕得多,原因 1 . 它以任何一种方式调用 ifconfig, 2. 管道生成一个子 shell,这意味着必须再次加载环境,以及 3. 加载并执行 grep,所有这些都决定 eth0 是否启动,如果没有重新执行 ifconfig提出来。所以,不,我认为这不值得。
  • 好吧,我介绍了isdown var,它在接口关闭时获取值1 (true)。所以 ifconfig up 只有在设置了 isdown 时才会执行。 ifupifdown 不是标准的。我的系统中没有它们。如果您愿意,可以使用它们,但不要指望您的脚本可以在其他地方工作。请记住这一点;)
  • 完成,仅当接口尚未关闭时才会调用ifconfig down(或ifdown)。默认情况下,假定接口已关闭。更改顶部的 isdown 的初始值(并适当地修复注释)
  • 那是因为#!/bin/sh 而不是使用bash shebang,((..)) 是 bash 语法。我将对其进行更改,使其与sh/POSIX 兼容。还有一些小装置;)
猜你喜欢
  • 1970-01-01
  • 2018-09-26
  • 1970-01-01
  • 2017-10-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多