1
0
mirror of https://github.com/Tafkas/fritzbox-munin.git synced 2023-10-10 13:36:55 +02:00
fritzbox-munin/fritzbox_helper.py

94 lines
3.1 KiB
Python
Raw Normal View History

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
"""
import hashlib
import httplib
import re
import sys
from xml.dom import minidom
USER_AGENT = "Mozilla/5.0 (U; Windows NT 5.1; rv:5.0) Gecko/20100101 Firefox/5.0"
2015-06-05 01:07:22 +02:00
def get_sid(server, password, port=80):
2015-06-04 22:08:29 +02:00
"""Obtains the sid after login into the fritzbox"""
2015-06-05 01:07:22 +02:00
conn = httplib.HTTPConnection(server + ':' + str(port))
2015-06-04 22:08:29 +02:00
2015-06-05 01:07:22 +02:00
headers = {"Accept": "application/xml",
"Content-Type": "text/plain",
"User-Agent": USER_AGENT}
2015-06-04 22:08:29 +02:00
2015-06-05 01:07:22 +02:00
initial_page = '/login_sid.lua'
conn.request("GET", initial_page, '', headers)
2015-06-04 22:08:29 +02:00
response = conn.getresponse()
data = response.read()
if response.status != 200:
print "%s %s" % (response.status, response.reason)
sys.exit(0)
else:
2015-06-05 01:07:22 +02:00
xml_data = minidom.parseString(data)
sid_info = xml_data.getElementsByTagName('SID')
sid = sid_info[0].firstChild.data
2015-06-04 22:08:29 +02:00
if sid == "0000000000000000":
2015-06-05 01:07:22 +02:00
challenge_info = xml_data.getElementsByTagName('Challenge')
challenge = challenge_info[0].firstChild.data
2015-06-04 22:08:29 +02:00
challenge_bf = (challenge + '-' + password).decode('iso-8859-1').encode('utf-16le')
m = hashlib.md5()
m.update(challenge_bf)
response_bf = challenge + '-' + m.hexdigest().lower()
else:
return sid
2015-06-05 01:07:22 +02:00
headers = {"Accept": "text/html,application/xhtml+xml,application/xml",
"Content-Type": "application/x-www-form-urlencoded",
"User-Agent": USER_AGENT}
2015-06-04 22:08:29 +02:00
2015-06-05 01:07:22 +02:00
login_page = "/login_sid.lua?&response=" + response_bf
conn.request("GET", login_page, '', headers)
2015-06-04 22:08:29 +02:00
response = conn.getresponse()
data = response.read()
if response.status != 200:
print "%s %s" % (response.status, response.reason)
sys.exit(0)
else:
2015-06-05 01:07:22 +02:00
sid = re.search("<SID>(.*?)</SID>", data).group(1)
2015-06-04 22:08:29 +02:00
if sid == "0000000000000000":
print "ERROR - No SID received because of invalid password"
sys.exit(0)
return sid
2015-06-05 01:07:22 +02:00
2015-06-04 22:08:29 +02:00
def get_page(server, sid, page, port=80):
"""Fetches a page from the Fritzbox and returns its content"""
2015-06-05 01:07:22 +02:00
conn = httplib.HTTPConnection(server + ':' + str(port))
2015-06-04 22:08:29 +02:00
2015-06-05 01:07:22 +02:00
headers = {"Accept": "application/xml",
"Content-Type": "text/plain",
"User-Agent": USER_AGENT}
2015-06-04 22:08:29 +02:00
2015-06-05 01:07:22 +02:00
page_with_sid = page + "?sid=" + sid
conn.request("GET", page_with_sid, '', headers)
2015-06-04 22:08:29 +02:00
response = conn.getresponse()
data = response.read()
if response.status != 200:
print "%s %s" % (response.status, response.reason)
print data
sys.exit(0)
else:
2015-06-05 01:07:22 +02:00
return data