From b193e29fe5172c00a040bdc350ae4ec74baaff00 Mon Sep 17 00:00:00 2001 From: mistersmie Date: Tue, 19 Mar 2019 21:54:27 +0100 Subject: [PATCH] add optional fritzbox_username environment variable to do username/password authentication this implementation should also work seamlessly with "master-password" style authentication. --- fritzbox_cpu_temperature.py | 7 ++++++- fritzbox_cpu_usage.py | 7 ++++++- fritzbox_helper.py | 10 +++++++--- fritzbox_memory_usage.py | 7 ++++++- fritzbox_power_consumption.py | 7 ++++++- fritzbox_uptime.py | 7 ++++++- fritzbox_wifi_devices.py | 7 ++++++- 7 files changed, 43 insertions(+), 9 deletions(-) diff --git a/fritzbox_cpu_temperature.py b/fritzbox_cpu_temperature.py index 4764ad4..08c5d31 100755 --- a/fritzbox_cpu_temperature.py +++ b/fritzbox_cpu_temperature.py @@ -10,6 +10,7 @@ [fritzbox_*] env.fritzbox_ip [ip address of the fritzbox] env.fritzbox_password [fritzbox password] + env.fritzbox_username [optional: fritzbox username] This plugin supports the following munin configuration parameters: #%# family=auto contrib @@ -31,7 +32,11 @@ def get_cpu_temperature(): server = os.environ['fritzbox_ip'] password = os.environ['fritzbox_password'] - session_id = fh.get_session_id(server, password) + if "fritzbox_username" in os.environ: + fritzuser = os.environ['fritzbox_username'] + session_id = fh.get_session_id(server, password, fritzuser) + else: + session_id = fh.get_session_id(server, password) data = fh.get_page_content(server, session_id, PAGE) m = re.search(pattern, data) diff --git a/fritzbox_cpu_usage.py b/fritzbox_cpu_usage.py index c5c0f80..e19d4fe 100755 --- a/fritzbox_cpu_usage.py +++ b/fritzbox_cpu_usage.py @@ -10,6 +10,7 @@ [fritzbox_*] env.fritzbox_ip [ip address of the fritzbox] env.fritzbox_password [fritzbox password] + env.fritzbox_username [optional: fritzbox username] This plugin supports the following munin configuration parameters: #%# family=auto contrib @@ -31,7 +32,11 @@ def get_cpu_usage(): server = os.environ['fritzbox_ip'] password = os.environ['fritzbox_password'] - session_id = fh.get_session_id(server, password) + if "fritzbox_username" in os.environ: + fritzuser = os.environ['fritzbox_username'] + session_id = fh.get_session_id(server, password, fritzuser) + else: + session_id = fh.get_session_id(server, password) data = fh.get_page_content(server, session_id, PAGE) m = re.search(pattern, data) diff --git a/fritzbox_helper.py b/fritzbox_helper.py index b1110bb..5d7bcf1 100755 --- a/fritzbox_helper.py +++ b/fritzbox_helper.py @@ -10,6 +10,7 @@ [fritzbox_*] env.fritzbox_ip [ip address of the fritzbox] env.fritzbox_password [fritzbox password] + env.fritzbox_username [optional: fritzbox username] This plugin supports the following munin configuration parameters: #%# family=auto contrib @@ -29,22 +30,25 @@ from lxml import etree USER_AGENT = "Mozilla/5.0 (Macintosh; Intel Mac OS X x.y; rv:10.0) Gecko/20100101 Firefox/10.0" -def get_session_id(server, password, port=80): +def get_session_id(server, password, username=None, port=80): """Obtains the session id after login into the Fritzbox. See https://avm.de/fileadmin/user_upload/Global/Service/Schnittstellen/AVM_Technical_Note_-_Session_ID.pdf for deteils (in German). :param server: the ip address of the Fritzbox :param password: the password to log into the Fritzbox webinterface + :param username: (optional) the username with which to log into the Fritzbox webinterface :param port: the port the Fritzbox webserver runs on :return: the session id """ + userpar = '' if username is None else '?username={}'.format(username) + headers = {"Accept": "application/xml", "Content-Type": "text/plain", "User-Agent": USER_AGENT} - url = 'http://{}:{}/login_sid.lua'.format(server, port) + url = 'http://{}:{}/login_sid.lua{}'.format(server, port, userpar) try: r = requests.get(url, headers=headers) r.raise_for_status() @@ -67,7 +71,7 @@ def get_session_id(server, password, port=80): "Content-Type": "application/x-www-form-urlencoded", "User-Agent": USER_AGENT} - url = 'http://{}:{}/login_sid.lua?&response={}'.format(server, port, response_bf) + url = 'http://{}:{}/login_sid.lua?{}&response={}'.format(server, port, userpar[1:], response_bf) try: r = requests.get(url, headers=headers) r.raise_for_status() diff --git a/fritzbox_memory_usage.py b/fritzbox_memory_usage.py index e1cdb7c..a4b616f 100755 --- a/fritzbox_memory_usage.py +++ b/fritzbox_memory_usage.py @@ -10,6 +10,7 @@ [fritzbox_*] env.fritzbox_ip [ip address of the fritzbox] env.fritzbox_password [fritzbox password] + env.fritzbox_username [optional: fritzbox username] This plugin supports the following munin configuration parameters: #%# family=auto contrib @@ -32,7 +33,11 @@ def get_memory_usage(): server = os.environ['fritzbox_ip'] password = os.environ['fritzbox_password'] - session_id = fh.get_session_id(server, password) + if "fritzbox_username" in os.environ: + fritzuser = os.environ['fritzbox_username'] + session_id = fh.get_session_id(server, password, fritzuser) + else: + session_id = fh.get_session_id(server, password) data = fh.get_page_content(server, session_id, PAGE) matches = re.finditer(pattern, data) if matches: diff --git a/fritzbox_power_consumption.py b/fritzbox_power_consumption.py index 38d09ee..fb5a418 100755 --- a/fritzbox_power_consumption.py +++ b/fritzbox_power_consumption.py @@ -10,6 +10,7 @@ [fritzbox_*] env.fritzbox_ip [ip address of the fritzbox] env.fritzbox_password [fritzbox password] + env.fritzbox_username [optional: fritzbox username] This plugin supports the following munin configuration parameters: #%# family=auto contrib @@ -32,7 +33,11 @@ def get_power_consumption(): server = os.environ['fritzbox_ip'] password = os.environ['fritzbox_password'] - session_id = fh.get_session_id(server, password) + if "fritzbox_username" in os.environ: + fritzuser = os.environ['fritzbox_username'] + session_id = fh.get_session_id(server, password, fritzuser) + else: + session_id = fh.get_session_id(server, password) data = fh.get_page_content(server, session_id, PAGE) matches = re.finditer(pattern, data) if matches: diff --git a/fritzbox_uptime.py b/fritzbox_uptime.py index c08c898..a9e50b3 100755 --- a/fritzbox_uptime.py +++ b/fritzbox_uptime.py @@ -10,6 +10,7 @@ [fritzbox_*] env.fritzbox_ip [ip address of the fritzbox] env.fritzbox_password [fritzbox password] + env.fritzbox_username [optional: fritzbox username] This plugin supports the following munin configuration parameters: #%# family=auto contrib @@ -39,7 +40,11 @@ def get_uptime(): server = os.environ['fritzbox_ip'] password = os.environ['fritzbox_password'] - session_id = fh.get_session_id(server, password) + if "fritzbox_username" in os.environ: + fritzuser = os.environ['fritzbox_username'] + session_id = fh.get_session_id(server, password, fritzuser) + else: + session_id = fh.get_session_id(server, password) data = fh.get_page_content(server, session_id, PAGE) matches = re.finditer(pattern, data) if matches: diff --git a/fritzbox_wifi_devices.py b/fritzbox_wifi_devices.py index b2408da..f303a39 100755 --- a/fritzbox_wifi_devices.py +++ b/fritzbox_wifi_devices.py @@ -10,6 +10,7 @@ [fritzbox_*] env.fritzbox_ip [ip address of the fritzbox] env.fritzbox_password [fritzbox password] + env.fritzbox_username [optional: fritzbox username] This plugin supports the following munin configuration parameters: #%# family=auto contrib @@ -35,7 +36,11 @@ def get_connected_wifi_devices(): server = os.environ['fritzbox_ip'] password = os.environ['fritzbox_password'] - session_id = fh.get_session_id(server, password) + if "fritzbox_username" in os.environ: + fritzuser = os.environ['fritzbox_username'] + session_id = fh.get_session_id(server, password, fritzuser) + else: + session_id = fh.get_session_id(server, password) data = fh.get_page_content(server, session_id, PAGE) m = re.search(pattern, data) if m: