SHARE
TWEET

fritzbox.py

a guest May 31st, 2017 124 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2.  
  3. #### Installation #################################
  4. #
  5. # apt-get install python-pip
  6. # pip install request
  7. # pip install lxml
  8. # # if lxml failes, try   "apt-get install python-lxml"
  9. # pip install fritzconnection
  10. #
  11. ###################################################
  12.  
  13. import sys
  14.  
  15.  
  16. class FritzDevice(object):
  17.  
  18.     fc = None
  19.     ip = None
  20.     password = None
  21.  
  22.     def __init__(self, ip, password):
  23.         from fritzconnection import FritzConnection
  24.         self.fc = FritzConnection(address=ip, password=password)
  25.         self.ip = ip
  26.         self.password = password
  27.  
  28.     def get_data(self, service, action):
  29.         return self.fc.call_action(service, action)
  30.  
  31.     @property
  32.     def services(self):
  33.         import fritzconnection as _fc
  34.         return _fc.print_api(address=self.ip, password=self.password)
  35.  
  36.  
  37. class Information(FritzDevice):
  38.  
  39.     def fetch(self, service, action, keys=[]):
  40.         data = ""
  41.  
  42.         if not isinstance(action, (list, tuple)):
  43.             action = [action]
  44.  
  45.         for a in action:
  46.             ret = self.get_data(service, a)
  47.  
  48.             if keys:
  49.                 ks = keys
  50.             else:
  51.                 ks = ret.keys()
  52.  
  53.             for key in ks:
  54.                 try:
  55.                     data += "%s:%s " % (key, ret[key])
  56.                 except KeyError:
  57.                     continue
  58.  
  59.         return data
  60.  
  61.     @property
  62.     def ext_ip_address(self):
  63.         # external IP Address
  64.         service = 'WANPPPConnection'
  65.         action = 'GetExternalIPAddress'
  66.  
  67.         keys = [
  68.         'NewExternalIPAddress',
  69.         ]
  70.  
  71.         return self.fetch(service, action, keys)
  72.  
  73.     @property
  74.     def dsl_information(self):
  75.         # DSL Information
  76.         service = 'WANDSLInterfaceConfig'
  77.         action = 'GetInfo'
  78.         keys = [
  79.         'NewUpstreamAttenuation',
  80.         'NewUpstreamPower',
  81.         'NewDownstreamCurrRate',
  82.         'NewDownstreamMaxRate',
  83.         'NewUpstreamNoiseMargin',
  84.         'NewDownstreamPower',
  85.         'NewUpstreamMaxRate',
  86.         'NewDownstreamNoiseMargin',
  87.         'NewDownstreamAttenuation',
  88.         'NewUpstreamCurrRate',
  89.         ]
  90.  
  91.         return self.fetch(service, action, keys)
  92.  
  93.     @property
  94.     def wan_information(self):
  95.         # WAN Information
  96.         service = 'WANCommonInterfaceConfig'
  97.         action = [
  98.           'GetTotalBytesSent',
  99.           'GetTotalBytesReceived',
  100.           'GetTotalPacketsSent',
  101.           'GetTotalPacketsReceived',
  102.         ]
  103.  
  104.         return self.fetch(service, action)
  105.  
  106.  
  107. def run():
  108.  
  109.     arguments = sys.argv
  110.     arguments.pop(0)
  111.     ip, password, arg = arguments
  112.  
  113.     fc = Information(ip, password)
  114.  
  115.     if arg == "api_description":
  116.         ret = fc.services
  117.     elif arg == "dslinfo":
  118.         ret = fc.dsl_information
  119.     elif arg == "traffic":
  120.         ret = fc.wan_information
  121.     elif arg == 'ext_ip_address':
  122.         ret = fc.ext_ip_address
  123.     else:
  124.         ret = "unknown Argument"
  125.  
  126.     return ret
  127.  
  128.  
  129. if __name__ == '__main__':
  130.     output = run()
  131.     print(output)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top