public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
From: Brian Inglis <Brian.Inglis@SystematicSw.ab.ca>
To: cygwin@cygwin.com
Subject: Re: Automatically populate resolv.conf when new DNS is acquired
Date: Tue, 26 Jun 2018 19:23:00 -0000	[thread overview]
Message-ID: <c69b1d0a-74e9-bfe0-7c7e-500499844da5@SystematicSw.ab.ca> (raw)
In-Reply-To: <65242f5d-cc15-0024-47a8-0ce6be81aced@mail.com>

[-- Attachment #1: Type: text/plain, Size: 1372 bytes --]

On 2018-06-26 04:33, john doe wrote:
> In gnupg2 the use of dirmngr utility is required to interact with a keyserver.
> Dirmngr requires that '/etc/resolv.conf' be populated with my name servers.
> That means that everytime the dns changes (new network ...) I need to manually
> edit that file.
> How can I let Cygwin update that file whenever the DNS is changed?

Attached an awk script to generate resolv.conf from Windows ipconfig /all
output, run from .cygwin_profile (sourced under Cygwin from login .bash_profile)
using the stanza below: it only replaces an existing writable /etc/resolv.conf
when the content changes - touch, chown, chmod /etc/resolv.conf to enable.

# update /etc/resolv.conf if changed
c=/etc/resolv.conf
test -w $c					&& \
i=$(/usr/bin/which -- ipconfig)			&& \
r=$(/usr/bin/which -- resolv.awk)		&& \
t=$(/bin/mktemp -t -- resolv.conf.$$.XXXXXXXX)  && \
if $i /all | $r      >  $t; then
    /usr/bin/cmp -s -- $t $c	|| \
    /bin/cp -fv     -- $t $c
    /bin/rm -f	    -- $t
fi

unset c i r t

This could be used in a bash script run from a Windows scheduled task when a
relevant DHCP event occurs: you can find DHCP events by checking Windows Admin
Tools/Event Viewer/Window Logs/System/Filter Current Log/Event
Sources/Dhcp-Client,DHCPv6-Client, or a similar PowerShell script.

-- 
Take care. Thanks, Brian Inglis, Calgary, Alberta, Canada

[-- Attachment #2: resolv.awk --]
[-- Type: text/plain, Size: 766 bytes --]

#!/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 }
}


[-- Attachment #3: Type: text/plain, Size: 219 bytes --]


--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

  reply	other threads:[~2018-06-26 14:18 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-06-26 16:11 Autamatically " john doe
2018-06-26 19:23 ` Brian Inglis [this message]
2018-06-27 11:08   ` Automatically " john doe
2018-06-28  4:40     ` Brian Inglis
2018-06-27 11:01 ` Autamatically " Andrey Repin
2018-06-27 16:10   ` john doe

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=c69b1d0a-74e9-bfe0-7c7e-500499844da5@SystematicSw.ab.ca \
    --to=brian.inglis@systematicsw.ab.ca \
    --cc=cygwin@cygwin.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).