From d44d03fce4c82757d494478e6a35f3da5eb2002e Mon Sep 17 00:00:00 2001 From: Adam Blackburn Date: Mon, 23 Nov 2009 21:51:07 -0600 Subject: [PATCH 1/2] add support for sending the hostname using dhclient --- wicd/wnettools.py | 46 +++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 41 insertions(+), 5 deletions(-) diff --git a/wicd/wnettools.py b/wicd/wnettools.py index caa0764..43eb341 100644 --- a/wicd/wnettools.py +++ b/wicd/wnettools.py @@ -270,10 +270,16 @@ class BaseInterface(object): client = None cmd = "" return (client, cmd) + + # probably /var/lib/wicd/dhclient.conf with defaults + dhclient_conf_path = os.path.join( + wpath.varlib, + 'dhclient.conf' + ) client_dict = { "dhclient" : - {'connect' : r"%(cmd)s %(iface)s", + {'connect' : r"%(cmd)s -cf %(dhclientconf)s %(iface)s", 'release' : r"%(cmd)s -r %(iface)s", 'id' : misc.DHCLIENT, }, @@ -294,12 +300,39 @@ class BaseInterface(object): }, } (client_name, cmd) = get_client_name(self.DHCP_CLIENT) + + # cause dhclient doesn't have a handy dandy argument + # for specifing the hostname to be sent + if client_name == "dhclient" and flavor: + if hostname == None: + # will use the system hostname + # we'll use that if there is hostname passed + # that shouldn't happen, though + hostname = '' + print 'attempting to set hostname with dhclient' + print 'using dhcpcd or another supported client may work better' + dhclient_template = \ + open(os.path.join(wpath.etc, 'dhclient.conf.template'), 'r') + + output_conf = open(dhclient_conf_path, 'w') + + for line in dhclient_template.readlines(): + line = line.replace('$_HOSTNAME', hostname) + output_conf.write(line) + + output_conf.close() + dhclient_template.close() + if not client_name or not cmd: print "WARNING: Failed to find a valid dhcp client!" return "" if flavor == "connect": - return client_dict[client_name]['connect'] % {"cmd":cmd, "iface":self.iface, "hostname":hostname} + return client_dict[client_name]['connect'] % \ + { "cmd" : cmd, + "iface" : self.iface, + "hostname" : hostname, + 'dhclientconf' : dhclient_conf_path } elif flavor == "release": return client_dict[client_name]['release'] % {"cmd":cmd, "iface":self.iface} else: @@ -535,7 +568,7 @@ class BaseInterface(object): def _check_dhcp_result(self, success): """ Print and return the correct DHCP connection result. - Keyword Arguents: + Keyword Arguments: success -- boolean specifying if DHCP was succesful. Returns: @@ -550,15 +583,18 @@ class BaseInterface(object): return 'dhcp_failed' @neediface(False) - def StartDHCP(self,hostname): + def StartDHCP(self, hostname): """ Start the DHCP client to obtain an IP address. + + Keyword Arguments: + hostname -- the hostname to send to the DHCP server Returns: A string representing the result of the DHCP command. See _check_dhcp_result for the possible values. """ - cmd = self._get_dhcp_command('connect',hostname) + cmd = self._get_dhcp_command('connect', hostname) if self.verbose: print cmd self.dhcp_object = misc.Run(cmd, include_stderr=True, return_obj=True) pipe = self.dhcp_object.stdout From f7d39b249d28218dfb4c4844d0555db6569b550f Mon Sep 17 00:00:00 2001 From: Andrew Psaltis Date: Thu, 26 Nov 2009 00:01:07 -0500 Subject: [PATCH 2/2] Fix annoying KeyError bug if no prior dhcp hostname has been set. --- wicd/networking.py | 2 ++ wicd/wnettools.py | 2 ++ 2 files changed, 4 insertions(+) diff --git a/wicd/networking.py b/wicd/networking.py index aad0633..0ea258e 100644 --- a/wicd/networking.py +++ b/wicd/networking.py @@ -448,6 +448,8 @@ class ConnectThread(threading.Thread): else: # Run dhcp... self.SetStatus('running_dhcp') + if self.network.get('dhcphostname') == None: + self.network['dhcphostname'] = os.uname()[1] print "Running DHCP with hostname",self.network["dhcphostname"] dhcp_status = iface.StartDHCP(self.network["dhcphostname"]) if dhcp_status in ['no_dhcp_offers', 'dhcp_failed']: diff --git a/wicd/wnettools.py b/wicd/wnettools.py index 43eb341..1cf8568 100644 --- a/wicd/wnettools.py +++ b/wicd/wnettools.py @@ -328,6 +328,8 @@ class BaseInterface(object): return "" if flavor == "connect": + if not hostname: + hostname = os.uname()[1] return client_dict[client_name]['connect'] % \ { "cmd" : cmd, "iface" : self.iface,