确定上行链路接口是否可用

确定上行链路接口是否可用

我有一个防火墙系统,由两个上行链路提供服务,这些上行链路连接到不同的 ISP。我使用 shorewall 来管理防火墙;它通过连接跟踪在两个接口上平衡流量负载。所有这一切都运行良好。

我编写了一些脚本,当检测到某个接口不再可用时,它会打开/关闭接口。我不喜欢 shorewall 提供的 LSM(Ubuntu 中没有),而是选择使用 netplugd 进行接口监控,并使用 cron 中的 ping 脚本进行连接测试。

ping 脚本基本上只执行一个操作ping -I eth[01] 8.8.8.8。如果 ping 失败,它会再次尝试,如果还失败,它会告诉 shorewall 关闭该接口。

我的问题发生在防火墙使接口停止使用时,然后我就无法再执行。除了 ping 直接网关外,ping它总是响应。Destination Host Unreachable

有没有办法通过接口测试真实的互联网连接,而无需通过它发送常规流量?

以下是我编写的脚本:

检查接口:(从 cron 和 netplugd 事件运行)

#!/bin/bash
#
# Verify state of interfaces
#
google_ping() {
        if ! ping -I "$1" -n -c1 -w1 -q 8.8.8.8 >/dev/null 2>&1; then
                # Try harder
                ping -I "$1" -n -c2 -w5 -q 8.8.8.8 >/dev/null 2>&1
        fi
}

REFRESH=
for i in eth0 eth1; do
        if google_ping $i; then
                if ! [ -e /tmp/shorewall-$i.up ]; then
                        echo Interface $i came up
                        touch /tmp/shorewall-$i.up
                        REFRESH=true
                fi
        else
                if [ -e /tmp/shorewall-$i.up ]; then
                        echo Interface $i went down
                        rm /tmp/shorewall-$i.up
                        REFRESH=true
                fi
        fi
done
if [ -n "$REFRESH" ]; then
        echo Kicking shorewall...
        /sbin/shorewall refresh >/dev/null
fi

(我知道 shorewall 允许您关闭接口,/var/lib/shorewall/firewall disable eth1但我遇到了一些不稳定性,所以我只是使用刷新)

/etc/shorewall/可用

# Used by shorewall to check if interface is usable
# This is sourced instead of executed so don't exit but return

# Interface is up
if [ -e "/tmp/shorewall-$1.up" ]; then
        return 0
elif ls /tmp/shorewall-*.up >/dev/null 2>&1; then
        return 1
else
        # No interfaces are up - pretend they're up and hope for the best
        return 0
fi

相关内容