2016-03-08 172 views

回答

1

我使用Python下Zenoss的組查詢設備。 Key正在發送data ='{「action」:「DeviceRouter」,「method」:「getDevices」,「data」的請求:[{「uid」:「/ zport/dmd/Devices /」}]「tid 「:1}「)

import requests 
def getZenossDeviceList(zenossURI, username, password, data): 
    try: 
      s = requests.Session() 
      s.auth = (username, password) 
      s.headers["Content-Type"] = "application/json" 
      r = s.post(zenossURI, data=data, timeout=(3.05, 30)); 
    except Exception as ex: 
      raise Exception("getZenossDeviceList: {0}",ex) 
    else: 
      if r.status_code == requests.codes.ok: 
        return r.json() 
      else: 
        raise Exception("getZenossDeviceList: {0}", r.reason) 


def getServerList(username,password): 
     hostList = [] 
     zenossURI="https://<ZENOSSS_SERVER_IP>/zport/dmd/device_router" 

    try: 
      r = getZenossDeviceList(zenossURI, username, password,\ 
        data='{"action":"DeviceRouter", "method":"getDevices","data":[{"uid":"/zport/dmd/Devices/<AbsolutePath_for_Groupname>"}],"tid":1}') 
      print r 
    except Exception as e: 
      print "Exception", e