mdns-repeater-mikrotik/run.sh

47 lines
1.5 KiB
Bash

#!/bin/bash
# on error exit
set -e
# set vlan parent interface
INTERFACE=eth0
# assign vlan ids via commandline parameter 1
VLANS=$1
if [[ -z "$VLANS" ]];then
echo -e "No VLAN-IDs assigned through environment variable with name 'VLANS'!\nExit container."
exit 1
fi
# build interface list with parent interface and vlan ids
ALLVLANIF=$(echo -n $VLANS | sed -re "s/\b([0-9]+)/${INTERFACE}.\1/g")
# get MTU of parent interface
MTU=$(ip link show $INTERFACE | grep -Ei 'mtu \d+' | cut -d " " -f2)
# cleanup non used vlan interfaces
for VLAN in $(ls -1 /sys/class/net | grep -E "^${INTERFACE}\\." | grep -Ev "$(echo $ALLVLANIF | tr ' ' '|')") ;do
echo "Deleting orphaned interface: $VLAN"
ip link delete $VLAN
done
# for each vlan id
for VLAN in $VLANS; do
VLANIF="${INTERFACE}.$VLAN"
# create vlan interface
[[ ! -d "/sys/class/net/${VLANIF}" ]] && {
echo "Creating interface ${VLANIF}"
ip link add link $INTERFACE name "$VLANIF" mtu $MTU type vlan id $VLAN
}
echo "Bring up $VLANIF interface"
ip link set "$VLANIF" up
# starting DHCP client on interface
[[ -f "/var/run/udhcpc.${VLANIF}.pid" ]] && {
kill "$(cat "/var/run/udhcpc.${VLANIF}.pid")" >/dev/null
rm "/var/run/udhcpc.${VLANIF}.pid"
}
echo "Starting dhcp client on ${VLANIF}"
udhcpc -b -i $VLANIF -x hostname:"$HOSTNAME" -p "/var/run/udhcpc.${VLANIF}.pid"
done
echo "Starting mDNS repeater process ... "
# run repeater on vlan interfaces
exec /bin/mdns-repeater -f $ALLVLANIF