Module: check_mk
Branch: master
Commit: 978be5e94faea456164cbde23cbf214285421c42
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=978be5e94faea4…
Author: Bastian Kuhn <bk(a)mathias-kettner.de>
Date: Wed Oct 9 22:10:06 2013 +0200
agent_fritz: Agent now able to login
---
agents/special/agent_fritz | 52 ++++++++++++++++++++++++++++++--------------
1 file changed, 36 insertions(+), 16 deletions(-)
diff --git a/agents/special/agent_fritz b/agents/special/agent_fritz
index 8160b42..df86197 100755
--- a/agents/special/agent_fritz
+++ b/agents/special/agent_fritz
@@ -1,31 +1,51 @@
#!/usr/bin/python
+# encoding: utf-8
+
from lxml import etree
-import hashlib
+import hashlib, sys
import inspect, pprint
+import urllib
address = "http://fritz.box"
username = ''
-password = "test123"
+password = u"test123"
+sidfile = '/tmp/sid_cache'
+
+try:
+ """
+ Session IDs are valid for 10minutes.
+ Cause of the limited number of Session IDs for a fritzbox, why
+ have to try to persist thes IDs
+ """
+ sid = file(sidfile).read()
+except IOError:
+ sid = ""
-url = '%s/login_sid.lua' % address
-login_page = etree.parse(url).getroot()
+login_url = '%s/login_sid.lua' % address
+# Try to login with old session id
+login_page = etree.parse(login_url+"?sid="+sid).getroot()
sid = login_page.findtext('SID')
-block = login_page.findtext('BlockTime')
+# If login with old session id not work, try to get a new one
if sid == "0000000000000000":
challenge = login_page.findtext('Challenge')
- challenge = "123456z"
- password = ".abc"
- rep = challenge + '-' + password
- rep = rep.encode('utf-16le')
- print rep
+ rep = (challenge + '-' + password).encode('utf-16le')
response = challenge + '-' + hashlib.md5(rep).hexdigest()
+ login_page = etree.parse(login_url+"?username=%s&response=%s" %
(username, response)).getroot()
+ sid = login_page.findtext('SID')
+ #if still not possible to get an id, exit with error
+ if sid == "0000000000000000":
+ print "Canot get a session id. Please check passwort"
+ sys.exit(1)
+ file(sidfile,'w').write(sid)
- print response
+#Just testing yet:
+urls = [
+ "system/ecostat.lua",
+ "/net/home_auto_overview.lua"
+
+]
+
+print urllib.urlopen("%s/%s?sid=%s" % (address,urls[0], sid)).read()
- login_page = etree.parse(url+"?username=%s&response=%s" % (username,
response)).getroot()
- print url+"?username=%s&response=%s" % (username, response)
- sid = login_page.findtext('SID')
- block = login_page.findtext('BlockTime')
-print "SID: %s, Block: %s" % ( sid, block )