Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! /bin/bash
- ### BEGIN INIT INFO
- # Provides: dns-sync
- # Required-Start:
- # Required-Stop:
- # Default-Start: S
- # Default-Stop:
- # Short-Description: Synchronizes /etc/resolv.conf in WLS with Windows DNS
- ### END INIT INFO
- PATH=/sbin:/bin
- PS=/mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe
- . /lib/init/vars.sh
- . /lib/lsb/init-functions
- do_start () {
- #Retrieve nameservers from via Powershell
- IFS=$'\r\n' GLOBIGNORE='*' command eval 'NAMESERVERS=($($PS -Command "Get-DnsClientServerAddress -AddressFamily IPv4 | Select-Object -ExpandPropert ServerAddresses"))'
- #Retrive search domains via powershell
- IFS=$'\r\n' GLOBIGNORE='*' command eval 'SEARCH_DOMAIN=($($PS -Command "Get-DnsClientGlobalSetting | Select-Object -ExpandProperty SuffixSearchList"))'
- #Uniqify
- UNIQUE_NAMESERVERS=($(/usr/bin/tr ' ' '\n' <<< "${NAMESERVERS[@]}" | /usr/bin/sort -u | /usr/bin/tr '\n' ' '))
- UNIQUE_SEARCH_DOMAIN=($(/usr/bin/tr ' ' '\n' <<< "${SEARCH_DOMAIN[@]}" | /usr/bin/sort -u | /usr/bin/tr '\n' ' '))
- #Modify /etc/resolv.conf
- touch /etc/resolv.conf
- sed -i '/nameserver/d' /etc/resolv.conf > /dev/null 2>&1 || true
- sed -i '/search/d' /etc/resolv.conf > /dev/null 2>&1 || true
- for i in "${UNIQUE_NAMESERVERS[@]}"
- do
- echo "nameserver ${i}" >> /etc/resolv.conf
- done
- echo "search ${UNIQUE_SEARCH_DOMAIN[@]}" >> /etc/resolv.conf
- }
- do_status () {
- HOSTNAME=$(hostname)
- if [ "$HOSTNAME" ] ; then
- return 0
- else
- return 4
- fi
- }
- case "$1" in
- start|"")
- do_start
- ;;
- restart|reload|force-reload)
- echo "Error: argument '$1' not supported" >&2
- exit 3
- ;;
- stop)
- # No-op
- ;;
- status)
- do_status
- exit $?
- ;;
- *)
- echo "Usage: dns-sync.sh [start|stop]" >&2
- exit 3
- ;;
- esac
Add Comment
Please, Sign In to add comment