Skip to content

Instantly share code, notes, and snippets.

@wmono
Created October 27, 2010 15:24
Show Gist options
  • Save wmono/649250 to your computer and use it in GitHub Desktop.
Save wmono/649250 to your computer and use it in GitHub Desktop.
OpenVPN dhcp-options handlers for Mac OS X (Snow Leopard)
#!/bin/bash
# Down script for OpenVPN
# Copyright (c) 2010 eqv.ca
# Licensed under New BSD http://www.opensource.org/licenses/bsd-license.php
NETSTAT=/usr/sbin/netstat
NETWORKSETUP=/usr/sbin/networksetup
# Override DHCP values or leave blank
DNS=
SEARCH=
DEFAULT_IFACE=`$NETSTAT -nr -f inet | grep '^default' | awk '{print $6}'`
DEFAULT_HWPORT=$(
$NETWORKSETUP -listallhardwareports | \
while read line; do
if [[ $line =~ ^"Hardware Port: "(.*) ]]; then
HWPORT=${BASH_REMATCH[1]}
fi
if [[ $line =~ ^"Device: $DEFAULT_IFACE"$ ]]; then
echo $HWPORT
exit 0
fi
done)
if [ -z "$DEFAULT_IFACE" ]; then
echo "Warning: Could not detect default gateway interface."
echo "DHCP options will not be reset."
exit 0
fi
if [ -z "$DEFAULT_HWPORT" ]; then
echo "Warning: Could not detect hardware port for default gateway interface."
echo "DHCP options will not be reset."
exit 0
fi
$NETWORKSETUP -setdnsservers $DEFAULT_HWPORT ${DNS-empty}
$NETWORKSETUP -setsearchdomains $DEFAULT_HWPORT ${SEARCH-empty}
#!/bin/bash
# Up script for OpenVPN
# Copyright (c) 2010 eqv.ca
# Licensed under New BSD http://www.opensource.org/licenses/bsd-license.php
NETSTAT=/usr/sbin/netstat
NETWORKSETUP=/usr/sbin/networksetup
# Prepend DHCP options or leave blank
DNS=
SEARCH=
FOREIGN_OPTS="${!foreign_option_@}"
for var in $FOREIGN_OPTS; do
VALUE="${!var}"
if [[ "$VALUE" =~ ^"dhcp-option DNS "(.*) ]]; then
DNS="$DNS${BASH_REMATCH[1]} "
elif [[ "$VALUE" =~ ^"dhcp-option DOMAIN "(.*) ]]; then
SEARCH="$SEARCH${BASH_REMATCH[1]} "
else
echo "Warning: foreign option $VALUE not recognized."
fi
done
if [ -z "$DNS" -a -z "$SEARCH" ]; then
# No options were sent
exit 0
fi
DEFAULT_IFACE=`$NETSTAT -nr -f inet | grep '^default' | awk '{print $6}'`
DEFAULT_HWPORT=$(
$NETWORKSETUP -listallhardwareports | \
while read line; do
if [[ $line =~ ^"Hardware Port: "(.*) ]]; then
HWPORT=${BASH_REMATCH[1]}
fi
if [[ $line =~ ^"Device: $DEFAULT_IFACE"$ ]]; then
echo $HWPORT
exit 0
fi
done)
if [ -z "$DEFAULT_IFACE" ]; then
echo "Warning: Could not detect default gateway interface."
echo "DHCP options will not be implemented."
exit 0
fi
if [ -z "$DEFAULT_HWPORT" ]; then
echo "Warning: Could not detect hardware port for default gateway interface."
echo "DHCP options will not be implemented."
exit 0
fi
if [ "$DNS" ]; then
$NETWORKSETUP -setdnsservers "$DEFAULT_HWPORT" $DNS
fi
if [ "$SEARCH" ]; then
$NETWORKSETUP -setsearchdomains "$DEFAULT_HWPORT" $SEARCH
fi
echo -n "DNS servers: "; $NETWORKSETUP -getdnsservers "$DEFAULT_HWPORT"
echo -n "DNS search path: "; $NETWORKSETUP -getsearchdomains "$DEFAULT_HWPORT"
@pquentin
Copy link

Since macOS 10.15 Catalina, the output of /usr/sbin/netstat has changed and I needed to use this instead:

DEFAULT_IFACE=`$NETSTAT -nr -f inet | grep '^default' | awk '{print $4}'`

ie. switch from $6 to $4. Using $NF (last column) also works for me on 10.14 as the Expire column is never filled for my default routes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment