Created
October 11, 2016 08:57
-
-
Save dan82840/6185b8f6351bde2d22feb476f5e976c8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# | |
. /lib/functions.sh | |
LOG_FILE=/tmp/vlan.log | |
START_VLAN_NUM=$1 | |
END_VLAN_NUM=$2 | |
usage() { | |
echo "test-vlan.sh [START_VLAN_NUM] [END_VLAN_NUM]" | |
} | |
check_args() { | |
local num=$1 | |
[ "$num" != "2" ] && { | |
usage | |
exit 1 | |
} | |
} | |
network_switch_vlan_cb() { | |
local section=$1 | |
config_get vlan $section vlan | |
if [ "$vlan" == "1" ] || [ "$vlan" == "2" ]; then | |
return | |
fi | |
uci delete network.$section | |
uci commit network | |
} | |
network_interface_cb() { | |
local section=$1 | |
local ifn | |
config_get ifname $section ifname | |
config_get type $section type | |
ifn=$(echo $ifname | sed 's/br-//') | |
if [ "$type" == "bridge" ] && [ "$ifname" != "br-lan" ]; then | |
uci delete network.$section | |
uci commit network | |
ifdown $ifn | |
fi | |
} | |
del_all_lan_vlan() { | |
echo "Deleting ALL LAN VLAN !!!" >> $LOG_FILE | |
config_load network | |
config_foreach network_switch_vlan_cb switch_vlan | |
config_foreach network_interface_cb interface | |
} | |
# $1: vlan id | |
add_vlan() { | |
local id=$1 | |
local name="lan$id" | |
echo "Adding VLAN $id !!!" >> $LOG_FILE | |
uci add network switch_vlan | |
uci set network.@switch_vlan[-1].device='switch0' | |
uci set network.@switch_vlan[-1].vlan="$i" | |
uci set network.@switch_vlan[-1].ports='0t 1t 2t 3t 4t' | |
uci set network.@switch_vlan[-1].vid="$id" | |
uci set network.@switch_vlan[-1].o_priority='0' | |
uci set network.$name=interface | |
uci set network.$name.ifname="eth1.$id" | |
uci set network.$name.proto='static' | |
uci set network.$name.type='bridge' | |
uci set network.$name.ipaddr="192.168.$id.1" | |
uci set network.$name.netmask='255.255.255.0' | |
uci set network.$name.gateway="192.168.$id.1" | |
uci commit network | |
} | |
main() { | |
echo "===================================" >> $LOG_FILE | |
del_all_lan_vlan | |
for i in $(seq $START_VLAN_NUM $END_VLAN_NUM); do | |
add_vlan "$i" | |
reload_config network & | |
done | |
echo "===================================" >> $LOG_FILE | |
} | |
check_args $# $START_VLAN_NUM $END_VLAN_NUM | |
main | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment