-
-
Save adrianohirata/67821ecd907b9c5d2fdf80a8def0d903 to your computer and use it in GitHub Desktop.
Init.d script for keeping WSL resolv.conf in-sync with Windows
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /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 | |
# Description: Updated to keep codepage so it won't change the console font | |
# Original script by Matthias Brooks https://gist.github.com/matthiassb/9c8162d2564777a70e3ae3cbee7d2e95 | |
### END INIT INFO | |
PATH=/sbin:/bin:/mnt/c/WINDOWS/system32 | |
PS=/mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe | |
. /lib/init/vars.sh | |
. /lib/lsb/init-functions | |
do_start () { | |
while true | |
do | |
oemcp=$(reg.exe query "HKLM\\SYSTEM\\CurrentControlSet\\Control\\Nls\\CodePage" /v OEMCP | sed -n 3p | sed -e 's|\r||g' | grep -o '[[:digit:]]*') | |
chcp.com $oemcp > /dev/null | |
#Retrieve nameservers from via Powershell | |
TEMPFILE=$(mktemp) | |
$PS -Command "Get-DnsClientServerAddress -AddressFamily IPv4 | Select-Object -ExpandProperty ServerAddresses" > $TEMPFILE | |
/usr/bin/awk '!x[$0]++' $TEMPFILE > $TEMPFILE.2 | |
IFS=$'\r\n' GLOBIGNORE='*' command eval 'UNIQUE_NAMESERVERS=($(cat $TEMPFILE.2))' | |
rm -f $TEMPFILE $TEMPFILE.2 | |
#Retrive search domains via powershell | |
IFS=$'\r\n' GLOBIGNORE='*' command eval 'SEARCH_DOMAIN=($($PS -Command "Get-DnsClientGlobalSetting | Select-Object -ExpandProperty SuffixSearchList"))' | |
UNIQUE_SEARCH_DOMAIN=($(/usr/bin/tr ' ' '\n' <<< "${SEARCH_DOMAIN[@]}" | /usr/bin/sort -u | /usr/bin/tr '\n' ' ')) | |
chcp.com $oemcp > /dev/null | |
# 65001 | |
#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 | |
if [ ${#UNIQUE_SEARCH_DOMAIN[@]} -ne 0 ]; then | |
echo "search ${UNIQUE_SEARCH_DOMAIN[@]}" >> /etc/resolv.conf | |
fi | |
sleep 300 | |
done | |
} | |
do_status () { | |
PID=$(cat /var/run/dns-sync.pid 2>/dev/null) | |
if [ "$PID" == "" ]; then | |
echo "dns-sync is not running" | |
return | |
fi | |
if ps -p $PID > /dev/null | |
then | |
echo "dns-sync is running" | |
else | |
echo "dns-sync is not running" | |
fi | |
} | |
case "$1" in | |
start|"") | |
kill $(cat /var/run/dns-sync.pid >/dev/null 2>&1) > /dev/null 2>&1 || true | |
do_start & | |
DO_SYNC_PID=$! | |
echo "${DO_SYNC_PID}" > /var/run/dns-sync.pid | |
;; | |
restart|reload|force-reload) | |
echo "Error: argument '$1' not supported" >&2 | |
exit 3 | |
;; | |
stop) | |
PID=$(cat /var/run/dns-sync.pid) | |
if ps -p $PID > /dev/null | |
then | |
kill -9 $(cat /var/run/dns-sync.pid) | |
echo "dns-sync stopped" | |
else | |
echo "dns-sync is not running" | |
fi | |
;; | |
status) | |
do_status | |
exit $? | |
;; | |
*) | |
echo "Usage: dns-sync.sh [start|stop]" >&2 | |
exit 3 | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment