Skip to content

Instantly share code, notes, and snippets.

@for2ando
Created January 18, 2024 14:22
Show Gist options
  • Save for2ando/3cd96d6b6972bccad14632f464af854f to your computer and use it in GitHub Desktop.
Save for2ando/3cd96d6b6972bccad14632f464af854f to your computer and use it in GitHub Desktop.
Update /etc/resolv.conf from a ipconfig /all output, for Windows w/ Cygwin. Retreive from https://cygwin.com/pipermail/cygwin/2018-June/237655.html.
#!/usr/bin/awk -f
# resolv.awk - create Windows resolv.conf from ipconfig /all output
{ sub( /\r/, "", $NF) } # trim \r
# collect DNS domain suffixes
/D[Nn][Ss]\sSuffix[^:]*:\s\S/ { domain[$NF] = $NF }
# collect DNS search suffixes
/Search\sList[^:]*:\s\S/ { search[$NF] = $NF }
# collect DNS server IP addresses
/DNS\sServers[^:]*:\s\S/ { dns = 1 } # enable
dns && $NF ~ /^[0-9.]+$/ { nameserver[++ns] = $NF } # collect
dns && $NF !~ /^[0-9.]+$/ { dns = 0 } # disable
# output unique resolv.conf entries
END {
for (n = 1; n <= ns; ++n) { print "nameserver", nameserver[n] }
for (d in domain) { print "domain", domain[d] }
p = "search"
for (s in search) {
printf "%s %s", p, search[s]
p = ""
}
if (!p) { print p }
}
#!/bin/sh
# update /etc/resolv.conf if changed
cpbk=$(cmd /c chcp | awk '{sub(/\r/, "", $NF); print $NF}')
cmd /c chcp 437
resolvconf=/etc/resolv.conf
test -w $resolvconf && \
ipconfig=$(/usr/bin/which -- ipconfig) && \
resolvawk=$(/usr/bin/which -- resolv.awk) && \
tmpfile1=$(/bin/mktemp -t -- resolv.conf.$$.XXXXXXXX) && \
if $ipconfig /all | $resolvawk > $tmpfile1; then
/usr/bin/cmp -s -- $tmpfile1 $resolvconf || \
/bin/cp -fv -- $tmpfile1 $resolvconf
/bin/rm -f -- $tmpfile1
fi
echo "-$cpbk-"
cmd /c chcp $cpbk
unset cpbk resolvconf ipconfig resolvawk tmpfile1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment