1
0
mirror of https://github.com/gryf/wicd.git synced 2025-12-21 13:28:08 +01:00

Fixed crash bug in script configuration dialog when a network doesn't have script options written in the config file yet.

Refactored networking.py to not have to create a new wnettools interface every time a method gets called.  Now it reuses the same one and makes changes to the iface name/driver as needed.
Refactored a few methods in wnettools.py to be organized more logically and reduce external program calls.
In experimental branch, added a few methods to networking/wnettools that can be used for enabling/disabling interfaces, as well as unloading/loading the driver associated with an interface.
Added a check for mii-tool/ethtool that gets run when wicd starts, so it can decide which to use to check for a wired connection.
Added a check for ip, to decide how to flush the routing tables.
Rewrote some of the DHCP client checking code.
Added a method (that's currently unused) to release a dhcp lease for each of the supported clients.
This commit is contained in:
imdano
2008-02-29 14:16:21 +00:00
parent 1b6108ce03
commit 3986ddd4fb
7 changed files with 257 additions and 80 deletions

View File

@@ -82,6 +82,12 @@ def blank_to_none(text):
return "None" return "None"
else: else:
return str(text) return str(text)
def get_val(con, network, val, default="None"):
if not con.has_option(network, val):
con.set(network, val, default)
return con.get(network, val)
def get_script_info(network, network_type): def get_script_info(network, network_type):
""" Read script info from disk and load it into the configuration dialog """ """ Read script info from disk and load it into the configuration dialog """
@@ -90,17 +96,16 @@ def get_script_info(network, network_type):
if network_type == "wired": if network_type == "wired":
con.read(wired_conf) con.read(wired_conf)
if con.has_section(network): if con.has_section(network):
info["pre_entry"] = con.get(network, "beforescript") info["pre_entry"] = get_val(con, network, "beforescript")
info["post_entry"] = con.get(network, "afterscript") info["post_entry"] = get_val(con, network, "afterscript")
info["disconnect_entry"] = con.get(network, "disconnectscript") info["disconnect_entry"] = get_val(con, network, "disconnectscript")
else: else:
bssid = wireless.GetWirelessProperty(int(network), "bssid") bssid = wireless.GetWirelessProperty(int(network), "bssid")
con.read(wireless_conf) con.read(wireless_conf)
if con.has_section(bssid): if con.has_section(bssid):
info["pre_entry"] = con.get(bssid, "beforescript") info["pre_entry"] = get_val(con, bssid, "beforescript")
info["post_entry"] = con.get(bssid, "afterscript") info["post_entry"] = get_val(con, bssid, "afterscript")
info["disconnect_entry"] = con.get(bssid, "disconnectscript") info["disconnect_entry"] = get_val(con, bssid, "disconnectscript")
return info return info
def write_scripts(network, network_type, script_info): def write_scripts(network, network_type, script_info):

View File

@@ -45,6 +45,7 @@ import dbus
import dbus.service import dbus.service
if getattr(dbus, 'version', (0, 0, 0)) >= (0, 41, 0): if getattr(dbus, 'version', (0, 0, 0)) >= (0, 41, 0):
import dbus.glib import dbus.glib
# wicd specific libraries # wicd specific libraries
import wpath import wpath
import networking import networking
@@ -176,7 +177,7 @@ class ConnectionWizard(dbus.service.Object):
This number is major-minor-micro. Major is only incremented if minor This number is major-minor-micro. Major is only incremented if minor
reaches > 9. Minor is incremented if changes that break core stucture reaches > 9. Minor is incremented if changes that break core stucture
are implemented. Micro is for everything else, and micro may be are implemented. Micro is for everything else, and micro may be
anything >= 0. This number is effective starting wicd v1.2.0 anything >= 0. This number is effective starting wicd v1.2.0.
""" """
version = '1.5.0' version = '1.5.0'

15
misc.py
View File

@@ -167,8 +167,9 @@ def ParseEncryption(network):
x = x.replace("$_" + str(t).upper(), str(network[t])) x = x.replace("$_" + str(t).upper(), str(network[t]))
z = z + "\n" + x z = z + "\n" + x
y += 1 y += 1
# Write the data to the files
# then chmod them so they can't be read by normal users # Write the data to the files then chmod them so they can't be read
# by normal users.
file = open(wpath.networks + network["bssid"].replace(":", "").lower(), file = open(wpath.networks + network["bssid"].replace(":", "").lower(),
"w") "w")
os.chmod(wpath.networks + network["bssid"].replace(":", "").lower(), 0600) os.chmod(wpath.networks + network["bssid"].replace(":", "").lower(), 0600)
@@ -207,14 +208,14 @@ def LoadEncryptionMethods():
index = -1 index = -1
for current in requiredFields: for current in requiredFields:
# The pretty names will start with an * so we can # The pretty names will start with an * so we can
# seperate them with that # separate them with that.
if current[0] == "*": if current[0] == "*":
# Make underscores spaces # Make underscores spaces
# and remove the * # and remove the *
encryptionTypes[typeID][2][index][0] = current.replace("_", encryptionTypes[typeID][2][index][0] = current.replace("_",
" ").lstrip("*") " ").lstrip("*")
else: else:
# Add to the list of things that are required # Add to the list of things that are required.
index = len(encryptionTypes[typeID][2]) index = len(encryptionTypes[typeID][2])
encryptionTypes[typeID][2][index] = {} encryptionTypes[typeID][2][index] = {}
encryptionTypes[typeID][2][index][1] = current encryptionTypes[typeID][2][index][1] = current
@@ -252,7 +253,6 @@ def get_gettext():
_ = lang.gettext _ = lang.gettext
return _ return _
def to_unicode(x): def to_unicode(x):
""" Attempts to convert a string to unicode """ """ Attempts to convert a string to unicode """
try: # This may never fail, but let's be safe try: # This may never fail, but let's be safe
@@ -270,12 +270,13 @@ def to_unicode(x):
def error(parent, message): def error(parent, message):
""" Shows an error dialog """ """ Shows an error dialog """
dialog = gtk.MessageDialog(parent, gtk.DIALOG_MODAL, gtk.MESSAGE_ERROR, dialog = gtk.MessageDialog(parent, gtk.DIALOG_MODAL, gtk.MESSAGE_ERROR,
gtk.BUTTONS_OK) gtk.BUTTONS_OK)
dialog.set_markup(message) dialog.set_markup(message)
dialog.run() dialog.run()
dialog.destroy() dialog.destroy()
class LogWriter():
class LogWriter:
""" A class to provide timestamped logging. """ """ A class to provide timestamped logging. """
def __init__(self): def __init__(self):
self.file = open(wpath.log + 'wicd.log','a') self.file = open(wpath.log + 'wicd.log','a')

View File

@@ -36,9 +36,9 @@ class WiredConnectThread() -- Connection thread for wired
# #
# #
# Much thanks to wieman01 for help and support with various types of encyption # Much thanks to wieman01 for help and support with various types of encyption.
# Also thanks to foxy123, yopnono, and the many others who reported bugs helped # Also thanks to foxy123, yopnono, and the many others who reported bugs helped
# and helped keep this project moving # and helped keep this project moving.
# #
import re import re
@@ -63,12 +63,33 @@ class Controller(object):
after_script = None after_script = None
disconnect_script = None disconnect_script = None
driver = None driver = None
wiface = None
liface = None
def __init__(self): def __init__(self):
""" Initialise the class. """ """ Initialise the class. """
self.global_dns_1 = None self.global_dns_1 = None
self.global_dns_2 = None self.global_dns_2 = None
self.global_dns_3 = None self.global_dns_3 = None
def SetWiface(self, iface):
self.wiface.SetInterface(iface)
def SetLiface(self, iface):
self.liface.SetInterface(iface)
def __setattr__(self, attr, value):
if attr == 'wireless_interface':
object.__setattr__(self, attr, value)
if self.wiface:
self.SetWiface(value)
print 'hmm', self.wireless_interface
elif attr == 'wired_interface':
object.__setattr__(self, attr, value)
if self.liface:
self.SetLiface(value)
else:
object.__setattr__(self, attr, value)
class ConnectThread(threading.Thread): class ConnectThread(threading.Thread):
@@ -168,6 +189,21 @@ class Wireless(Controller):
""" Initialise the class. """ """ Initialise the class. """
Controller.__init__(self) Controller.__init__(self)
self.wpa_driver = None self.wpa_driver = None
self.wiface = wnettools.WirelessInterface(self.wireless_interface,
self.wpa_driver)
def __setattr__(self, attr, value):
if attr == 'wpa_driver':
self.__dict__[attr] = value
if self.wiface:
self.SetWPADriver(value)
else:
object.__setattr__(self, attr, value)
def LoadInterfaces(self):
""" Load the wnettools controls for the wired/wireless interfaces. """
self.wiface = wnettools.WirelessInterface(self.wireless_interface,
self.wpa_driver)
def Scan(self, essid=None): def Scan(self, essid=None):
""" Scan for available wireless networks. """ Scan for available wireless networks.
@@ -179,8 +215,7 @@ class Wireless(Controller):
A list of available networks sorted by strength. A list of available networks sorted by strength.
""" """
wiface = wnettools.WirelessInterface(self.wireless_interface, wiface = self.wiface
self.wpa_driver)
# Prepare the interface for scanning # Prepare the interface for scanning
wiface.Up() wiface.Up()
@@ -193,7 +228,7 @@ class Wireless(Controller):
wiface.SetEssid(essid) wiface.SetEssid(essid)
aps = wiface.GetNetworks() aps = wiface.GetNetworks()
print aps #print aps
aps.sort(key=lambda x: x['strength']) aps.sort(key=lambda x: x['strength'])
return aps return aps
@@ -219,9 +254,7 @@ class Wireless(Controller):
The current signal strength. The current signal strength.
""" """
wiface = wnettools.WirelessInterface(self.wireless_interface, return self.wiface.GetSignalStrength(iwconfig)
self.wpa_driver)
return wiface.GetSignalStrength(iwconfig)
def GetDBMStrength(self, iwconfig=None): def GetDBMStrength(self, iwconfig=None):
""" Get the dBm signal strength of the current network. """ Get the dBm signal strength of the current network.
@@ -230,9 +263,7 @@ class Wireless(Controller):
The current dBm signal strength. The current dBm signal strength.
""" """
wiface = wnettools.WirelessInterface(self.wireless_interface, return self.wiface.GetDBMStrength(iwconfig)
self.wpa_driver)
return wiface.GetDBMStrength(iwconfig)
def GetCurrentNetwork(self, iwconfig=None): def GetCurrentNetwork(self, iwconfig=None):
""" Get current network name. """ Get current network name.
@@ -241,9 +272,7 @@ class Wireless(Controller):
The name of the currently connected network. The name of the currently connected network.
""" """
wiface = wnettools.WirelessInterface(self.wireless_interface, return self.wiface.GetCurrentNetwork(iwconfig)
self.wpa_driver)
return wiface.GetCurrentNetwork(iwconfig)
def GetIP(self): def GetIP(self):
""" Get the IP of the interface. """ Get the IP of the interface.
@@ -252,15 +281,11 @@ class Wireless(Controller):
The IP address of the interface in dotted notation. The IP address of the interface in dotted notation.
""" """
wiface = wnettools.WirelessInterface(self.wireless_interface, return self.wiface.GetIP()
self.wpa_driver)
return wiface.GetIP()
def GetIwconfig(self): def GetIwconfig(self):
""" Get the out of iwconfig. """ """ Get the out of iwconfig. """
wiface = wnettools.WirelessInterface(self.wireless_interface, return self.wiface.GetIwconfig()
self.wpa_driver)
return wiface.GetIwconfig()
def CreateAdHocNetwork(self, essid, channel, ip, enctype, key, def CreateAdHocNetwork(self, essid, channel, ip, enctype, key,
enc_used, ics): enc_used, ics):
@@ -276,8 +301,7 @@ class Wireless(Controller):
ics -- enable internet connection sharing ics -- enable internet connection sharing
""" """
wiface = wnettools.WirelessInterface(self.wireless_interface, wiface = self.wiface
self.wpa_driver)
print 'Creating ad-hoc network' print 'Creating ad-hoc network'
print 'Killing dhclient and wpa_supplicant' print 'Killing dhclient and wpa_supplicant'
wiface.StopDHCP() wiface.StopDHCP()
@@ -288,7 +312,7 @@ class Wireless(Controller):
wiface.SetMode('ad-hoc') wiface.SetMode('ad-hoc')
wiface.SetChannel(channel) wiface.SetChannel(channel)
wiface.SetEssid(essid) wiface.SetEssid(essid)
#Right now it just assumes you're using WEP # Right now it just assumes you're using WEP
if enc_used: if enc_used:
print 'Setting encryption key' print 'Setting encryption key'
wiface.SetKey(key) wiface.SetKey(key)
@@ -332,14 +356,11 @@ class Wireless(Controller):
return wnettools.GetWirelessInterfaces() return wnettools.GetWirelessInterfaces()
def GetKillSwitchStatus(self): def GetKillSwitchStatus(self):
wiface = wnettools.WirelessInterface(self.wireless_interface, return self.wiface.GetKillSwitchStatus()
self.wpa_driver)
return wiface.GetKillSwitchStatus()
def Disconnect(self): def Disconnect(self):
""" Disconnect from the network. """ """ Disconnect from the network. """
wiface = wnettools.WirelessInterface(self.wireless_interface, wiface = self.wiface
self.wpa_driver)
if self.disconnect_script != None: if self.disconnect_script != None:
print 'Running wireless network disconnect script' print 'Running wireless network disconnect script'
misc.ExecuteScript(self.disconnect_script) misc.ExecuteScript(self.disconnect_script)
@@ -347,6 +368,16 @@ class Wireless(Controller):
wiface.SetAddress('0.0.0.0') wiface.SetAddress('0.0.0.0')
wiface.Down() wiface.Down()
wiface.Up() wiface.Up()
def SetDriver(self):
self.driver = self.GetDriverName()
def GetDriverName(self):
""" Gets the driver associated with the wireless interface. """
return self.wiface.GetDriverName()
def SetWPADriver(self, driver):
self.wiface.SetWpaDriver(driver)
class WirelessConnectThread(ConnectThread): class WirelessConnectThread(ConnectThread):
""" A thread class to perform the connection to a wireless network. """ A thread class to perform the connection to a wireless network.
@@ -425,10 +456,9 @@ class WirelessConnectThread(ConnectThread):
wiface.SetAddress('0.0.0.0') wiface.SetAddress('0.0.0.0')
liface.SetAddress('0.0.0.0') liface.SetAddress('0.0.0.0')
print 'Stopping wpa_supplicant, dhclient, dhclient3' print 'Stopping wpa_supplicant, and any running dhcp clients'
wiface.StopDHCP()
wiface.StopWPA() wiface.StopWPA()
liface.StopDHCP() wnettools.StopDHCP()
if self.should_die: if self.should_die:
wiface.Up() wiface.Up()
@@ -561,6 +591,14 @@ class Wired(Controller):
""" Initialise the class. """ """ Initialise the class. """
Controller.__init__(self) Controller.__init__(self)
self.wpa_driver = None self.wpa_driver = None
self.liface = wnettools.WiredInterface(self.wired_interface)
def __setattr__(self, attr, val):
object.__setattr__(self, attr, val)
def LoadInterfaces(self):
""" Load the wnettools controls for the wired/wireless interfaces. """
self.liface = wnettools.WiredInterface(self.wired_interface)
def CheckPluggedIn(self): def CheckPluggedIn(self):
""" Check whether the wired connection is plugged in. """ Check whether the wired connection is plugged in.
@@ -569,8 +607,7 @@ class Wired(Controller):
The status of the physical connection link. The status of the physical connection link.
""" """
liface = wnettools.WiredInterface(self.wired_interface) return self.liface.GetPluggedIn()
return liface.GetPluggedIn()
def Connect(self, network): def Connect(self, network):
""" Spawn a connection thread to connect to the network. """ Spawn a connection thread to connect to the network.
@@ -594,12 +631,11 @@ class Wired(Controller):
The IP address of the interface in dotted notation. The IP address of the interface in dotted notation.
""" """
liface = wnettools.WiredInterface(self.wired_interface) return self.liface.GetIP()
return liface.GetIP()
def Disconnect(self): def Disconnect(self):
""" Disconnect from the network. """ """ Disconnect from the network. """
liface = wnettools.WiredInterface(self.wired_interface) liface = self.liface
if self.disconnect_script != None: if self.disconnect_script != None:
print 'Running wired disconnect script' print 'Running wired disconnect script'
misc.Run(self.disconnect_script) misc.Run(self.disconnect_script)
@@ -607,6 +643,25 @@ class Wired(Controller):
liface.SetAddress('0.0.0.0') liface.SetAddress('0.0.0.0')
liface.Down() liface.Down()
liface.Up() liface.Up()
def SetDriver(self):
self.driver = self.GetDriverName()
def GetDriverName(self):
""" Get the driver associated with the wired interface. """
return self.liface.GetDriverName()
def LoadDriver(self):
return self.liface.LoadDriver(self.driver)
def UnloadDriver(self):
return self.liface.UnloadDriver(self.driver)
def EnableInterface(self):
return self.liface.Up()
def DisableInterface(self):
return self.liface.Down()
class WiredConnectThread(ConnectThread): class WiredConnectThread(ConnectThread):
@@ -680,10 +735,9 @@ class WiredConnectThread(ConnectThread):
wiface.SetAddress('0.0.0.0') wiface.SetAddress('0.0.0.0')
liface.SetAddress('0.0.0.0') liface.SetAddress('0.0.0.0')
print 'Stopping wpa_supplicant, dhclient, dhclient3' print 'Stopping wpa_supplicant, and any dhcp clients'
wiface.StopDHCP()
wiface.StopWPA() wiface.StopWPA()
liface.StopDHCP() wnettools.StopDHCP()
if self.should_die: if self.should_die:
liface.Up() liface.Up()

View File

@@ -32,5 +32,7 @@ proxy_obj = bus.get_object('org.wicd.daemon', '/org/wicd/daemon')
daemon = dbus.Interface(proxy_obj, 'org.wicd.daemon') daemon = dbus.Interface(proxy_obj, 'org.wicd.daemon')
if __name__ == '__main__': if __name__ == '__main__':
daemon.Disconnect()
daemon.SetForcedDisconnect(False)
daemon.SetSuspend(True) daemon.SetSuspend(True)

View File

@@ -376,7 +376,7 @@ class TrayIcon():
def usage(): def usage():
""" Print usage information. """ """ Print usage information. """
print """ print """
wicd 1.40 wicd 1.50
wireless (and wired) connection daemon front-end. wireless (and wired) connection daemon front-end.
Arguments: Arguments:

View File

@@ -99,6 +99,11 @@ def GetDefaultGateway():
gateway = None gateway = None
return gateway return gateway
def StopDHCP():
""" Stop the DHCP client. """
cmd = 'killall dhclient dhclient3 pump dhcpcd-bin'
misc.Run(cmd)
def GetWirelessInterfaces(): def GetWirelessInterfaces():
""" Get available wireless interfaces. """ Get available wireless interfaces.
@@ -122,38 +127,124 @@ class Interface(object):
""" """
self.iface = iface self.iface = iface
self.verbose = verbose self.verbose = verbose
self.Check()
def SetInterface(self, iface):
""" Sets the interface. """
self.iface = iface
def CheckDHCP(self): def CheckDHCP(self):
""" Check that all required tools are available. """ """ Check that all required tools are available. """
# TODO: Implement this function. # TODO: Implement this function.
# THINGS TO CHECK FOR: ethtool, pptp-linux, dhclient, host # THINGS TO CHECK FOR: ethtool, pptp-linux, dhclient, host
dhcpclients = ["dhclient", "dhcpcd", "pump -i"] dhcpclients = ["dhclient", "dhcpcd", "pump"]
for client in dhcpclients: for client in dhcpclients:
if misc.Run("which " + client.split()[0]): if misc.Run("which " + client):
DHCP_CLIENT = client DHCP_CLIENT = client
break break
if not DHCP_CLIENT: if not DHCP_CLIENT:
print "WARNING: NO DHCP CLIENT DETECTED!" print "WARNING: NO DHCP CLIENT DETECTED! Make sure there is one \
set in your path."
return
elif DHCP_CLIENT == "dhclient":
DHCP_CMD = "dhclient"
DHCP_RELEASE = "dhclient -r"
elif DHCP_CLIENT == "pump":
DHCP_CMD = "pump -i"
DHCP_RELEASE = "pump -r -i"
elif DHCP_CLIENT == "dhcpcd":
DHCP_CMD = "dhcpcd"
DHCP_RELEASE = "dhcpcd -r"
self.DHCP_CMD = DHCP_CMD
self.DHCP_RELEASE = DHCP_RELEASE
self.DHCP_CLIENT = DHCP_CLIENT self.DHCP_CLIENT = DHCP_CLIENT
def CheckWiredTools(self):
""" Check for the existence of ethtool and mii-tool. """
if misc.Run("which mii-tool"):
self.MIITOOL_FOUND = True
else:
self.MIITOOL_FOUND = False
if misc.Run("which ethtool"):
self.ETHTOOL_FOUND = True
else:
self.ETHTOOL_FOUND = False
def Check(self): def Check(self):
""" Check that all required tools are available.""" """ Check that all required tools are available."""
# TODO: Implement this function. # TODO: Implement this function.
# THINGS TO CHECK FOR: ethtool, pptp-linux, dhclient, host # THINGS TO CHECK FOR: ethtool, pptp-linux, dhclient, host
pass self.CheckDHCP()
self.CheckWiredTools()
if misc.Run("which ip"):
self.IP_FOUND = True
else:
self.IP_FOUND = False
def Up(self): def Up(self):
""" Bring the network interface up. """ """ Bring the network interface up. """
cmd = 'ifconfig ' + self.iface + ' up' cmd = 'ifconfig ' + self.iface + ' up'
if self.verbose: print cmd if self.verbose: print cmd
misc.Run(cmd) misc.Run(cmd)
return True
def Down(self): def Down(self):
""" Take down the network interface. """ """ Take down the network interface. """
cmd = 'ifconfig ' + self.iface + ' down' cmd = 'ifconfig ' + self.iface + ' down'
if self.verbose: print cmd if self.verbose: print cmd
misc.Run(cmd) misc.Run(cmd)
return True
def GetDriverName(self):
""" Determine the driver name for the interface.
Attempt to use ethtool to get the driver associated with a given
interface. If ethtool is not installed or ethtool fails to provide
a driver, None is returned.
"""
if self.ETHTOOL_FOUND:
cmd = 'ethtool -i ' + self.iface
driver_pattern = re.compile('.*driver: (.*?)\n', re.I | re.M |
re.S)
driver_name = misc.RunRegex(driver_pattern, misc.Run(cmd))
if not driver_name or not self.ETHTOOL_FOUND:
print ("Could not determine driver name for iface " + self.iface +
" Is ethtool installed?")
return driver_name
def LoadDriver(self, driver):
""" Enables the interface by loading the module given by driver. """
if not driver:
print 'Error: No driver associated with this interface.'
return False
cmd = "modprobe " + driver
if self.verbose: print cmd
output = misc.Run(cmd, True, True)
out = output.readlines()
if out and out[0].startswith("FATAL"):
print "Could not enable Interface: " + out[0]
return False
return True
def UnloadDriver(self, driver):
""" Disables the interface by removing the module given by driver """
if not driver:
print 'Error: No driver associated with this interface.'
return False
cmd = "modprobe -r " + driver
if self.verbose: print cmd
output = misc.Run(cmd, True, True)
out = output.readlines()
if out and out[0].startswith("FATAL"):
print "Could not enable Interface: " + out[0]
return False
return True
def SetAddress(self, ip=None, netmask=None, broadcast=None): def SetAddress(self, ip=None, netmask=None, broadcast=None):
""" Set the IP addresses of an interface. """ Set the IP addresses of an interface.
@@ -252,30 +343,30 @@ class Interface(object):
return "dhcp_failed" return "dhcp_failed"
def StartDHCP(self): def StartDHCP(self):
""" Start the DHCP client to obtain an IP address. """ """ Start the DHCP client to obtain an IP address. """
self.CheckDHCP() cmd = self.DHCP_CMD + " " + self.iface
DHCP_CLIENT = self.DHCP_CLIENT
cmd = DHCP_CLIENT + " " + self.iface
if self.verbose: print cmd if self.verbose: print cmd
pipe = misc.Run(cmd, include_stderr=True, return_pipe=True) pipe = misc.Run(cmd, include_stderr=True, return_pipe=True)
DHCP_CLIENT = self.DHCP_CLIENT
if DHCP_CLIENT == "dhclient": if DHCP_CLIENT == "dhclient":
return self._parse_dhclient(pipe) return self._parse_dhclient(pipe)
elif DHCP_CLIENT == "pump -i": elif DHCP_CLIENT == "pump":
return self._parse_pump(pipe) return self._parse_pump(pipe)
elif DHCP_CLIENT == "dhcpcd": elif DHCP_CLIENT == "dhcpcd":
return self._parse_dhcpcd(pipe) return self._parse_dhcpcd(pipe)
def StopDHCP(self): def ReleaseDHCP(self):
""" Stop the DHCP client. """ """ Release the DHCP lease for this interface. """
cmd = 'killall dhclient dhclient3 pump dhcpcd-bin' cmd = self.DHCP_RELEASE + " " + self.iface
if self.verbose: print cmd
misc.Run(cmd) misc.Run(cmd)
def FlushRoutes(self): def FlushRoutes(self):
""" Flush all network routes. """ """ Flush all network routes. """
cmd = 'ip route flush dev ' + self.iface if self.IP_FOUND:
cmd = "ip route flush dev " + self.iface
else:
cmd = 'route del dev ' + self.iface
if self.verbose: print cmd if self.verbose: print cmd
misc.Run(cmd) misc.Run(cmd)
@@ -323,25 +414,44 @@ class WiredInterface(Interface):
mii-tool will be used instead. mii-tool will be used instead.
""" """
if self.ETHTOOL_FOUND:
return self._eth_get_plugged_in()
elif self.MIITOOL_FOUND:
return self._mii_get_plugged_in()
else:
print 'Error: No way of checking for a wired connection. Make \
sure that either mii-tool or ethtool is installed.'
return False
def _eth_get_plugged_in(self):
link_tool = 'ethtool' link_tool = 'ethtool'
if not self.IsUp(): if not self.IsUp():
print 'Wired Interface is down, putting it up' print 'Wired Interface is down, putting it up'
self.Up() self.Up()
time.sleep(6) time.sleep(6)
tool_data = misc.Run(link_tool + ' ' + self.iface, True) tool_data = misc.Run(link_tool + ' ' + self.iface, True)
if misc.RunRegex(re.compile('(Operation not supported|\ if misc.RunRegex(re.compile('(Link detected: yes)', re.I | re.M |
ethtool: command not found)', re.I), re.S), tool_data) is not None:
tool_data) is not None:
print "ethtool check failed, falling back to mii-tool"
link_tool = 'mii-tool'
tool_data = misc.Run(link_tool + ' ' + self.iface, True)
if misc.RunRegex(re.compile('(Link detected: yes|link ok)',
re.I | re.M | re.S), tool_data) is not None:
return True return True
else: else:
return False return False
def _mii_get_plugged_in(self):
link_tool = 'mii-tool'
tool_data = misc.Run(link_tool + ' ' + self.iface, True)
if misc.RunRegex(re.compile('(Invalid argument)', re.I | re.M | re.S),
tool_data) is not None:
print 'Wired Interface is down, putting it up'
self.Up()
time.sleep(4)
tool_data = misc.Run(link_tool + ' ' + self.iface, True)
if misc.RunRegex(re.compile('(link ok)', re.I | re.M | re.S),
tool_data) is not None:
return True
else:
return False
def IsUp(self): def IsUp(self):
""" Determines if the interface is up. """ """ Determines if the interface is up. """
cmd = "ifconfig " + self.iface cmd = "ifconfig " + self.iface
@@ -369,6 +479,10 @@ class WirelessInterface(Interface):
""" """
Interface.__init__(self, iface, verbose) Interface.__init__(self, iface, verbose)
self.wpa_driver = wpa_driver self.wpa_driver = wpa_driver
def SetWpaDriver(self, driver):
""" Sets the wpa_driver. """
self.wpa_driver = driver
def SetEssid(self, essid): def SetEssid(self, essid):
""" Set the essid of the wireless interface. """ Set the essid of the wireless interface.