SHARE
TWEET

Untitled

a guest Jul 13th, 2016 93 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. cat /opt/vyatta/sbin/on-dhcp-event.sh
  2. #!/bin/bash
  3.  
  4. # This script came from ubnt.com forum user "bradd" in the following post
  5. # http://community.ubnt.com/t5/EdgeMAX/Automatic-DNS-resolution-of-DHCP-client-names/td-p/651311
  6. # It has been modified by Ubiquiti to update the /etc/host file
  7. # instead of adding to the CLI.
  8. # Thanks to forum user "itsmarcos" for bug fix & improvements
  9. # Thanks to forum user "ruudboon" for multiple domain fix
  10. # Thanks to forum user "chibby85" for expire patch and static-mapping
  11.  
  12. if [ $# -lt 5 ]; then
  13.   echo Invalid args
  14.   logger -s -t on-dhcp-event "Invalid args \"$@\""
  15.   exit 1
  16. fi
  17.  
  18. action=$1
  19. client_name=$2
  20. client_ip=$3
  21. client_mac=$4
  22. domain=$5
  23. file=/etc/hosts
  24. changes=0
  25.  
  26. if [ "$domain" == "..YYZ!" ]; then
  27.     client_fqdn_name=$client_name
  28.     client_search_expr=$client_name
  29. else
  30.     client_fqdn_name=$client_name.$domain
  31.     client_search_expr="$client_name\\.$domain"
  32. fi
  33.  
  34. case "$action" in
  35.   commit) # add mapping for new lease
  36.     echo "- new lease event, setting static mapping for host "\
  37.          "$client_fqdn_name (MAC=$client_mac, IP=$client_ip)"
  38.     #
  39.     # grep fails miserably with \t in the search expression.
  40.     # In the following line one <Ctrl-V> <TAB> is used after $client_search_expr
  41.     # followed by a single space
  42.     grep -q " $client_search_expr    #on-dhcp-event " $file
  43.     if [ $? == 0 ]; then
  44.        echo pattern found, removing
  45.        wc1=`cat $file | wc -l`
  46.        sudo sed -i "/ $client_search_expr\t #on-dhcp-event /d" $file
  47.        wc2=`cat $file | wc -l`
  48.        if [ "$wc1" -eq "$wc2" ]; then
  49.          echo No change
  50.        fi
  51.     else
  52.        echo pattern NOT found
  53.     fi
  54.    
  55.     # check if hostname already exists (e.g. a static host mapping)
  56.     # if so don't overwrite
  57.     grep -q " $client_search_expr    " $file
  58.     if [ $? == 0 ]; then
  59.        echo host $client_fqdn_name already exists, exiting
  60.        exit 1
  61.     fi
  62.  
  63.     line="$client_ip\t $client_fqdn_name\t #on-dhcp-event $client_mac"
  64.     sudo sh -c "echo -e '$line' >> $file"
  65.     ((changes++))
  66.     echo Entry was added
  67.     ;;
  68.  
  69.   release) # delete mapping for released address
  70.     echo "- lease release event, deleting static mapping for host $client_fqdn_name"
  71.     wc1=`cat $file | wc -l`
  72.     sudo sed -i "/ $client_search_expr\t #on-dhcp-event /d" $file  
  73.     wc2=`cat $file | wc -l`
  74.     if [ "$wc1" -eq "$wc2" ]; then
  75.       echo No change
  76.     else
  77.       echo Entry was removed
  78.       ((changes++))      
  79.     fi
  80.     ;;
  81.  
  82.   *)
  83.     logger -s -t on-dhcp-event "Invalid command \"$1\""
  84.     exit 1;
  85.     ;;
  86. esac
  87.  
  88. if [ $changes -gt 0 ]; then
  89.   echo Success
  90.   pid=`cat /var/run/dnsmasq/dnsmasq.pid`
  91.   if [ -n "$pid" ]; then
  92.      sudo kill -SIGHUP $pid
  93.   fi
  94. else
  95.   echo No changes made
  96. fi
  97. exit 0
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top