--- badi/public_scripts/switchgate/dhclient-exit-hooks.d/switchgate-helper 2009/04/23 17:42:48 1.1.1.1 +++ badi/public_scripts/switchgate/dhclient-exit-hooks.d/switchgate-helper 2020/04/04 21:29:07 1.3 @@ -2,16 +2,62 @@ # dhclient-exit-hook: switchgate-helper RUN="yes" - +SWITCHGATE="/usr/local/sbin/switchgate" if [ "$RUN" = "yes" ]; then - SWITCHGATE_CONFDIR=$(/usr/local/sbin/switchgate -C) - - if [ ! -d $SWITCHGATE_CONFDIR ]; then - mkdir "$SWITCHGATE_CONFDIR" + # ipv4 or ipv6? + if [ -z "$(echo "$reason" | sed -e "s/^.*6$//")" ]; then + IF_FILENAME="dhcp6.${interface}" + IPV="IPv6" + else + IF_FILENAME="dhcp.${interface}" + IPV="IPv4" + fi + + # get writeable tmp dir from switchgate + SWITCHGATE_TMPDIR="$("$SWITCHGATE" -C)" + if [ $? -ne 0 ]; then + echo -e "Error: Failed to query switchgate:\n\n$SWITCHGATE_TMPDIR" + exit 1 + elif [ -z "$SWITCHGATE_TMPDIR" ]; then + echo "Error: No writeable dir from switchgate received. Please check." + exit 1 fi - # Register switchgate routers - echo "$new_routers" | sed -e "s/ /\n/g" > "$SWITCHGATE_CONFDIR"/dhcp.$interface + + case "$reason" in + + BOUND|RENEW|REBIND|REBOOT|BOUND6|RENEW6|REBIND6|REBOOT6) + + # check there is a gateway given + if [ -z "$new_routers" ]; then + echo "No $IPV gateway supplied from DHCP server." + # remove the gw file + if [ -f "$SWITCHGATE_TMPDIR/$IF_FILENAME" ]; then + rm "$SWITCHGATE_TMPDIR/$IF_FILENAME" + fi + exit 0 + fi + + # create writeable dir if not already existing + if [ ! -d "$SWITCHGATE_TMPDIR" ]; then + mkdir "$SWITCHGATE_TMPDIR" + fi + + echo "Adding DHCP supplied gateways to the list of gateways for switchgate: $new_routers" + + # Register switchgate routers + echo "$new_routers" | sed -e "s/ /\n/g" > "$SWITCHGATE_TMPDIR/$IF_FILENAME" + ;; + + *) + # remove the gw file + if [ -f "$SWITCHGATE_TMPDIR/$IF_FILENAME" ]; then + rm "$SWITCHGATE_TMPDIR/$IF_FILENAME" + fi + exit 0 + ;; + + esac fi