zefie icon

zefie openwrt wan2 + traffic marking

zefie | PRO | 08/27/18 01:35:45 PM UTC | 0 ⭐ | 897 👁️ | Never ⏰ | []
Bash |

2.52 KB

|

None

|

0 👍

/

0 👎

#!/bin/ash
Z_WAN_IF="eth0"
Z_WAN2_IF="eth2"
Z_WAN2_MAC="A0:63:91:B8:86:BC"
Z_TABLE="200"
Z_WAIT=5
Z_COUNT=0
 
# Create interface if it doesn't exist
if [ $(ip link | grep "${Z_WAN_IF}" | grep UP | wc -l) -gt 0 ]; then
        if [ $(ip link | grep "${Z_WAN2_IF}" | wc -l) -lt 1 ]; then
                ip link add link ${Z_WAN_IF} ${Z_WAN2_IF} type macvlan
                if [ $? -ne 0 ]; then exit; fi
                ifconfig ${Z_WAN2_IF} hw ether "${Z_WAN2_MAC}"
                if [ $? -ne 0 ]; then exit; fi
 
                # Wait for interface
                Z_COUNT=0
                while [ ${Z_COUNT} -lt ${Z_WAIT} ]; do
                        if [ $(ip link | grep "${Z_WAN2_IF}" | wc -l) -eq 1 ]; then
                                break;
                        fi
                        sleep 1
                        Z_COUNT=$(expr ${Z_COUNT} + 1)
                done
        fi
fi
 
if [ $(ip link | grep "${Z_WAN2_IF}" | grep UP | wc -l) -eq 1 ] && [ -z "${1}" ]; then
        Z_WAN_IP="$(ip addr | grep "${Z_WAN_IF}" -A2 | grep inet | grep "/24" | cut -d' ' -f6 | cut -d'/' -f1)"
        if [ "${Z_WAN_IP}" == "" ]; then exit; fi
        if [ $(ip route show table ${Z_TABLE} | grep "${Z_WAN2_IF}" | wc -l) -eq 0 ]; then
                Z_COUNT=0
                while [ ${Z_COUNT} -lt ${Z_WAIT} ]; do
                        Z_WAN2_IP="$(ip addr | grep "${Z_WAN2_IF}" -A2 | grep inet | grep "/24" | cut -d' ' -f6 | cut -d'/' -f1)"
                        if [ "${Z_WAN2_IP}" != "" ]; then break; fi
                        Z_COUNT=$(expr ${Z_COUNT} + 1)
                        sleep 1
                done
                if [ "${Z_WAN2_IP}" == "" ]; then exit; fi
                Z_WAN2_GATEWAY="$(echo ${Z_WAN2_IP} | cut -d'.' -f1,2,3).254"
                ip route flush table ${Z_TABLE}
                ip route show table main | grep -Ev ^default | while read ROUTE ; do ip route add table ${Z_TABLE} ${ROUTE} ; done
                ip route add default via ${Z_WAN2_GATEWAY} dev ${Z_WAN2_IF} src ${Z_WAN2_IP} table ${Z_TABLE}
                ip rule add fwmark ${Z_TABLE} table ${Z_TABLE}
                ip route flush cache
        fi
        iptables -A OUTPUT -o eth2 -j MARK --set-mark ${Z_TABLE}
 
        # Set WAN2 Flag for Free Wifi
        iptables -t mangle -A PREROUTING -i wlan1-1 -j MARK --set-mark ${Z_TABLE}
 
        # Set WAN2 Flag for Zefie Server
        iptables -t mangle -A PREROUTING -m mac --mac-source 1C:75:08:11:83:96 -j MARK --set-mark ${Z_TABLE}
 
        exit 0;
fi

Comments