def instantiate_fwlans(self): """ FritzWLAN instances should only be instantiated once to improve performance """ for address in self.fritz_addresses: if len(address) > 2: self.fwlans.append(FritzWLAN(address=address[0], user=address[2], password=address[1])) else: self.fwlans.append(FritzWLAN(address=address[0], password=address[1]))
def track_devices(tracking_duration, tracking_period): # instanciate FritzWLAN just once for reusage fwlan = FritzWLAN(address=ADDRESS, password=PASSWORD) stop = datetime.datetime.now() + tracking_duration while datetime.datetime.now() < stop: report_devices(fwlan) time.sleep(tracking_period)
class Fritzbox: def __init__(self, ipAddress, password): # instanciate FritzWLAN just once for reusage self.__fwlan = FritzWLAN(address=ipAddress, password=password) def getActiveMacs(self): """ Gets a FritzWLAN instance and returns a list of mac addresses from the active devices """ active_macs = list() # iterate over all wlans: for n in itertools.count(1): self.__fwlan.service = n try: hostsInfo = self.__fwlan.get_hosts_info() except FritzServiceError: break else: active_macs.extend(entry['mac'] for entry in hostsInfo) return active_macs def getNumberActiveMacs(self, macs): activeMacs = self.getActiveMacs() numResidentsAtHome = 0 # decide here what to do with this information: for mac in activeMacs: for macResident in macs: if mac == macResident: numResidentsAtHome = numResidentsAtHome + 1 # return number of macs = residents at home return numResidentsAtHome
def get_connected_wifi_devices(): """gets the numbrer of currently connected wifi devices""" try: conn = FritzWLAN(address=hostname, password=os.environ['fritzbox_password']) except Exception as e: print(e) sys.exit("Couldn't get connection uptime") connected_devices = conn.host_number print('wifi.value %d' % connected_devices)
def _poll_loop(self): self._current_data = FritzBoxData() last_hosts: Optional[datetime] = None while self._running: try: self._current_data.status = FritzStatus(address=self._address, password=self._password) if not last_hosts or (datetime.now() - last_hosts) / timedelta(seconds=10) >= 1: self.current_data.hosts = FritzHosts(address=self._address, password=self._password) self.current_data.wlan = FritzWLAN(address=self._address, password=self._password) last_hosts = datetime.now() except IOError as e: log.warning(f"Failed to get FritzBox data: {e}") self._notify_listeners() self._queue.get()
# Argument Parser erzeugen parser = argparse.ArgumentParser(description="Simple Fritz!Box switching.") parser.add_argument("service", help="Service Name", choices=services) parser.add_argument("-d", "--device", help="Service SmartHome: device name", choices=geraete, default=None) parser.add_argument("-s", "--switch", help="Switching operation", choices=schalter, default="toggle") args = parser.parse_args() # API connection fc = FritzConnection(address=fritz_address, user=fritz_user, password=fritz_password) if args.service == "WirelessGuestAccess": # Servicenummer des Gäste-WLAN finden print("Searching for wireless guest access with SSID", guest_ssid) fwlan = FritzWLAN(address=fritz_address, user=fritz_user, password=fritz_password) service = find_guest_wlan(fwlan, guest_ssid) if service is None: print("FATAL: no wireless guest access found") exit(1) fwlan.service = service # aktuellen Zustand lesen old_enable = fc.call_action(service_name="WLANConfiguration" + str(fwlan.service), action_name="GetInfo")["NewEnable"] # neuen Zustand bestimmen if args.switch == "on": new_enable = True elif args.switch == "off": new_enable = False else: new_enable = not old_enable
user_change_message_id = {} with open("conf", "r") as config: key = config.readline().split(":", 1)[-1].strip() allowed_users = [ int(x) for x in config.readline().split(':', 1)[-1].strip().split(',') if len(x) > 0 ] router_ip = config.readline().split(':', 1)[-1].strip() router_pw = config.readline().split(':', 1)[-1].strip() db_vals = config.readline().split(':', 1)[-1].strip() user, user_pw, location, port, database = db_vals.split(',') port = int(port) router_connection = FritzConnection(address=router_ip, password=router_pw) router_wlan = FritzWLAN(router_connection) router_host = FritzHosts(router_connection) router_status = FritzStatus(router_connection) router_model = router_status.modelname metadata = MetaData() db_engine = create_engine( f'mysql+pymysql://{user}:{user_pw}@{location}:{port}/{database}') metadata.reflect(db_engine, only=['stats']) Base = automap_base(metadata=metadata) Base.prepare() db_statistics_class = Base.classes.stats Session = sessionmaker(bind=db_engine) db_session = Session()
def __init__(self, ipAddress, password): # instanciate FritzWLAN just once for reusage self.__fwlan = FritzWLAN(address=ipAddress, password=password)