def start_hotspot(): """ Starts the hotspot using nmcli. """ global hotspot_ssid global hotspot_pswd global hotspot_enabled global wifi_on try: print("Turning on Wi-Fi...") bt_send("Busy=Turning on Wi-Fi...") print(str(nmcli("radio", "wifi", "on"))) wifi_on = True bt_send("WiFi=True") print("Done.") print("Starting hotspot...") bt_send("Busy=Starting hotspot...") try: print(str(nmcli("device", "wifi", "hotspot", "con-name", hotspot_ssid, "ssid", hotspot_ssid, "band", "bg", "password", hotspot_pswd))) hotspot_enabled = True bt_send("Hotspot=True") print("Done.") except ErrorReturnCode as e: log_err("Unable to start the hotspot!") print(str(e)) except ErrorReturnCode as e: log_err("Unable to turn on Wi-Fi!") print(str(e))
def get_connected_wifi(): try: for device in nmcli('d'): if ' connected' in device.strip(): return ' '.join(device.split()[3:]) except Exception: logging.exception('get connected wifi')
def stop_wireless(): ''' Try official ways to stop wireless such as nmcli and rfkill. These often leave the service enabled, or the service is re-enabled on boot. To do: check rmcomm piconets ''' if not sh.which('nm'): sh.aptitude('install', 'nmcli') assert sh.which('nm') if not sh.which('service'): service_path = '/usr/local/sbin/service' with open(service_path, 'w') as service_file: service_file.write(service_script_text) os.chmod(service_path, 0o755) assert sh.which('service') try: sh.nmcli('nm', 'wifi', 'off') sh.nmcli('nm', 'wwan', 'off') except: pass # rfkill block all try: #if not sh.which ('rfkill'): # sh.aptitude('install', 'rfkill') #assert sh.which ('rfkill') sh.rfkill('block', 'all') except: # some variants of linux don't have /dev/rfkill, # so there's no program rfkill pass # /etc/init.d/bluetooth stop try: sh.service(Bluetooth, 'stop') except: try: sh.service(Bluetooth+'-unused', 'stop') except: pass
def stop_hotspot(): global hotspot_enabled bt_send("Busy=Stopping hotspot...") print("Stopping hotspot...") try: print(str(nmcli("connection", "down", hotspot_ssid))) hotspot_enabled = False bt_send("Hotspot=False") print("Done.") except ErrorReturnCode as e: log_err("Unable to stop the hotspot!") print(str(e))
def turn_on_wifi(): global wifi_on try: print("Turning on Wi-Fi...") bt_send("Busy=Turning on Wi-Fi...") print(str(nmcli("radio", "wifi", "on"))) wifi_on = True bt_send("WiFi=True") print("Done.") except ErrorReturnCode as e: log_err("Unable to turn on Wi-Fi!") print(str(e))
def connect_to_wifi(ssid, password): try: with sudo: nmcli('c', 'delete', ssid) except Exception: pass try: with sudo: nmcli('d', 'disconnect', 'wlan0') except Exception: pass with sudo: nmcli('d', 'wifi', 'connect', ssid, 'password', password)
def nmcli_get_connections(pattern=None, pattern_ignore=None, fields=None, active=False): """ Gets the connections using nmcli :param pattern: string :param pattern_ignore: string :param fields: iterable objects :param active: boolean :return: list """ escape_color_pattern = re.compile(r'\x1B\[[0-?]*[ -/]*[@-~]') if not fields: fields = ['name', 'uuid', 'type', 'device'] params = list() if active: params.append('--active') out = sh.nmcli('-t', '-f', ','.join(fields), 'c', 'show', *params) connections = re.sub(escape_color_pattern, '', out.encode('utf-8')).splitlines() if pattern: connections = filter(re.compile(pattern).search, connections) if pattern_ignore: connections = filter( lambda x: re.compile(pattern_ignore).search(x) is None, connections) return connections
def parse_rfcomm(line): """ Parses the command received from the client and executes it. """ global type_pswd global ap global net_interface global net_scan global wifi_on global hotspot_enabled global led_thread_run global emergency_led_run if type_pswd is True: type_pswd = False if line != "#": print("Connecting to Wi-Fi AP " + ap) bt_send("Busy=Connecting to Wi-Fi AP " + ap) try: print(str(nmcli("device", "wifi", "connect", ap, "password", line))) print("Done.") send_ip() except ErrorReturnCode as e: log_err("Unable to connect!") print(str(e)) else: if len(line) == 2: if line == "01": if hotspot_enabled is True: stop_hotspot() try: print("Turning off Wi-Fi...") bt_send("Busy=Turning off Wi-Fi...") print(str(nmcli("radio", "wifi", "off"))) wifi_on = False bt_send("WiFi=False") print("Done.") except ErrorReturnCode as e: log_err("Unable to turn off Wi-Fi!") print(str(e)) elif line == "02": turn_on_wifi() elif line == "03": start_hotspot() elif line == "04": stop_hotspot() elif line == "05": send_ip() elif line == "06": bt_send("Busy=Looking for Wi-Fi access points...") print("Looking for Wi-Fi access points...") try: net_scan = list(Cell.all(net_interface)) if len(net_scan) == 0: bt_send("WiFiAPs=[]") else: net_scan.sort(key=get_ap_quality, reverse=True) msg = "WiFiAPs=[" + net_scan[0].ssid + \ "(" + net_scan[0].quality + ")" for i in range(1, min(len(net_scan), 10)): msg = msg + ", " + \ net_scan[i].ssid + \ "(" + net_scan[i].quality + ")" bt_send(msg + "]") print("Done.") except InterfaceError as e: log_err("Unable to scan!") print(str(e)) elif line == "07": shutdown_pi() elif line == "08": log("Rebooting...") led_thread_run = False emergency_led_run = False sleep(0.5) reboot("now") elif line[0] == '1': i = int(line[1]) if 0 <= i < min(len(net_scan), 10): ap = str(net_scan[i].ssid) print("Connecting to Wi-Fi AP " + ap) bt_send("Busy=Connecting to Wi-Fi AP " + ap) try: print(str(nmcli("connection", "up", "id", ap))) print("Done.") send_ip() except ErrorReturnCode as e: bt_send("TypePswd=" + ap) type_pswd = True else: log_err("Invalid command!") elif line[0] == '2': if line[1] == '0': stop_indi() elif line[1] == '1': indiweb_start() else: print("Received: \"" + line + "\"") log_err("Invalid command!") else: print("Received: \"" + line + "\"") log_err("Invalid command!") else: print("Received: \"" + line + "\"") log_err("Invalid command!")
def connect_to_any_wifi(): try: with sudo: nmcli('d', 'connect', 'wlan0') except Exception: logging.exception('connect to any wifi')
def enableWifi(): sh.nmcli("radio", "wifi", "on") time.sleep(5) print("WiFi enabled!")
def disableWifi(): sh.nmcli("radio", "wifi", "off") print("WiFi disabled!")
#!/usr/bin/env python import sh # get table of connections from nmcli connections = str(sh.nmcli( "--colors", "no", "--fields", "name,type,state", "connection", "show")) # split the table into header and rows header, rows = connections.split("\n", 1) # use rofi promt to chose connection selection = sh.rofi(sh.echo(rows), "-dmenu", "-i", "-p", "Toggle Connection", "-mesg", header) name = selection.split()[0] state = selection.split()[-1] # toggle connection command = "up" if state == "--" else "down" sh.nmcli("connection", command, name)