fix-ip6-default-route.sh: Added script to monitor IPv6 default route
[mgsautils.git] / host / fix-ip6-default-route.sh
1 #!/bin/sh
2 export PATH=${PATH}:/sbin
3 NETCFG=/etc/network/interfaces
4
5 [ "$1" = "-v" ] && DEBUG="printf" || DEBUG="true"
6
7 lastiface=""
8 lastgateway=""
9
10 while read -r line
11 do
12 if echo "${line}" | grep -q "[ \t]*#"
13 then
14 false
15 elif echo "${line}" | grep -q "^iface [^ ]* inet6 [^ ]*$"
16 then
17 echo "${line}" | ( IFS=' ' read -r _ iface _ method
18 "${DEBUG}" "Found %s with method %s.\n" "${iface}" "${method}"
19 if [ "${method}" != "static" ]
20 then
21 "${DEBUG}" "Interface %s does not use static configuration. Ambiguous config, exiting.\n" "${iface}"
22 exit 2
23 fi
24 ) || exit $?
25 lastiface="${line}"
26 elif [ -n "${lastiface}" ]
27 then
28 if gateway=$(echo "${line}" | grep -o "gateway *[0-9a-fA-F:]*:[0-9a-fA-F:]*")
29 then
30 if [ -z "${lastgateway}" ]
31 then
32 lastgateway="${gateway}"
33 "${DEBUG}" "Found %s for %s.\n" "${lastgateway}" "${lastiface}"
34 else
35 "${DEBUG}" "A gateway was already defined for another interface. Ambiguous situation, exiting.\n"
36 exit 3
37 fi
38 fi
39 fi
40 done < "${NETCFG}"
41
42 if [ -z "${lastiface}" ]
43 then
44 "${DEBUG}" "No inet6 interface config found.\n"
45 exit 1
46 else
47 iface=$(echo "${lastiface}" | grep -oP "(?<=iface )[^ ]*(?= inet6)")
48 "${DEBUG}" "Checking if %s needs to be reconfigured...\n" "${iface}"
49 fi
50
51 defroute=$(ip -6 route | grep "^default ")
52 if [ -z "${defroute}" ] || echo "${defroute}" | grep -q " proto ra "
53 then
54 # If we get here, we will provide output of our actions. It is not affected by verbose mode.
55 printf "No default route, or it is RA-configured. Reconfiguring %s...\n" "${iface}"
56 ifdown -v "${iface}"
57 ip link set "${iface}" down
58 ifup -v "${iface}"
59 else
60 "${DEBUG}" "Found healthy default route: %s.\nNo action is necessary.\n" "${defroute}"
61 fi