Skip to content

Instantly share code, notes, and snippets.

@Tydus
Created December 7, 2016 14:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Tydus/ed51d7a90a44df6166ec4467259c223c to your computer and use it in GitHub Desktop.
Save Tydus/ed51d7a90a44df6166ec4467259c223c to your computer and use it in GitHub Desktop.
config virtual wi-fi APs
#!/bin/sh
#=======================================
# config_vwifi.sh
# Config virtual wi-fi (bridge to vlans)
#=======================================
# show which port you have by `robocfg show'
PORT_LIST="0t 1t 2t 3t 4t 8t"
# The LAN interface
LAN_IF="eth0"
# Suffix of IP address for each interface
SUFFIX=253
#=======================================
# Don't change these
#=======================================
VLAN_LIST="100 101 102"
VLAN100_IP="10.10.0.$SUFFIX/22"
VLAN101_IP="10.10.64.$SUFFIX/22"
VLAN102_IP="10.10.128.$SUFFIX/24"
# These are only candidates,
# will do nothing if the corresponding if is not present
VLAN100_WL="wl0.1 wl1.1"
VLAN101_WL="wl0.2 wl1.2"
VLAN102_WL="wl0.3 wl1.3"
#=======================================
# Utility Functions
#=======================================
prompt() {
echo $@ >&2
}
# Get all elements from $1 which presents in $2
# E.g.: filter "eth1 eth2 eth3" "eth2" => "eth2"
filter() {
source=$1
present=$2
dst=""
for i in $source; do
echo $present | grep -Eq "(^| )$i( |$)"
if [ $? -eq 0 ]; then
if [ "x$dst" == "x" ]; then
dst=$i
else
dst="$dst $i"
fi
fi
done
echo "$dst"
}
# Get all elements from $1 which NOT presents in $2
# E.g.: filterout "eth1 eth2 eth3" "eth2" => "eth1 eth3"
filterout() {
source=$1
present=$2
dst=""
for i in $source; do
echo $present | grep -Eq "(^| )$i( |$)"
if [ $? -ne 0 ]; then
if [ "x$dst" == "x" ]; then
dst=$i
else
dst="$dst $i"
fi
fi
done
echo "$dst"
}
for i in $VLAN_LIST; do
# Set switch
robocfg vlan $i ports "$PORT_LIST"
# Set vlan interface
vconfig add $LAN_IF $i
# Create br$i for vlan$i
brctl addbr br$i
brctl addif br$i vlan$i
# Remove all wireless from br0,
# and add them to br$i
eval "wifis=\$VLAN${i}_WL"
for j in $wifis; do
ifconfig $j &>/dev/null
if [ $? -eq 0 ]; then
brctl delif br0 $j
brctl addif br$i $j
else
prompt "Warning: wifi $j from vlan $i is not present"
fi
done
# Set link up
ifconfig vlan$i up
ifconfig br$i up
# Set up IP
eval "ip=\$VLAN${i}_IP"
ip a add $ip dev br$i
done
# We need to reconfigure and reset authentication daemon (eapd) according to new settings
# Set up all VWIFI wireless interfaces
# Note: lan*_ifname(s) MUST take the continuous numbers from 1
# E.g.:
# lan_ifnames="vlan1 eth1 eth2" # This is 0
# lan1_ifnames="vlan100 wl0.1 wl1.1"
# lan2_ifnames="vlan101 wl0.2 wl1.2"
VWIFI_WL=""
j=1
for i in $VLAN_LIST; do
eval "wl=\$VLAN${i}_WL"
# Add to lan$i_ifnames
nvram set lan${j}_ifnames="vlan$i $wl"
nvram set lan${j}_ifname="br$i"
j=$((j+1))
# Collect all vwifis
VWIFI_WL="$VWIFI_WL $wl"
done
# Remove all vwifis from LAN
lan_ifnames=`nvram get lan_ifnames`
#lan_ifnames_remain=""
#for i in $lan_ifnames; do
# echo $VWIFI_WL | grep -Eq "(^| )$i( |$)"
# if [ $? -ne 0 ]; then
# lan_ifnames_remain="$lan_ifnames_remain $i"
# fi
#done
lan_ifnames_remain=`filterout "$lan_ifnames" "$VWIFI_WL"`
nvram set lan_ifnames="$lan_ifnames_remain"
killall eapd
eapd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment