Exemplo n.º 1
0
def get_channel(if_name):
    """Returns the channel the given interface is currently operating on. 

    """
    if if_name not in iwlibs.getWNICnames():
        raise CHANError("Unable to set channel (invalid wireless interface: %s)" % if_name)
    interface = iwlibs.Wireless(if_name)
    return interface.getChannel()
Exemplo n.º 2
0
    def start_servers(self):
        """Starts the servers that listen for messages from neighboring nodes.
        If the IP of an interface has changed, the server is restarted to listen
        at the new IP. If interfaces have been shut down, servers are stopped.

        """
        for if_name in iwlibs.getWNICnames():
            if not util.is_interface_up(if_name):
                # interface is down
                if if_name in self._msg_ports.keys():
                    # stop listening for messages if we previously used it
                    syslog(LOG_DEBUG, str(datetime.now()) + " Messaging:start_servers: %s: interface is down, stop listening for messages at %s:%s" % (if_name, self._msg_ports[if_name].getHost().host, self._msg_ports[if_name].getHost().port))
                    self._msg_ports[if_name].stopListening()
                    del self._msg_ports[if_name]
                # stop listening for updates if we previously used the interface
                if if_name in self._upd_ports.keys():
                    syslog(LOG_DEBUG, str(datetime.now()) + " Messaging:start_servers: %s: interface is down, stop listening for updates at %s:%s" % (if_name, self._upd_ports[if_name][0].getHost().host, self._upd_ports[if_name][0].getHost().port))
                    self._upd_ports[if_name][0].stopListening()
                    del self._upd_ports[if_name]
            else:
                # interface is up, determine current ip and broadcast address
                try:
                    ip = netifaces.ifaddresses(if_name)[netifaces.AF_INET][0]['addr']
                    bcast = netifaces.ifaddresses(if_name)[netifaces.AF_INET][0]['broadcast']
                except KeyError:
                    syslog(LOG_DEBUG, str(datetime.now()) + " Messaging:start_servers: %s: unable to determine IP address, although the interface seems to be up " % (if_name))
                    continue
                # check if IP has changed
                if if_name in self._msg_ports.keys() and \
                   ip != self._msg_ports[if_name].getHost().host:
                    syslog(LOG_DEBUG, str(datetime.now()) + " Messaging:start_servers: %s: IP address has changed, stop listening at  %s:%s" % (if_name, self._msg_ports[if_name].getHost().host, self._msg_ports[if_name].getHost().port))
                    # ip has changed, stop listening at the old address
                    self._msg_ports[if_name].stopListening()
                    del self._msg_ports[if_name]
                # check if broadcast address has changed
                if if_name in self._upd_ports.keys() and \
                   bcast != self._upd_ports[if_name][0].getHost().host:
                    syslog(LOG_DEBUG, str(datetime.now()) + " Messaging:start_servers: %s: broadcast address has changed, stop listening at  %s:%s" % (if_name, self._upd_ports[if_name][0].getHost().host, self._upd_ports[if_name][0].getHost().port))
                    # broadcast address has changed, stop listening at the old
                    # address
                    self._upd_ports[if_name][0].stopListening()
                    del self._upd_ports[if_name]
                # start listening for messages if necessary
                if if_name not in self._msg_ports.keys():
                    # listen at the new address
                    factory = MessageServerFactory(self.mica)
                    self._msg_ports[if_name] = reactor.listenTCP(self.PORT,
                                                                 factory, 20,
                                                                 ip)
                    syslog(LOG_DEBUG, str(datetime.now()) + " Messaging:start_servers: %s: listening at %s:%s" % (if_name, ip, self.PORT))
                # start listening for updates if necessary
                if if_name not in self._upd_ports.keys():
                    # listen at the new address
                    protocol = UpdateProtocol(self.mica)
                    port = reactor.listenUDP(self.PORT, protocol, bcast)
                    self._upd_ports[if_name] = (port, protocol)
                    syslog(LOG_DEBUG, str(datetime.now()) + " Messaging:start_servers: %s: listening at %s:%s" % (if_name, bcast, self.PORT))
Exemplo n.º 3
0
def shut_down_interfaces(if_names=None):
    """Wrapper to shut down more than one interface.

    """
    # if no interfaces are specified, use all wireless interfaces
    if not if_names:
        if_names = iwlibs.getWNICnames()
    for if_name in if_names:
        shut_down_interface(if_name)
Exemplo n.º 4
0
def get_if_name(channel):
    """Returns the name of the interface that is tuned to the given channel.

    """
    for if_name in iwlibs.getWNICnames():
        try:
            tuned_channel = iwlibs.Wireless(if_name).getChannel()
        except IOError:
            continue
        if tuned_channel == channel and is_interface_up(if_name):
            return if_name
    raise CHANError("No interface tuned to channel %s and set up" % channel)
Exemplo n.º 5
0
def get_free_if_name(if_names=None):
    """Check (and return) a so far unused network interface

    """
    # if no interfaces are specified, use all wireless interfaces
    if not if_names:
        if_names = iwlibs.getWNICnames()
    for if_name in if_names:
        print if_name
        if not is_interface_up(if_name):
            return if_name
    raise CHANError("No free interfaces left")
Exemplo n.º 6
0
def set_channel(if_name, channel, set_ip=True):
    """Set the channel for the interface. This is a bit more complicated since we also have
    to set the ESSID, Cell ID,  and the IP according to /etc/hosts.

    """
    if if_name not in iwlibs.getWNICnames():
        raise CHANError("Unable to set channel (invalid wireless interface: %s)" % if_name)
    interface = iwlibs.Wireless(if_name)
    try:
        print if_name, channel
        interface.setChannel(channel)
    except ValueError, IOError:
        raise CHANError("Unable to set channel (invalid channel: %s)" % channel)
Exemplo n.º 7
0
def main(wifinic):
    """List interfaces and create one graph window each."""
    try:
        wifi = iwlibs.Wireless(wifinic)
        window = Window(wifi)
        window.start()
    except IOError:
        wifinics = iwlibs.getWNICnames()
        for wifinic in wifinics:
            message = "Not a valid Wifi interface, trying: {}".format(wifinic)
            warnings.warn(message, SyntaxWarning)
            try:
                wifi = iwlibs.Wireless(wifinic)
                window = Window(wifi)
                window.start()
            except:
                pass
    except:
        raise
Exemplo n.º 8
0
    def poll(self):
        """
        Poll. Return a data list of metric 3-tuples (name, value, timestamp)

        :return: data list of metric 3-tuples (name, value, timestamp)
        :rtype: ``list``
        """
        logger.info('Polling WiFi stats')
        ts = int(time.time())
        stats = []
        try:
            nicnames = getWNICnames()
        except Exception:
            logger.error('Error getting WNIC names; cannot poll wifi',
                         exc_info=True)
            return []
        for n in nicnames:
            try:
                stats.extend(self._poll_nic(ts, n))
            except Exception:
                logger.error('Error polling NIC %s', n, exc_info=True)
        return stats
Exemplo n.º 9
0
 def getWirelessInterfaces(self):
     return getWNICnames()
Exemplo n.º 10
0
	def getWirelessInterfaces(self):
		return getWNICnames()
Exemplo n.º 11
0
def iwconfig(interface):
    """ Get wireless information from the device driver. """
    if interface not in getWNICnames():
        print "%-8.16s  no wireless extensions." % (interface, )
    else:
        wifi = Wireless(interface)
        line = """%-8.16s  %s  """ % (interface, wifi.getWirelessName())
        if (wifi.getEssid()):
            line = line + """ESSID:"%s"  \n          """ % (wifi.getEssid(), )
        else:
            line = line + "ESSID:off/any  \n          "

        # Mode, Frequency, and Access Point
        line = line + "Mode:" + wifi.getMode()
        try:
            line = line + "  Frequency:" + wifi.getFrequency()
        except IOError, (error_number, error_string):
            # Some drivers do not return frequency info if not associated
            pass

        if (wifi.wireless_info.getMode() == IW_MODE_ADHOC):
            ap_type = "Cell"
        else:
            ap_type = "Access Point"
        ap_addr = wifi.getAPaddr()
        if (ap_addr == "00:00:00:00:00:00"):
            ap_addr = "Not-Associated"
        line = line + "  " + ap_type + ": " + ap_addr + "   "
        print line

        # Bit Rate, TXPower, and Sensitivity line
        line = "          "
        bitrate = getBitrate(wifi)
        if bitrate:
            line = line + bitrate
        txpower = getTXPower(wifi)
        if txpower:
            line = line + txpower
        sensitivity = getSensitivity(wifi)
        if sensitivity:
            line = line + sensitivity
        print line

        # Retry, RTS, and Fragmentation line
        line = "          "
        retry = getRetrylimit(wifi)
        if retry:
            line = line + retry
        rts = getRTS(wifi)
        if rts:
            line = line + rts
        fragment = getFragmentation(wifi)
        if fragment:
            line = line + fragment
        print line

        # Encryption line
        line = "          "
        line = line + getEncryption(wifi)
        print line

        # Power Management line
        line = "          "
        line = line + getPowerManagement(wifi)
        print line

        try:
            stat, qual, discard, missed_beacon = wifi.getStatistics()
        except IOError, (error_number, error_string):
            # Some drivers do not return statistics info if not associated
            pass
Exemplo n.º 12
0
def iwconfig(interface):
    """ Get wireless information from the device driver. """
    if interface not in getWNICnames():
        print "%-8.16s  no wireless extensions." % (interface, )
    else:
        wifi = Wireless(interface)
        line = """%-8.16s  %s  """ % (interface, wifi.getWirelessName())
        if (wifi.getEssid()):
            line = line + """ESSID:"%s"  \n          """ % (wifi.getEssid(), )
        else:
            line = line + "ESSID:off/any  \n          "

        # Mode, Frequency, and Access Point
        line = line + "Mode:" + wifi.getMode()
        try:
            line = line + "  Frequency:" + wifi.getFrequency()
        except IOError as e:
            if (sys.version_info[0] == 3):
                error_number, error_string = e.args
            else:
                error_number = e[0]
                error_string = e[1]
            # Some drivers do not return frequency info if not associated
            pass

        if (wifi.wireless_info.getMode() == pythonwifi.flags.IW_MODE_ADHOC):
            ap_type = "Cell"
        else:
            ap_type = "Access Point"
        ap_addr = wifi.getAPaddr()
        if (ap_addr == "00:00:00:00:00:00"):
            ap_addr = "Not-Associated"
        line = line + "  " + ap_type + ": " + ap_addr + "   "
        print line

        # Bit Rate, TXPower, and Sensitivity line
        line = "          "
        bitrate = getBitrate(wifi)
        if bitrate:
            line = line + bitrate
        txpower = getTXPower(wifi)
        if txpower:
            line = line + txpower
        sensitivity = getSensitivity(wifi)
        if sensitivity:
            line = line + sensitivity
        print line

        # Retry, RTS, and Fragmentation line
        line = "          "
        retry = getRetrylimit(wifi)
        if retry:
            line = line + retry
        rts = getRTS(wifi)
        if rts:
            line = line + rts
        fragment = getFragmentation(wifi)
        if fragment:
            line = line + fragment
        print line

        # Encryption line
        line = "          "
        line = line + getEncryption(wifi)
        print line

        # Power Management line
        line = "          "
        line = line + getPowerManagement(wifi)
        print line

        try:
            stat, qual, discard, missed_beacon = wifi.getStatistics()
        except IOError as e:
            if (sys.version_info[0] == 3):
                error_number, error_string = e.args
            else:
                error_number = e[0]
                error_string = e[1]
            # Some drivers do not return statistics info if not associated
            pass
        else:
            # Link Quality, Signal Level and Noise Level line
            line = "          "
            line = line + "Link Quality:%s/100  " % (qual.quality, )
            line = line + "Signal level:%sdBm  " % (qual.signallevel, )
            line = line + "Noise level:%sdBm" % (qual.noiselevel, )
            print line
            # Rx line
            line = "          "
            line = line + "Rx invalid nwid:%s  " % (discard['nwid'], )
            line = line + "Rx invalid crypt:%s  " % (discard['code'], )
            line = line + "Rx invalid frag:%s" % (discard['fragment'], )
            print line
            # Tx line
            line = "          "
            line = line + "Tx excessive retries:%s  " % (discard['retries'], )
            line = line + "Invalid misc:%s   " % (discard['misc'], )
            line = line + "Missed beacon:%s" % (missed_beacon, )
            print line

    print
Exemplo n.º 13
0
def iwconfig(interface):
    """ Get wireless information from the device driver. """
    if interface not in getWNICnames():
        print "%-8.16s  no wireless extensions." % (interface, )
    else:
        wifi = Wireless(interface)
        print """%-8.16s  %s  ESSID:"%s" """ % (interface,
            wifi.getWirelessName(), wifi.getEssid())
        if (wifi.wireless_info.getMode() == pythonwifi.flags.IW_MODE_ADHOC):
            ap_type = "Cell"
        else:
            ap_type = "Access Point"
        ap_addr = wifi.getAPaddr()
        if (ap_addr == "00:00:00:00:00:00"):
            ap_addr = "Not-Associated"
        print """          Mode:%s  Frequency:%s  %s: %s""" % (
            wifi.getMode(), wifi.getFrequency(), ap_type, ap_addr)

        # Bit Rate, TXPower, and Sensitivity line
        line = "          "
        bitrate = getBitrate(wifi)
        if bitrate:
            line = line + bitrate
        txpower = getTXPower(wifi)
        if txpower:
            line = line + txpower
        sensitivity = getSensitivity(wifi)
        if sensitivity:
            line = line + sensitivity
        print line

        # Retry, RTS, and Fragmentation line
        line = "          "
        retry = getRetrylimit(wifi)
        if retry:
            line = line + retry
        rts = getRTS(wifi)
        if rts:
            line = line + rts
        fragment = getFragmentation(wifi)
        if fragment:
            line = line + fragment
        print line

        # Encryption line
        line = "          "
        line = line + getEncryption(wifi)
        print line

        # Power Management line
        line = "          "
        line = line + getPowerManagement(wifi)
        print line

        stat, qual, discard, missed_beacon = wifi.getStatistics()

        # Link Quality, Signal Level and Noise Level line
        line = "          "
        line = line + "Link Quality:%s/100  " % (qual.quality, )
        line = line + "Signal level:%sdBm  " % (qual.signallevel, )
        line = line + "Noise level:%sdBm" % (qual.noiselevel, )
        print line

        # Rx line
        line = "          "
        line = line + "Rx invalid nwid:%s  " % (discard['nwid'], )
        line = line + "Rx invalid crypt:%s  " % (discard['code'], )
        line = line + "Rx invalid frag:%s" % (discard['fragment'], )
        print line

        # Tx line
        line = "          "
        line = line + "Tx excessive retries:%s  " % (discard['retries'], )
        line = line + "Invalid misc:%s   " % (discard['misc'], )
        line = line + "Missed beacon:%s" % (missed_beacon, )
        print line

    print
Exemplo n.º 14
0
def iwconfig(interface):
    """ Get wireless information from the device driver. """
    if interface not in getWNICnames():
        print "%-8.16s  no wireless extensions." % (interface, )
    else:
        wifi = Wireless(interface)
        line = """%-8.16s  %s  """ % (interface, wifi.getWirelessName())
        if (wifi.getEssid()):
            line = line + """ESSID:"%s"  \n          """ % (wifi.getEssid(), )
        else:
            line = line + "ESSID:off/any  \n          "

        # Mode, Frequency, and Access Point
        line = line + "Mode:" + wifi.getMode()
        try:
            line = line + "  Frequency:" + wifi.getFrequency()
        except IOError, (error_number, error_string):
            # Some drivers do not return frequency info if not associated
            pass

        if (wifi.wireless_info.getMode() == pythonwifi.flags.IW_MODE_ADHOC):
            ap_type = "Cell"
        else:
            ap_type = "Access Point"
        ap_addr = wifi.getAPaddr()
        if (ap_addr == "00:00:00:00:00:00"):
            ap_addr = "Not-Associated"
        line = line + "  " + ap_type + ": " + ap_addr + "   "
        print line

        # Bit Rate, TXPower, and Sensitivity line
        line = "          "
        bitrate = getBitrate(wifi)
        if bitrate:
            line = line + bitrate
        txpower = getTXPower(wifi)
        if txpower:
            line = line + txpower
        sensitivity = getSensitivity(wifi)
        if sensitivity:
            line = line + sensitivity
        print line

        # Retry, RTS, and Fragmentation line
        line = "          "
        retry = getRetrylimit(wifi)
        if retry:
            line = line + retry
        rts = getRTS(wifi)
        if rts:
            line = line + rts
        fragment = getFragmentation(wifi)
        if fragment:
            line = line + fragment
        print line

        # Encryption line
        line = "          "
        line = line + getEncryption(wifi)
        print line

        # Power Management line
        line = "          "
        line = line + getPowerManagement(wifi)
        print line

        try:
            stat, qual, discard, missed_beacon = wifi.getStatistics()
        except IOError, (error_number, error_string):
            # Some drivers do not return statistics info if not associated
            pass