Skip to content

Instantly share code, notes, and snippets.

@jchappell82
Last active February 12, 2017 18:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jchappell82/c5c9e0fa7170fd244f8d51737e2e5ce5 to your computer and use it in GitHub Desktop.
Save jchappell82/c5c9e0fa7170fd244f8d51737e2e5ce5 to your computer and use it in GitHub Desktop.
#!/bin/bash
# This script came from ubnt.com forum user "bradd" in the following post
# http://community.ubnt.com/t5/EdgeMAX/Automatic-DNS-resolution-of-DHCP-client-names/td-p/651311
# It has been modified by Ubiquiti to update the /etc/host file
# instead of adding to the CLI.
# Thanks to forum user "itsmarcos" for bug fix & improvements
# Thanks to forum user "ruudboon" for multiple domain fix
# Thanks to forum user "chibby85" for expire patch and static-mapping
# Thanks to forum user "meyergru" for patch for static-mapping with domain
if [ $# -lt 5 ]; then
echo Invalid args
logger -s -t on-dhcp-event "Invalid args \"$@\""
exit 1
fi
action=$1
base_client_name=$2
client_ip=$3
client_mac=$4
domain=$5
file=/etc/hosts
changes=0
# First, prepare the client_name variable to guarantee it's only got valid
# characters.
# This prepares the client_name as follows:
# 1) Deletes whitespace.
# 2) Deletes non alphanumeric, non-underscore/hyphen, or non-period chars.
# Example: 'gam\!~ma box.local.foo.net' becomes 'gammabox.local.foo.net'
clean_client_name=`echo $base_client_name | tr -c -ds '[:alnum:]_.-'`
# Split the client_name into an array at each period, in case the client sent
# an FQDN.
client_array=(${clean_client_name//./ })
# Finally, assign the actual client name from the array.
client_name=${client_array[0]}
if [[ ! "$client_name" =~ ^[0-9a-zA-Z_-]{1,255}$ ]]; then
logger -s -t on-dhcp-event "Invalid client name: $client_name"
exit 1
fi
if [ "$domain" != "..YYZ!" ] && [[ ! "$domain" =~ ^[0-9a-zA-Z\._-]{1,255}$ ]]; then
logger -s -t on-dhcp-event "Invalid domain: $domain"
exit 1
fi
if [ "$domain" == "..YYZ!" ]; then
client_fqdn_name=$client_name
client_search_expr=$client_name
elif [[ $client_name == *".$domain" ]]; then
client_fqdn_name=$client_name
client_search_expr=$client_name
else
client_fqdn_name=$client_name.$domain
client_search_expr="$client_name\\.$domain"
fi
case "$action" in
commit) # add mapping for new lease
echo "- new lease event, setting static mapping for host "\
"$client_fqdn_name (MAC=$client_mac, IP=$client_ip)"
#
# grep fails miserably with \t in the search expression.
# In the following line one <Ctrl-V> <TAB> is used after $client_search_expr
# followed by a single space
grep -q " $client_search_expr #on-dhcp-event " $file
if [ $? == 0 ]; then
echo pattern found, removing
wc1=`cat $file | wc -l`
sudo sed -i "/ $client_search_expr\t #on-dhcp-event /d" $file
wc2=`cat $file | wc -l`
if [ "$wc1" -eq "$wc2" ]; then
echo No change
fi
else
echo pattern NOT found
fi
# check if hostname already exists (e.g. a static host mapping)
# if so don't overwrite
grep -q " $client_search_expr " $file
if [ $? == 0 ]; then
echo host $client_fqdn_name already exists, exiting
exit 1
fi
line="$client_ip\t $client_fqdn_name\t #on-dhcp-event $client_mac"
sudo sh -c "echo -e '$line' >> $file"
((changes++))
echo Entry was added
;;
release) # delete mapping for released address
echo "- lease release event, deleting static mapping for host $client_fqdn_name"
wc1=`cat $file | wc -l`
sudo sed -i "/ $client_search_expr\t #on-dhcp-event /d" $file
wc2=`cat $file | wc -l`
if [ "$wc1" -eq "$wc2" ]; then
echo No change
else
echo Entry was removed
((changes++))
fi
;;
*)
logger -s -t on-dhcp-event "Invalid command \"$1\""
exit 1;
;;
esac
if [ $changes -gt 0 ]; then
echo Success
pid=`cat /var/run/dnsmasq/dnsmasq.pid`
if [ -n "$pid" ]; then
sudo kill -SIGHUP $pid
fi
else
echo No changes made
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment