1
0
mirror of https://github.com/gryf/wicd.git synced 2026-03-14 05:35:49 +01:00

Add support for using resolvconf instead of directly editing resolv.conf if possible.

This commit is contained in:
Dan O'Reilly
2009-03-09 00:07:39 -04:00
parent 842d281b28
commit b66bd696af
3 changed files with 67 additions and 44 deletions

View File

@@ -74,7 +74,7 @@ blacklist_strict = '!"#$%&\'()*+,./:;<=>?@[\\]^`{|}~ '
blacklist_norm = ";`$!*|><&\\"
blank_trans = maketrans("", "")
__all__ = ["SetDNS", "GetDefaultGateway", "GetWiredInterfaces",
__all__ = ["GetDefaultGateway", "GetWiredInterfaces",
"GetWirelessInterfaces", "IsValidWpaSuppDriver"]
def _sanitize_string(string):
@@ -89,33 +89,6 @@ def _sanitize_string_strict(string):
else:
return string
def SetDNS(dns1=None, dns2=None, dns3=None, dns_dom=None, search_dom=None):
""" Set the DNS of the system to the specified DNS servers.
Opens up resolv.conf and writes in the nameservers.
Keyword arguments:
dns1 -- IP address of DNS server 1
dns2 -- IP address of DNS server 2
dns3 -- IP address of DNS server 3
dns_dom -- DNS domain
search_dom -- DNS search domain
"""
resolv = open("/etc/resolv.conf", "w")
if dns_dom:
resolv.write("domain %s\n" % dns_dom)
if search_dom:
resolv.write('search %s\n' % search_dom)
for dns in [dns1, dns2, dns3]:
if dns:
if misc.IsValidIP(dns):
print 'Setting DNS : ' + dns
resolv.write('nameserver ' + dns + '\n')
else:
print 'DNS IP is not a valid IP address, not writing to resolv.conf'
resolv.close()
def GetDefaultGateway():
""" Attempts to determine the default gateway by parsing route -n. """
route_info = misc.Run("route -n")
@@ -291,6 +264,11 @@ class BaseInterface(object):
self.CheckWirelessTools()
self.CheckSudoApplications()
self.CheckRouteFlushTool()
self.CheckResolvConf()
def CheckResolvConf(self):
""" Checks for the existence of resolvconf."""
self.resolvconf_cmd = self._find_program_path("resolvconf")
def CheckDHCP(self):
""" Check for the existence of valid DHCP clients.
@@ -518,7 +496,48 @@ class BaseInterface(object):
return
if self.verbose: print cmd
misc.Run(cmd)
def SetDNS(self, dns1=None, dns2=None, dns3=None,
dns_dom=None, search_dom=None):
""" Set the DNS of the system to the specified DNS servers.
Opens up resolv.conf and writes in the nameservers.
Keyword arguments:
dns1 -- IP address of DNS server 1
dns2 -- IP address of DNS server 2
dns3 -- IP address of DNS server 3
dns_dom -- DNS domain
search_dom -- DNS search domain
"""
resolv_params = ""
if dns_dom:
resolv_params = ''.join([resolv_params, 'domain ', dns_dom, '\n'])
if search_dom:
resolv_params = ''.join([resolv_params, 'search ', search_dom,
'\n'])
valid_dns_list = ['nameserver']
for dns in [dns1, dns2, dns3]:
if dns:
if misc.IsValidIP(dns):
print 'Setting DNS : ' + dns
valid_dns_list.append(dns)
else:
print 'DNS IP is not a valid IP address, skipping'
# Make sure we have more than just 'nameserver' in the list.
if len(valid_dns_list) > 1:
resolv_params += ' '.join(valid_dns_list) + '\n'
if self.resolvconf_cmd:
print "running resolvconf"
p = misc.Run(' '.join([self.resolvconf_cmd, '-a', self.iface]),
include_stderr=True, return_obj=True)
p.communicate(input=resolv_params)[0]
else:
resolv = open("/etc/resolv.conf", "w")
resolv.write(resolv_params + "\n")
resolv.close()
def FlushRoutes(self):
""" Flush network routes for this device. """