#!/usr/bin/awk -f # cyg-resolv.awk - create Cygwin resolv.conf from Windows ipconfig /all output function nextaddr( addr, name, ns, nameserveraddr, nameserverhostname ,private,n) { if (DEBUG) print addr, name, ns > DEBUG # private subnets private = (addr ~ p10 || addr ~ p172 || addr ~ p192) # default name if (private && !name) { name = "private" } # private subnets come first if unoccupied if (private && !(0 in nameserveraddr)) { n = 0 } else { n = ++ns } if (addr) { nameserveraddr[n] = addr } if (name) { nameserverhostname[n] = name } if (DEBUG) print addr, name, ns > DEBUG return ns } function addrs( NS, nameserveraddr, ns, c, l) { # $ getent ahostsv4 $NS # 1.0.0.1 STREAM one.one.one.one # 1.0.0.1 DGRAM one.one.one.one # 1.1.1.1 STREAM # 1.1.1.1 DGRAM # 149.112.112.112 0 dns.quad9.net # 9.9.9.9 0 # 208.67.220.220 0 resolver2.opendns.com # 208.67.222.222 0 resolver1.opendns.com # 8.8.4.4 0 google-public-dns-b.google.com # 8.8.8.8 0 google-public-dns-a.google.com # 149.112.122.20 0 CAshieldProtected # 149.112.121.20 0 l = 0 c = "/usr/bin/getent ahostsv4 " NS while ((c | getline) > 0) { if ($1 != l) { ns = nextaddr( $1, $3, ns, nameserveraddr, nameserverhostname) } l = $1 } return ns } BEGIN { # private subnets # digit patterns: .[0[0]]0-255, .[0]16-31 d0_255 = "(.(0{0,2}[0-9]|0{0,1}[1-9][0-9]|1[0-9][0-9]|2([0-4][0-9]|5[0-5])))" d16_31 = ".0?(1[6-9]|2[0-9]|3[01])" # 10.0.0.0-.255.255.255 p10 = "^10" d0_255 "{3}$" # 172.16.0.0-.31.255.255 p172 = "^172" d16_31 d0_255 "{2}$" # 192.168.0.0-.255.255 p192 = "^192.168" d0_255 "{2}$" # public name servers # CIRA Canadian Shield Protected+malware+phishing NS = "dns.quad9.net. one.one.one.one. " \ "resolver2.opendns.com. resolver1.opendns.com. " \ "google-public-dns-b.google.com. google-public-dns-a.google.com. " \ "CAshieldProtected" SUF = "ca. org. com. net." CFHD = "# /etc/resolv.conf" CFHD = CFHD " - Internet Domain Name System resolver configuration file" CDS = "# domain suffix" CSSL = "# suffix search list" } /\r/ { sub( /\r/, "", $NF) } # trim \r # collect DNS domain suffixes /D[Nn][Ss]\sSuffix[^:]*:\s\S/ { last = $NF if (last ~ /\./ && last !~ /\.$/) last = last "."; # add root dot if (!(last in domain)) { domain[last] = last domains = domains " " last } while (last ~ /\..+\..+/) { # strip labels if more than two for domain sub(/^[^.]+./, "", last) if (last ~ /\./ && last !~ /\.$/) last = last "."; # add root dot if (!(last in domain)) { domain[last] = last domains = domains " " last } } } # collect DNS search suffixes /Search\sList[^:]*:\s\S/ { for (d in domain) { if (!(d in search)) search[d] = d if (d ~ /shaw[^.]+./) { last = "shaw.ca." if (!(last in search)) { search[last] = last domains = domains " " last } } } last = $NF if (last ~ /\./ && last !~ /\.$/) last = last "."; # add root dot if (!(last in search)) { search[last] = last domains = domains " " last } while (last ~ /\..+\..+/) { # strip labels if more than two for domain sub(/^[^.]+./, "", last) if (last ~ /\./ && last !~ /\.$/) last = last "."; # add root dot if (!(last in search)) { search[last] = last domains = domains " " last } } ns = split( SUF, sa) for (s = 1; s <= ns; ++s) { last = sa[s] if (last ~ /\./ && last !~ /\.$/) last = last "."; # add root dot if (!(last in search)) { search[last] = last domains = domains " " last } } } # collect DNS server IP V4 addresses /DNS\sServers[^:]*:\s\S/ { dns = 1 } # start - enable dns && $NF ~ /^([0-9A-Fa-f]{0,4}:){1,7}[0-9A-Fa-f]{0,4}$/ { next } # skip IP V6 dns && $NF ~ /^([0-9]{1,3}\.){3}[0-9]{1,3}$/ { # collect IP V4 ns = nextaddr( $NF, last, ns, nameserveraddr, nameserverhostname) last = "" } dns && $NF !~ /^([0-9A-Fa-f]{0,4}:){1,7}[0-9A-Fa-f]{0,4}$/ && \ $NF !~ /^([0-9]{1,3}\.){3}[0-9]{1,3}$/ { dns = 0 } # non-IP disable # output unique resolv.conf entries END { print CFHD ns = addrs( NS, nameserveraddr, ns) for (n = 0; n <= ns; ++n) { if (n in nameserverhostname && nameserverhostname[n]) { print "#", nameserverhostname[n] } if (n in nameserveraddr && nameserveraddr[n]) { print "nameserver", nameserveraddr[n] } } print CDS for (d in domain) { print "domain " d } print CSSL if (domains) print "search" domains }