Sets the monitor mode (or remove it) from an interface.
    params:
     - iface: the iwconfig interface
     - monitor: True if the interface should be set in monitor mode,
                False if it should be in managed mode

def set_iface_monitor(iface, monitor):
    """Sets the monitor mode (or remove it) from an interface.
    params:
     - iface: the iwconfig interface
     - monitor: True if the interface should be set in monitor mode,
                False if it should be in managed mode
    """
    mode = get_iface_mode(iface)
    if mode == "unknown":
        warning("Could not parse iwconfig !")
    current_monitor = mode == "monitor"
    if monitor == current_monitor:
        # Already correct
        return True
    s_mode = "monitor" if monitor else "managed"

    def _check_call(commands):
        p = subprocess.Popen(commands,
                             stderr=subprocess.PIPE,
                             stdout=subprocess.PIPE)
        stdout, stderr = p.communicate()
        if p.returncode != 0:
            warning("%s failed !" % " ".join(commands))
            return False
        return True
    try:
        assert _check_call(["ifconfig", iface, "down"])
        assert _check_call(["iwconfig", iface, "mode", s_mode])
        assert _check_call(["ifconfig", iface, "up"])
        return True
    except AssertionError:
        return False