Advertisement
Guest User

update-resolv-conf

a guest
May 30th, 2021
495
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. #!/usr/bin/env bash
  2. #
  3. # Parses DHCP options from openvpn to update resolv.conf
  4. # To use set as 'up' and 'down' script in your openvpn *.conf:
  5. # up /etc/openvpn/update-resolv-conf
  6. # down /etc/openvpn/update-resolv-conf
  7. #
  8. # Used snippets of resolvconf script by Thomas Hood <[email protected]>
  9. # and Chris Hanson
  10. # Licensed under the GNU GPL. See /usr/share/common-licenses/GPL.
  11. # 07/2013 [email protected] Fixed intet name
  12. #
  13. # Example envs set from openvpn:
  14. # foreign_option_1='dhcp-option DNS 193.43.27.132'
  15. # foreign_option_2='dhcp-option DNS 193.43.27.133'
  16. # foreign_option_3='dhcp-option DOMAIN be.bnc.ch'
  17. # foreign_option_4='dhcp-option DOMAIN-SEARCH bnc.local'
  18.  
  19. ## The 'type' builtins will look for file in $PATH variable, so we set the
  20. ## PATH below. You might need to directly set the path to 'resolvconf'
  21. ## manually if it still doesn't work, i.e.
  22. ## RESOLVCONF=/usr/sbin/resolvconf
  23. export PATH=$PATH:/sbin:/usr/sbin:/bin:/usr/bin
  24. RESOLVCONF=$(type -p resolvconf)
  25.  
  26. case $script_type in
  27.  
  28. up)
  29. for optionname in ${!foreign_option_*} ; do
  30. option="${!optionname}"
  31. echo $option
  32. part1=$(echo "$option" | cut -d " " -f 1)
  33. if [ "$part1" == "dhcp-option" ] ; then
  34. part2=$(echo "$option" | cut -d " " -f 2)
  35. part3=$(echo "$option" | cut -d " " -f 3)
  36. if [ "$part2" == "DNS" ] ; then
  37. IF_DNS_NAMESERVERS="$IF_DNS_NAMESERVERS $part3"
  38. fi
  39. if [[ "$part2" == "DOMAIN" || "$part2" == "DOMAIN-SEARCH" ]] ; then
  40. IF_DNS_SEARCH="$IF_DNS_SEARCH $part3"
  41. fi
  42. fi
  43. done
  44. R=""
  45. if [ "$IF_DNS_SEARCH" ]; then
  46. R="search "
  47. for DS in $IF_DNS_SEARCH ; do
  48. R="${R} $DS"
  49. done
  50. R="${R}
  51. "
  52. fi
  53.  
  54. for NS in $IF_DNS_NAMESERVERS ; do
  55. R="${R}nameserver $NS
  56. "
  57. done
  58. #echo -n "$R" | $RESOLVCONF -x -p -a "${dev}"
  59. echo -n "$R" | $RESOLVCONF -x -a "${dev}.inet"
  60. ;;
  61. down)
  62. $RESOLVCONF -d "${dev}.inet"
  63. ;;
  64. esac
  65.  
  66. # Workaround / [email protected]
  67. # force exit with no errors. Due to an apparent conflict with the Network Manager
  68. # $RESOLVCONF sometimes exits with error code 6 even though it has performed the
  69. # action correctly and OpenVPN shuts down.
  70. exit 0
  71.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement