Guest User

Untitled

a guest
Jul 23rd, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | None | 0 0
  1. #! /bin/bash
  2. ### BEGIN INIT INFO
  3. # Provides: dns-sync
  4. # Required-Start:
  5. # Required-Stop:
  6. # Default-Start: S
  7. # Default-Stop:
  8. # Short-Description: Synchronizes /etc/resolv.conf in WLS with Windows DNS
  9. ### END INIT INFO
  10.  
  11. PATH=/sbin:/bin
  12. PS=/mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe
  13.  
  14. . /lib/init/vars.sh
  15. . /lib/lsb/init-functions
  16.  
  17. do_start () {
  18. #Retrieve nameservers from via Powershell
  19. IFS=$'\r\n' GLOBIGNORE='*' command eval 'NAMESERVERS=($($PS -Command "Get-DnsClientServerAddress -AddressFamily IPv4 | Select-Object -ExpandPropert ServerAddresses"))'
  20.  
  21. #Retrive search domains via powershell
  22. IFS=$'\r\n' GLOBIGNORE='*' command eval 'SEARCH_DOMAIN=($($PS -Command "Get-DnsClientGlobalSetting | Select-Object -ExpandProperty SuffixSearchList"))'
  23.  
  24. #Uniqify
  25. UNIQUE_NAMESERVERS=($(/usr/bin/tr ' ' '\n' <<< "${NAMESERVERS[@]}" | /usr/bin/sort -u | /usr/bin/tr '\n' ' '))
  26. UNIQUE_SEARCH_DOMAIN=($(/usr/bin/tr ' ' '\n' <<< "${SEARCH_DOMAIN[@]}" | /usr/bin/sort -u | /usr/bin/tr '\n' ' '))
  27.  
  28. #Modify /etc/resolv.conf
  29. touch /etc/resolv.conf
  30. sed -i '/nameserver/d' /etc/resolv.conf > /dev/null 2>&1 || true
  31. sed -i '/search/d' /etc/resolv.conf > /dev/null 2>&1 || true
  32.  
  33. for i in "${UNIQUE_NAMESERVERS[@]}"
  34. do
  35. echo "nameserver ${i}" >> /etc/resolv.conf
  36. done
  37.  
  38. echo "search ${UNIQUE_SEARCH_DOMAIN[@]}" >> /etc/resolv.conf
  39. }
  40.  
  41. do_status () {
  42. HOSTNAME=$(hostname)
  43. if [ "$HOSTNAME" ] ; then
  44. return 0
  45. else
  46. return 4
  47. fi
  48. }
  49.  
  50. case "$1" in
  51. start|"")
  52. do_start
  53. ;;
  54. restart|reload|force-reload)
  55. echo "Error: argument '$1' not supported" >&2
  56. exit 3
  57. ;;
  58. stop)
  59. # No-op
  60. ;;
  61. status)
  62. do_status
  63. exit $?
  64. ;;
  65. *)
  66. echo "Usage: dns-sync.sh [start|stop]" >&2
  67. exit 3
  68. ;;
  69. esac
Add Comment
Please, Sign In to add comment