從iLO獲取服務器信息

  • 128
  • 0
  • 2016-07-08

從iLO獲取服務器信息

Python實現。主要通過Python的Requests庫,從iLO的url接口/json/overview中get數據。

在獲取數據前需login以建立Session object;獲取數據后需logout以斷開iLO Active Sessions,防止堵塞iLO;同時get數據的頻率不能過高

 

import requests
import json
import warnings

warnings.simplefilter("ignore")

loginurl = 'https://serveriloip/json/login_session'
overviewurl = 'https://serveriloip/json/overview'

#create session object
s = requests.Session()

try:
    #login iLO
    payload = {'method': 'login', 'user_login': 'user', 'password': 'password'}
    s.post(loginurl, json=payload, verify=False)

    #get overview info
    r = s.get(overviewurl)
    data = json.loads(r.text)

    #get sessionkey and logout
    sdict = requests.utils.dict_from_cookiejar(s.cookies)
    slogout = {'method': 'logout', 'session_key': sdict['sessionKey']}
    s.post(loginurl, json=slogout)

    if 'system_health' in data and data['system_health'] == 'OP_STATUS_OK':
        print 0
    else:
        print 1
        
except Exception, e:
    print 1