def configureBhaulInterface(essid): """Configure an interface to talk to an access point""" try: log_info("Configuring backhaul interface onto AP: %s" % essid) # Take interface down log_command("/sbin/ifdown %s 2>&1" % BACKHAUL_IFNAME) # Write /etc/network/interfaces stanza ifaces = debian_interfaces() iface = ifaces[BACKHAUL_IFNAME] iface["wpa-ssid"] = essid ifaces[BACKHAUL_IFNAME] = iface try: remountrw("configureBhaulInterface") ifaces.write() finally: remountro("configureBhaulInterface") # Bring Up log_command("/sbin/ifup %s 2>&1" % BACKHAUL_IFNAME) ifaces = getInterfaces(returnOne=BACKHAUL_IFNAME) if len(ifaces) != 1 or not ifaces[0]["up"]: # Failed log_error("Failed to configure and bring up backhaul " \ "interface (%s)" % BACKHAUL_IFNAME) return False return True except: log_error("Unexpected exception while configuring backhaul!", sys.exc_info()) return False
def getBackhaulStatus(): """Returns the status of the backhaul interface""" nopeer = {"snr":0,"signal":0,"noise":0} ifaces = getInterfaces(returnOne=BACKHAUL_IFNAME) if len(ifaces) != 1: return ("critical", "Backhaul interface not found", nopeer) iface = ifaces[0] wiface = iwtools.get_interface(BACKHAUL_IFNAME) if not iface["up"]: ifaces = debian_interfaces() apname = ifaces[BACKHAUL_IFNAME]["wpa-ssid"].replace(ESSID_PREFIX, "") return ("critical", "%s - Not associated" % apname, nopeer) essid = wiface.get_essid() if not essid.startswith(ESSID_PREFIX) and essid != "Any": log_warn("Associated to invalid AP: %s" % essid) return ("warning", "Not associated to correct Access Point", nopeer) parts = essid.split("-") stats = wiface.get_stats() if getActiveUsername() != getISPUsername(): return ("warning", "Connected as '%s' not '%s'" % \ (getActiveUsername(), getISPUsername()), stats) return ("ok", "%s" % "-".join(parts[1:]), stats)
def checkUpstream(): ifaces = debian_interfaces() diface = ifaces[BACKHAUL_IFNAME] if diface["wpa-ssid"] == "default": # Not configured, scan for valid APs try: log_command("/sbin/ip link set up dev %s 2>&1" % BACKHAUL_IFNAME) wiface = iwtools.get_interface(BACKHAUL_IFNAME) scanres = wiface.scanall() except: log_error("Could not complete scan on backhaul interface (%s)!" % \ BACKHAUL_IFNAME, sys.exc_info()) scanres = [] # Exclude networks that don't match our ESSID aps = [ n for n in scanres if n.essid.startswith(ESSID_PREFIX)] # Pick the one with the highest SNR best = None for ap in aps: if best is None or ap.qual["snr"] > best.qual["snr"]: best = ap # Select it if best is not None: log_info("Selecting Access Point '%s' based on SNR strength" % \ best.essid) configureBhaulInterface(best.essid) # Hope for the best return # Check that the backhaul is up doUp = False ifaces = getInterfaces(returnOne=BACKHAUL_IFNAME) if len(ifaces) != 1: ensureBackhaulUp() return iface = ifaces[0] if not iface["up"]: ensureBackhaulUp() return # Check credentials are up to date if diface["wpa-identity"] != getActiveUsername() or \ diface["wpa-password"] != getActivePassword(): # Take down so that we re-authenticate log_info("Triggering reassociation to update credentials") log_command("/sbin/ifdown %s 2>&1" % BACKHAUL_IFNAME) ensureBackhaulUp() return # Check IP layer is OK dg = getGatewayIP() if dg == "": log_info("Triggering reassociation to acquire gateway") log_command("/sbin/ifdown %s 2>&1" % BACKHAUL_IFNAME) ensureBackhaulUp() return
def setISPDetails(username, password): try: # Write /etc/network/interfaces stanza ifaces = debian_interfaces() iface = ifaces[BACKHAUL_IFNAME] iface["wpa-identity"] = username iface["wpa-password"] = password ifaces[BACKHAUL_IFNAME] = iface try: remountrw("setISPDetails") ifaces.write() finally: remountro("setISPDetails") return True except: log_error("Unexpected exception while configuring ISP details!", sys.exc_info()) return False
def getISPPassword(): """Returns the currently configured ISP Password""" ifaces = debian_interfaces() return ifaces[BACKHAUL_IFNAME]["wpa-password"]
def getISPUsername(): """Returns the currently configured ISP Username""" ifaces = debian_interfaces() return ifaces[BACKHAUL_IFNAME]["wpa-identity"]