bash脚本把上下环路接口 [英] Bash script to bring up and down an interface on loop

查看:169
本文介绍了bash脚本把上下环路接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Ubuntu服务器作为NAT路由器。广域网接口是的eth1 和LAN接口的eth0 。我用ucarp虚拟IP上的LAN端故障转移。我写一个脚本,它会拉低LAN接口的eth0 如果WAN链路或默认网关出现故障(如LAN接口出现故障,那么ucarp可以释放NAT网关IP到另一个路由器在网络上)。此外,如果WAN口IP被ping通,那么LAN接口应该拿出,并应继续,直到WAN IP可以Ping通。

bash脚本:

 #!/斌/庆典
T1 = $(使用ifconfig | grep的-o为eth0)
T2 =eth0的
#RMT_IP =8.8.8.8
SLEEP_TIME =10平-c 2 8.8.8.8>的/ dev / null的
   PING_1 = $?如果[$ PING_1 = 1];然后
    如果[$ T1=$ T2!]然后
        ifconfig eth0命令了
        回声IFACE长大
    其他
        回声IFACE已经是
    科幻
其他
    如果[$ T1=$ T2];然后
        ifconfig eth0命令下来
        回声IFACE放倒
    其他
        回声IFACE已经下降
    科幻
科幻睡眠$ SLEEP_TIME

该脚本不会为我工作。我想,如果一个WAN IP可以Ping通,然后LAN接口的eth0 应保持向上。如果WAN IP无法ping通,那么接口应该被放倒。该脚本应在循环运行每隔10秒。如果WAN IP地址不能被ping通了较长一段时间内则的eth0 应该保持只跌,如果WAN IP得到了一些时间,那么的eth0 <后ping通/ code>应长大。我还计划运行开机脚本作为一个暴发户的工作。

编辑1:
我最后的脚本:

 #!/斌/庆典检查之间的超时= 5#延迟
pingip ='8.8.8.8'#来ping什么
IFACE =eth0的
LOG_FILE =在/ var / log / syslog的
isdown = 0#指示接口是否是涨还是跌
                   #开始假设接口达而真实的;做
    LOG_TIME =`日期+%B''%D''%T`
    如果平-q -c 2$ pingip&GT;&GT;的/ dev / null的;那么#平是好的 - 带来了IFACE
        如果[$ isdown-ne 0];然后
            ifup的$ IFACE&放大器;&安培; isdown = 0
            printf的$ LOG_TIME $ 0:接口提出了:%S \\ n$ IFACE|三通-a $ LOG_FILE
        科幻
    其他#平是坏的 - 把IFACE下降
        蜂鸣-f 4000
        如果[$ isdown-ne 1];然后
            ifdown你$ IFACE&放大器;&安培; isdown = 1
            printf的$ LOG_TIME $ 0:接口放倒:%S \\ n$ IFACE|三通-a $ LOG_FILE
        科幻
    科幻
    休眠$超时
DONE


解决方案

试试这个结果
如果ping成功然后将 $ IFACE 起来结果
如Ping不通然后将 $ IFACE

 #!/斌/庆典检查之间的超时= 3#延迟
IFACE =eth0的#带来哪些接口上/下
pingip ='8.8.8.8'#来ping什么
isdown = -1#显示接口是否启动(0)或下跌(1)
                        处于未知状态#启动而真实的;做
    如果平-q -c 2$ pingip然后#如果ping操作成功,带来了IFACE
        如果[$ isdown-ne 0];然后#如果没有了
            使用ifconfig$ IFACE向上和&安培; isdown = 0
            printf的:: IFACE提出了:%S \\ n$ IFACE
        科幻
    ELIF [$ isdown-ne 1];然后#如果平失败,带来IFACE下来,如果不是已经下来
        使用ifconfig$ IFACE向下放大器;&安培; isdown = 1
        printf的:: IFACE放倒:%S \\ n$ IFACE
    科幻
    休眠$超时
DONE

I use Ubuntu server as NAT router. WAN interface is eth1 and 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 eth0 if 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.

Bash Script:

#!/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 eth0 should 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 eth0 should remain down only and if the WAN ip gets pinged after some time then eth0 should be brought up. I also plan to run the script on boot up as an upstart job.

EDIT 1: My final script:

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

解决方案

try this one
if ping succeeds then bring $iface up
if ping fails then bring $iface down

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

这篇关于bash脚本把上下环路接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆