English 中文(简体)
Bash 铺设和铺设通道的接口
原标题:Bash script to bring up and down an interface on loop

我将乌班图服务器用作NAT路由器。 广域网接口为eth1,局域网界面为0。 我在局域网一侧使用木卡普虚拟管道进行故障处理。 如果广域网连接或缺省门缩小(如果局域网接口缩小,则ucarp可以将NAT网关排到网络的另一路由器),我将写下一个文字。 而且,如果广域网管道铺设,则应当建立局域网接口,直到广域网能够铺设为止。

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

文字对我不可行。 我想的是,如果能够打上广域网,那么局域网界面eth0就应当保持顶。 如果广域网无法铺设,那么接口就应当缩小。 该书应每10秒钟使用。 如果广域网不能长时间铺设,那么eth0就应当只剩下,如果广域网在一段时间后铺设,那么0。 我也计划把script作为起步工作。

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
" "$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
" "$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
" "$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
" "$iface"
    fi
    sleep "$timeout"
done
问题回答

暂无回答




相关问题
What does it mean "to write a web service"?

I just asked a question about whether it was possible to write a web-page-checking code and run it from free web server, and one supporter answered and said that it was possible only if I run "a web ...

How can I use exit codes to run shell scripts sequentially?

Since cruise control is full of bugs that have wasted my entire week, I have decided the existing shell scripts I have are simpler and thus better. Here is what I have so far svn update /var/www/...

Dynamically building a command in bash

I am construcing a command in bash dynamically. This works fine: COMMAND="java myclass" ${COMMAND} Now I want to dynamically construct a command that redirectes the output: LOG=">> myfile.log ...

Why does Scala create a ~/tmp directory when I run a script?

When I execute a Scala script from the command line, a directory named "tmp" is created in my home directory. It is always empty, so I simply deleted it without any apparent problem. Of course, when I ...

Ivy, ant and start scripts

I have a project that uses ant to build and ivy for dependencies. I would like to generate the start scripts for my project, with the classpath, based on the dependencies configured in Ivy, ...

热门标签