+ return self._check_output('netsh', 'interface', 'ip', 'show', 'dns')
+
+ def _netsh_set_dns(self, iface_name, addr):
+ """Modify DNS information on the primary interface."""
+ output = self._check_output('netsh', 'interface', 'ip', 'set', 'dns',
+ iface_name, 'static', addr)
+
+ def _netsh_set_dns_dhcp(self, iface_name):
+ """Modify DNS information on the primary interface."""
+ output = self._check_output('netsh', 'interface', 'ip', 'set', 'dns',
+ iface_name, 'dhcp')
+
+ def _get_interfaces_with_dns(self):
+ output = self._netsh_show_dns()
+ lines = output.split('\n')
+ iface_re = re.compile(r'^Configuration for interface \"(?P<name>.*)\"')
+ dns_re = re.compile(r'(?P<kind>.*):\s+(?P<dns>\d+\.\d+\.\d+\.\d+)')
+ iface_name = None
+ iface_dns = None
+ iface_kind = None
+ ifaces = []
+ for line in lines:
+ iface_match = iface_re.match(line)
+ if iface_match:
+ iface_name = iface_match.group('name')
+ dns_match = dns_re.match(line)
+ if dns_match:
+ iface_dns = dns_match.group('dns')
+ iface_dns_config = dns_match.group('kind').strip()
+ if iface_dns_config == "Statically Configured DNS Servers":
+ iface_kind = "static"
+ elif iface_dns_config == "DNS servers configured through DHCP":
+ iface_kind = "dhcp"
+ if iface_name and iface_dns and iface_kind:
+ ifaces.append( (iface_dns, iface_name, iface_kind) )
+ iface_name = None
+ iface_dns = None
+ return ifaces
+
+ def _save_primary_interface_properties(self):
+ # TODO(etienneb): On windows, an interface can have multiple DNS server
+ # configured. We should save/restore all of them.
+ ifaces = self._get_interfaces_with_dns()
+ self._primary_interfaces = ifaces
+
+ def _restore_primary_interface_properties(self):
+ for iface in self._primary_interfaces:
+ (iface_dns, iface_name, iface_kind) = iface
+ self._netsh_set_dns(iface_name, iface_dns)
+ if iface_kind == "dhcp":
+ self._netsh_set_dns_dhcp(iface_name)
+