This repository has been archived on 2024-06-05. You can view files and clone it, but cannot push or open issues or pull requests.
mdns-repeater-mikrotik/run.sh

47 lines
1.5 KiB
Bash
Raw Normal View History

2022-07-05 08:20:32 +02:00
#!/bin/bash
2023-06-10 10:01:06 +02:00
# on error exit
2022-07-05 08:20:32 +02:00
set -e
2023-06-10 10:01:06 +02:00
# 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")
2022-07-05 08:20:32 +02:00
2023-06-10 10:01:06 +02:00
# get MTU of parent interface
MTU=$(ip link show $INTERFACE | grep -Ei 'mtu \d+' | cut -d " " -f2)
2022-07-05 08:20:32 +02:00
2023-06-10 10:01:06 +02:00
# 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
2022-07-05 08:20:32 +02:00
2023-06-10 10:01:06 +02:00
# for each vlan id
2022-07-05 08:20:32 +02:00
for VLAN in $VLANS; do
2023-06-10 10:01:06 +02:00
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
2022-07-05 08:20:32 +02:00
}
2023-06-10 10:01:06 +02:00
echo "Bring up $VLANIF interface"
ip link set "$VLANIF" up
2022-07-05 08:20:32 +02:00
2023-06-10 10:01:06 +02:00
# 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"
2022-07-05 08:20:32 +02:00
}
2023-06-10 10:01:06 +02:00
echo "Starting dhcp client on ${VLANIF}"
udhcpc -b -i $VLANIF -x hostname:"$HOSTNAME" -p "/var/run/udhcpc.${VLANIF}.pid"
2022-07-05 08:20:32 +02:00
done
2023-06-10 10:01:06 +02:00
echo "Starting mDNS repeater process ... "
# run repeater on vlan interfaces
exec /bin/mdns-repeater -f $ALLVLANIF