def modem_setup(host, passwd, wifi_passwd): modem = Compal(host, passwd) modem.login() # And/or change wifi settings wifi = WifiSettings(modem) settings = wifi.wifi_settings settings.radio_2g.ssid = '🏚 Open' # \u1F3DA' settings.radio_2g.mode = 1 settings.radio_2g.security = 0 # 8 = WPA2/PSK # 20/40MHz settings.radio_5g.ssid = '\U0001F916\u2028\u2028\u0085🏚\u00A0 Open' settings.radio_5g.mode = 1 settings.radio_5g.security = 0 if settings.radio_5g.security == 0 or settings.radio_2g.security == 0: print('[warning]: WiFi security is disabled') settings.radio_2g.pre_shared_key = wifi_passwd settings.radio_5g.pre_shared_key = wifi_passwd wifi.update_wifi_settings(settings) wifi = WifiSettings(modem) settings = wifi.wifi_settings pprint.pprint(settings) modem.logout()
def switch(host, password, state, band, guest, pause, verbose=False): if guest: if state == Switch.OFF: raise ValueError('Argument guest (--guest, -g) not allowed for switch OFF action!') modem = Compal(host, password) modem.login() wifi = WifiSettings(modem) settings = wifi.wifi_settings print(f"Switching wifi {state.name} (band = {band})") if band == Band.BAND_2G or band == Band.ALL: settings.radio_2g.bss_enable = 1 if state == Switch.ON else 2 if band == Band.BAND_5G or band == Band.ALL: settings.radio_5g.bss_enable = 1 if state == Switch.ON else 2 if state == Switch.ON: band_mode_mask_on = { Band.BAND_2G: 1, Band.BAND_5G: 2, Band.ALL: 3 } settings.band_mode = (settings.band_mode & 3) | band_mode_mask_on.get(band, None) else: band_mode_mask_off = { Band.BAND_2G: 2, Band.BAND_5G: 1, Band.ALL: 0 } new_mode = settings.band_mode & band_mode_mask_off.get(band, None) settings.band_mode = new_mode if new_mode != 0 else 4 wifi.update_wifi_settings(settings, verbose) modem.logout() print(f"Wait {pause}s till wifi state is changed ...") time.sleep(pause) if guest: modem.login() wifi_guest_network = WifiGuestNetworkSettings(modem) guest_settings = wifi_guest_network.wifi_guest_network_settings wifi_guest_network.update_wifi_guest_network_settings(guest_settings.properties, True) modem.logout() print(f"Wait {pause}s till wifi state is changed ...") time.sleep(pause) print("Finished.")
def change_wifi_to(ip, password, enableWifi): lock.acquire() modem = Compal(ip, password) modem.login() # And/or change wifi settings wifi = WifiSettings(modem) settings = wifi.wifi_settings if enableWifi: if settings.radio_2g.bss_enable == 2: # ENABLE 2.4Ghz settings.radio_2g.bss_enable = 1 else: modem.logout() lock.release() return "WIFI was already ON" if not enableWifi: if settings.radio_2g.bss_enable == 1: # DISABLE 2.4Ghz settings.radio_2g.bss_enable = 2 else: modem.logout() lock.release() return "WIFI was already OFF" wifi.update_wifi_settings(settings, False) wifi = WifiSettings(modem) settings = wifi.wifi_settings # pprint.pprint(settings) if enableWifi and settings.radio_2g.bss_enable == 1: to_return = "WIFI (" + str(wifi.wifi_settings.radio_2g.ssid) + ") is now ON!" elif not enableWifi and settings.radio_2g.bss_enable == 2: to_return = "WIFI (" + str(wifi.wifi_settings.radio_2g.ssid) + ") is now OFF!" else: to_return = "\nERROR! Something went wrong... :(" modem.logout() lock.release() return to_return
def modem_setup(host, passwd): modem = Compal(host, passwd) modem.login() guest = WifiGuestNetworkSettings(modem) settings = guest.wifi_guest_network_settings # Change enabling state of the 3rd (index 2) 2g-interface (the one also editable via the UI) interface_index = 2 old_enabling_state = settings.guest_networks_2g[interface_index].enable pprint.pprint('Current GUEST-NETWORK state: ' + ('ON' if old_enabling_state == 1 else 'OFF')) settings.guest_networks_2g[ interface_index].enable = 1 if old_enabling_state == 2 else 2 guest.update_interface_guest_network_settings(settings, interface_index) new_enabling_state = guest.wifi_guest_network_settings.guest_networks_2g[ interface_index].enable pprint.pprint('New GUEST-NETWORK state: ' + ('ON' if new_enabling_state == 1 else 'OFF')) pprint.pprint(guest.wifi_guest_network_settings) modem.logout()
def toggle_wifi(ip, password): modem = Compal(ip, password) modem.login() # And/or change wifi settings wifi = WifiSettings(modem) settings = wifi.wifi_settings if settings.radio_2g.bss_enable == 2: # ENABLE 2.4Ghz settings.radio_2g.bss_enable = 1 else: # DISABLE 2.4Ghz settings.radio_2g.bss_enable = 2 wifi.update_wifi_settings(settings) wifi = WifiSettings(modem) settings = wifi.wifi_settings pprint.pprint(settings) modem.logout() if settings.radio_2g.bss_enable == 1: return "WIFI ("+ str(wifi.wifi_settings.radio_2g.ssid) + ") is now ON!" elif settings.radio_2g.bss_enable == 2: return "WIFI ("+ str(wifi.wifi_settings.radio_2g.ssid) + ") is now OFF!" else: return "ERROR! Something went wrong... :("
def wifi_enabled(ip, password): modem = Compal(ip, password) modem.login() wifi = WifiSettings(modem) enabled = wifi.wifi_settings.radio_2g.bss_enable == 1 modem.logout() return enabled
def modem_setup(host, passwd): modem = Compal(host, passwd) modem.login() guest = WifiGuestNetworkSettings(modem) settings = guest.wifi_guest_network_settings old_enabling_state = settings.enabling_2g.enabled pprint.pprint('Current GUEST-NETWORK state: ' + ('ON' if old_enabling_state else 'OFF')) guest.update_wifi_guest_network_settings(settings.properties, not old_enabling_state) settings = guest.wifi_guest_network_settings new_enabling_state = settings.enabling_2g.enabled pprint.pprint('New GUEST-NETWORK state: ' + ('ON' if new_enabling_state else 'OFF')) pprint.pprint(settings) modem.logout()
def test_successful_response(): load_login_responses("router") router = Compal("router", "1234") router.login()
def modem_setup(host, passwd, wifi_passwd, factory_reset=False): print("Attempting connection to %s with password: %s" % (host, passwd)) try: modem = Compal(host, passwd) modem.login() except Exception as err: print("Login to modem failed! Error: %s" % err) return if factory_reset: # Factory reset print("Performing factory reset...") modem.factory_reset() print("Sleeping for 5 minutes") time.sleep(300) # New connection + login again print("Logging in again...") modem = Compal(host, passwd) modem.login() # And/or change wifi settings wifi = WifiSettings(modem) settings = wifi.wifi_settings if wifi_passwd: settings.radio_2g.ssid = "modem_setup-2.4" settings.radio_2g.mode = False settings.radio_2g.security = 8 # 20/40MHz settings.radio_2g.bandwidth = 2 settings.radio_5g.ssid = "modem_setup-5" settings.radio_5g.mode = False settings.radio_5g.security = 8 settings.radio_2g.pre_shared_key = wifi_passwd settings.radio_5g.pre_shared_key = wifi_passwd wifi.update_wifi_settings(settings) dhcp = DHCPSettings(modem) dhcp.add_static_lease("192.168.178.17", "d0:50:99:0a:65:52") dhcp.add_static_lease("192.168.178.16", "BC:5F:F4:FE:05:15") dhcp.set_upnp_status(False) fw = PortForwards(modem) # Disable the firewall fw.update_firewall(enabled=True) # Delete all old rules rules = list(fw.rules) for rule in rules: rule.delete = True fw.update_rules(rules) # Create the new forwards fw.add_forward("192.168.178.17", 80, 80, Proto.tcp) fw.add_forward("192.168.178.17", 1022, 22, Proto.tcp) fw.add_forward("192.168.178.17", 443, 443, Proto.tcp) fw.add_forward("192.168.178.17", 32400, 32400, Proto.tcp) modem.logout()
def modem_setup(host, passwd, wifi_passwd, factory_reset=False): print("Attempting connection to %s with password: %s" % (host, passwd)) try: modem = Compal(host, passwd) modem.login() except Exception as err: print("Login to modem failed! Error: %s" % err) return if factory_reset: # Factory reset print("Performing factory reset...") modem.factory_reset() print("Sleeping for 5 minutes") time.sleep(300) # New connection + login again print("Logging in again...") modem = Compal(host, passwd) modem.login() # And/or change wifi settings wifi = WifiSettings(modem) settings = wifi.wifi_settings if wifi_passwd: settings.radio_2g.ssid = 'modem_setup-2.4' settings.radio_2g.mode = False settings.radio_2g.security = 8 # 20/40MHz settings.radio_2g.bandwidth = 2 settings.radio_5g.ssid = 'modem_setup-5' settings.radio_5g.mode = False settings.radio_5g.security = 8 settings.radio_2g.pre_shared_key = wifi_passwd settings.radio_5g.pre_shared_key = wifi_passwd wifi.update_wifi_settings(settings) dhcp = DHCPSettings(modem) dhcp.add_static_lease('192.168.178.17', 'd0:50:99:0a:65:52') dhcp.add_static_lease('192.168.178.16', 'BC:5F:F4:FE:05:15') dhcp.set_upnp_status(False) fw = PortForwards(modem) # Disable the firewall fw.update_firewall(enabled=True) # Delete all old rules rules = list(fw.rules) for rule in rules: rule.delete = True fw.update_rules(rules) # Create the new forwards fw.add_forward('192.168.178.17', 80, 80, Proto.tcp) fw.add_forward('192.168.178.17', 1022, 22, Proto.tcp) fw.add_forward('192.168.178.17', 443, 443, Proto.tcp) fw.add_forward('192.168.178.17', 32400, 32400, Proto.tcp) modem.logout()
def status(host, password): modem = Compal(host, password) modem.login() parser = etree.XMLParser(recover=True) global_settings = etree.fromstring(modem.xml_getter(GetFunction.GLOBALSETTINGS, {}).content, parser=parser) system_info = etree.fromstring(modem.xml_getter(GetFunction.CM_SYSTEM_INFO, {}).content, parser=parser) basic_status = etree.fromstring(modem.xml_getter(GetFunction.WIRELESSBASIC_2, {}).content, parser=parser) status_2 = etree.fromstring(modem.xml_getter(GetFunction.STATUS_2, {}).content, parser=parser) modem_status = { "model": global_settings.find("ConfigVenderModel").text, "hw_version": system_info.find("cm_hardware_version").text, "sw_version": global_settings.find("SwVersion").text, "cm_serial_number": system_info.find("cm_serial_number").text, "cm_mac_addr": system_info.find("cm_mac_addr").text, "operator_id": global_settings.find("OperatorId").text, "network_mode": global_settings.find("GwProvisionMode").text, "status": basic_status.find("cm_status").text.lower(), "uptime": system_info.find("cm_system_uptime").text } wifi_settings = WifiSettings(modem).wifi_settings wifi_band_settings = [wifi_settings.radio_2g, wifi_settings.radio_5g] wifi_status = [] for wifi_band in wifi_band_settings: wifi_band = { "radio": wifi_band.radio, "enabled": wifi_band.bss_enable == 1, "ssid": wifi_band.ssid, "hidden": wifi_band.hidden == 1 } wifi_status.append(wifi_band) wifi_guest_network_settings = WifiGuestNetworkSettings(modem).wifi_guest_network_settings wifi_guest_status = [ { "radio": "2g", "enabled": wifi_guest_network_settings.enabling_2g.enabled, "mac": wifi_guest_network_settings.enabling_2g.guest_mac, "ssid": wifi_guest_network_settings.properties.ssid, "hidden": wifi_guest_network_settings.properties.hidden == 1 }, { "radio": "5g", "enabled": wifi_guest_network_settings.enabling_5g.enabled, "mac": wifi_guest_network_settings.enabling_5g.guest_mac, "ssid": wifi_guest_network_settings.properties.ssid, "hidden": wifi_guest_network_settings.properties.hidden == 1 }, ] telephone_lines = list(status_2.iter("Line")) telephone_line = [ Commands.__create_telephone_line(telephone_lines[0]), Commands.__create_telephone_line(telephone_lines[1]), ] modem.logout() return { "modem": modem_status, "wifi": wifi_status, "wifi_guest": wifi_guest_status, "telephone_line": telephone_line, }
def reboot(host, password): modem = Compal(host, password) modem.login() modem.reboot()