Default DNS

Peter Hansen peter at engcorp.com
Tue Nov 20 23:46:01 EST 2001


A wrote:
> 
> Is there any possibility in Python to find out my primary or
> secondary DNS when using Dial Up connection from Windows?

Something like the following might be the quickest approach:

>>> import os,re
>>> text = os.popen('ipconfig /all').read()
>>> dns = re.search(r'DNS Servers[ .]*:\s+(\S+)\s*(\S+)?', text).groups()
>>> dns
('206.221.248.4', '206.221.248.5')

I tried using a pattern like this at first, but it only returned a
result of (' :',) and I'm not sure why:

>>> dns = re.search(r'DNS Servers[ .]*:((?:\s+)\S+)+', text).groups()
>>> dns
(' :',)

I suppose the non-grouping group (?:...) cannot be nested inside
another group?

-- 
----------------------
Peter Hansen, P.Eng.
peter at engcorp.com



More information about the Python-list mailing list