2015-06-04 22:08:29 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
"""
|
|
|
|
fritzbox_helper - A munin plugin for Linux to monitor AVM Fritzbox
|
|
|
|
Copyright (C) 2015 Christian Stade-Schuldt
|
|
|
|
Author: Christian Stade-Schuldt
|
|
|
|
Like Munin, this plugin is licensed under the GNU GPL v2 license
|
|
|
|
http://www.opensource.org/licenses/GPL-2.0
|
2015-06-04 22:28:25 +02:00
|
|
|
Add the following section to your munin-node's plugin configuration:
|
2015-06-04 22:08:29 +02:00
|
|
|
|
|
|
|
[fritzbox_*]
|
|
|
|
env.fritzbox_ip [ip address of the fritzbox]
|
2015-06-04 22:28:25 +02:00
|
|
|
env.fritzbox_password [fritzbox password]
|
|
|
|
|
2015-06-04 22:08:29 +02:00
|
|
|
This plugin supports the following munin configuration parameters:
|
|
|
|
#%# family=auto contrib
|
|
|
|
#%# capabilities=autoconf
|
2017-11-04 17:04:00 +01:00
|
|
|
|
|
|
|
The initial script was inspired by
|
|
|
|
https://www.linux-tips-and-tricks.de/en/programming/389-read-data-from-a-fritzbox-7390-with-python-and-bash
|
|
|
|
framp at linux-tips-and-tricks dot de
|
2015-06-04 22:08:29 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
import hashlib
|
|
|
|
import sys
|
|
|
|
|
2017-11-04 17:04:00 +01:00
|
|
|
import requests
|
|
|
|
from lxml import etree
|
|
|
|
|
2015-06-04 22:08:29 +02:00
|
|
|
|
2017-11-04 17:04:00 +01:00
|
|
|
def get_session_id(server, password, 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).
|
2015-06-05 01:07:22 +02:00
|
|
|
|
2017-11-04 17:04:00 +01:00
|
|
|
:param server: the ip address of the Fritzbox
|
|
|
|
:param password: the password to log into the Fritzbox webinterface
|
|
|
|
:param port: the port the Fritzbox webserver runs on
|
|
|
|
:return: the session id
|
|
|
|
"""
|
2015-06-04 22:08:29 +02:00
|
|
|
|
2015-06-05 01:07:22 +02:00
|
|
|
headers = {"Accept": "application/xml",
|
2020-07-15 13:22:22 +02:00
|
|
|
"Content-Type": "text/plain"}
|
2015-06-04 22:08:29 +02:00
|
|
|
|
2017-11-04 17:04:00 +01:00
|
|
|
url = 'http://{}:{}/login_sid.lua'.format(server, port)
|
|
|
|
try:
|
|
|
|
r = requests.get(url, headers=headers)
|
|
|
|
r.raise_for_status()
|
|
|
|
except requests.exceptions.HTTPError as err:
|
|
|
|
print(err)
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
root = etree.fromstring(r.content)
|
|
|
|
session_id = root.xpath('//SessionInfo/SID/text()')[0]
|
|
|
|
if session_id == "0000000000000000":
|
|
|
|
challenge = root.xpath('//SessionInfo/Challenge/text()')[0]
|
2019-04-09 09:47:49 +02:00
|
|
|
challenge_bf = ('{}-{}'.format(challenge, password)).encode('utf-16le')
|
2017-11-04 17:04:00 +01:00
|
|
|
m = hashlib.md5()
|
|
|
|
m.update(challenge_bf)
|
|
|
|
response_bf = '{}-{}'.format(challenge, m.hexdigest().lower())
|
2015-06-04 22:08:29 +02:00
|
|
|
else:
|
2017-11-04 17:04:00 +01:00
|
|
|
return session_id
|
2015-06-04 22:08:29 +02:00
|
|
|
|
2015-06-05 01:07:22 +02:00
|
|
|
headers = {"Accept": "text/html,application/xhtml+xml,application/xml",
|
2020-07-15 13:22:22 +02:00
|
|
|
"Content-Type": "application/x-www-form-urlencoded"}
|
2015-06-04 22:08:29 +02:00
|
|
|
|
2017-11-04 17:04:00 +01:00
|
|
|
url = 'http://{}:{}/login_sid.lua?&response={}'.format(server, port, response_bf)
|
|
|
|
try:
|
|
|
|
r = requests.get(url, headers=headers)
|
|
|
|
r.raise_for_status()
|
|
|
|
except requests.exceptions.HTTPError as err:
|
|
|
|
print(err)
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
root = etree.fromstring(r.content)
|
|
|
|
session_id = root.xpath('//SessionInfo/SID/text()')[0]
|
|
|
|
if session_id == "0000000000000000":
|
|
|
|
print("ERROR - No SID received because of invalid password")
|
2015-06-04 22:08:29 +02:00
|
|
|
sys.exit(0)
|
2017-11-04 17:04:00 +01:00
|
|
|
return session_id
|
|
|
|
|
2015-06-04 22:08:29 +02:00
|
|
|
|
2017-11-04 17:04:00 +01:00
|
|
|
def get_page_content(server, session_id, page, port=80):
|
|
|
|
"""Fetches a page from the Fritzbox and returns its content
|
2015-06-05 01:07:22 +02:00
|
|
|
|
2017-11-04 17:04:00 +01:00
|
|
|
:param server: the ip address of the Fritzbox
|
|
|
|
:param session_id: a valid session id
|
|
|
|
:param page: the page you are regquesting
|
|
|
|
:param port: the port the Fritzbox webserver runs on
|
|
|
|
:return: the content of the page
|
|
|
|
"""
|
2015-06-04 22:08:29 +02:00
|
|
|
|
2015-06-05 01:07:22 +02:00
|
|
|
headers = {"Accept": "application/xml",
|
2020-07-15 13:22:22 +02:00
|
|
|
"Content-Type": "text/plain"}
|
2015-06-04 22:08:29 +02:00
|
|
|
|
2017-11-04 17:04:00 +01:00
|
|
|
url = 'http://{}:{}/{}?sid={}'.format(server, port, page, session_id)
|
|
|
|
try:
|
|
|
|
r = requests.get(url, headers=headers)
|
|
|
|
r.raise_for_status()
|
|
|
|
except requests.exceptions.HTTPError as err:
|
|
|
|
print(err)
|
|
|
|
sys.exit(1)
|
|
|
|
return r.content
|
2019-04-09 09:47:49 +02:00
|
|
|
|
|
|
|
|
|
|
|
def get_xhr_content(server, session_id, page, port=80):
|
|
|
|
"""Fetches the xhr content from the Fritzbox and returns its content
|
|
|
|
|
|
|
|
:param server: the ip address of the Fritzbox
|
|
|
|
:param session_id: a valid session id
|
|
|
|
:param page: the page you are regquesting
|
|
|
|
:param port: the port the Fritzbox webserver runs on
|
|
|
|
:return: the content of the page
|
|
|
|
"""
|
|
|
|
|
|
|
|
headers = {"Accept": "application/xml",
|
2020-07-15 13:22:22 +02:00
|
|
|
"Content-Type": "application/x-www-form-urlencoded"}
|
2019-04-09 09:47:49 +02:00
|
|
|
|
|
|
|
url = 'http://{}:{}/data.lua'.format(server, port)
|
|
|
|
data = {"xhr": 1,
|
|
|
|
"sid": session_id,
|
|
|
|
"lang": "en",
|
|
|
|
"page": page,
|
|
|
|
"xhrId": "all",
|
|
|
|
"no_sidrenew": ""
|
|
|
|
}
|
|
|
|
try:
|
|
|
|
r = requests.post(url, data=data, headers=headers)
|
|
|
|
except requests.exceptions.HTTPError as err:
|
|
|
|
print(err)
|
|
|
|
sys.exit(1)
|
|
|
|
return r.content
|