dhclient ant policy based routing
Kalnozols, Andris
andris at hpl.hp.com
Thu Jan 23 21:08:01 UTC 2014
Hello, Richard.
I'm in the midst of writing a single hook script to perform dynamic
DNS updates and discovered that is insufficient to rely solely on the
"$reason" variable. To generalize, the "BOUND|RENEW|REBIND|REBOOT"
is important to the dhclient-exit-hooks context; the client's
"$interface" is operational with the "$new_*" DHCP variables.
Conversely, "RELEASE|EXPIRE|FAIL|STOP" is important in the
dhclient-enter-hooks context because the client's "$interface"
may be about to stop using the "$old_*" DHCP variables. Again,
this is a broad generalization.
Here is a Bash function that can be used to determine if your
script has been sourced in the "enter" context or the "exit"
context.
------
Andris
> # Although this script can be run as a stand-alone program, its
> # primary purpose is to be *sourced* by dhclient-script(8) as
> # either "dhclient-enter-hooks" or "dhclient-exit-hooks".
> # It's crucial to know in which context we are running so that
> # the DHCP environment variables do not get accidentally changed.
> # Inspect the builtin arrays BASH_SOURCE and FUNCNAME to make
> # this determination.
> #
> # Parameters: none
> #
> # Returns: The following global variables are set:
> #
> # Context ... The name our caller used to run this script
> # NOTE: This is distinct from the "$0" argument.
> # If this script was sourced, then "$0"
> # will be the name of the calling program,
> # *not* the name of this script.
> # Sourced ... Set to "true" if this script was sourced, i.e.,
> # run as a "." script. Otherwise, the value of
> # "${FUNCNAME[depth]}" will be "main" which
> # indicates the script was run as a stand-alone
> # program.
> #
> # Return value: 0 - always returned
> #
> get_context() {
>
> local depth
>
> ((depth = ${#BASH_SOURCE[@]} - 1))
> if ((depth >= 1))
> then
> Context="${BASH_SOURCE[1]}"
> [[ ${FUNCNAME[1]} = "source" ]] && Sourced="true"
> fi
> return 0
> }
====================================================================
On 1/23/2014 11:25 AM, richard lucassen wrote:
> Hello list,
>
> I have a linuxbox (Debian) with a fixed routed subnet on eth0. The eth0
> is the default gateway in the default route table.
>
> On eth2 I have a dynamic (non-rfc1918) address docsis router. I want
> the following things to be realized:
>
> 1) run dhclient on eth2
> 2) dhclient on eth2 must NOT set default gw on default route table
> 3) dhclient must fill a user defined route table
> 4) dhclient must set def gw on that user defined route table
> 5) dhclient must set policy based routing
> 6) dhclient must clear all of the above on stop
>
> Ok, I managed this. It works perfectly well. I just would like to have
> some feedback on the "case" statement like BOUND|RENEW|REBIND|REBOOT
> for activating the rules and FAIL|NBI|STOP|TIMEOUT|RELEASE|EXPIRE to
> clear the rules.
>
> Did I do this right or wrong?
>
> R.
>
>
> cat /etc/dhcp3/dhclient-enter-hooks.d/no-default-route
> #####################################################################
> # enter hooks script:
>
> case $reason in
>
> BOUND|RENEW|REBIND|REBOOT)
> # Do not set default gw, but create a copy to use it in exit hooks
> # scripts:
> export new_routers_copy=$new_routers
> # Avoid gw in default route table, but preserve copy for
> # policy based routing:
> unset new_routers
> ;;
>
> FAIL|NBI|STOP|TIMEOUT|RELEASE|EXPIRE)
> ip route del $old_network_number/$old_subnet_mask \
> dev $interface table 12
> ip route del default via $old_routers table 12
> ip rule del from $old_network_number/$old_subnet_mask lookup 12
> ;;
>
> esac
> #####################################################################
>
> cat /etc/dhcp3/dhclient-exit-hooks.d/no-default-route
>
> #####################################################################
> # exit hooks script:
>
> case $reason in
>
> BOUND|RENEW|REBIND|REBOOT)
> ip route add $new_network_number/$new_subnet_mask \
> dev $interface table 12
> ip route add default via $new_routers_copy table 12
> ip rule add from $new_network_number/$new_subnet_mask lookup 12
> ;;
>
> esac
>
> #####################################################################
>
>
>
More information about the dhcp-users
mailing list