首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Bash脚本在循环上打开和向下打开一个接口

Bash脚本在循环上打开和向下打开一个接口
EN

Stack Overflow用户
提问于 2012-04-20 09:48:08
回答 1查看 15.8K关注 0票数 1

我使用Ubuntu服务器作为NAT路由器。WAN接口为eth1,局域网接口为eth0。我在局域网端使用ucarp虚拟ip进行故障转移。我正在编写一个脚本,如果WAN链路或默认网关出现故障,将导致局域网接口eth0下降(如果局域网接口中断,则ucarp可以将NAT网关ip释放到网络上的另一个路由器)。另外,如果WAN ip被点击,那么LAN接口应该会出现,并且应该保持向上,直到WAN可以被点击。

Bash脚本:

代码语言:javascript
运行
复制
#!/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

剧本对我不管用。我想要的是,如果一个广域网ip可以点击,那么局域网接口eth0应该保持上升。如果不能点击广域网ip,则应该关闭接口。脚本应该每10秒在循环上运行一次。如果广域网ip在较长的一段时间内不能切换,那么eth0只应该保持下来,如果广域网ip在一段时间后被点击,那么就应该提出eth0。我还计划在启动时将脚本作为一个启动作业运行。

编辑1:我的最终脚本:

代码语言:javascript
运行
复制
#!/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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-04-20 10:43:55

试试这个

如果ping成功,那么就启动$iface

如果ping失败,那么将$iface降下来

代码语言:javascript
运行
复制
#!/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
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10243845

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档