中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

H3C設備服務器采集參數認證過程(包含redfish和res

發布時間:2020-03-17 07:59:57 來源:網絡 閱讀:693 作者:chier11 欄目:系統運維

該腳本針對H3C服務器分別對redfish和restfull兩種協議的認證方式進行測試,并合并。
有三個類,分別是redfish協議測試、restfull協議測試、以及兩個合并測試
文章最后使用redfish模塊簡單進行認證訪問測試。


import requests
import json
requests.packages.urllib3.disable_warnings()

'''
以下有三個類,分別是redfish協議測試、restfull協議測試、以及兩個合并測試。
'''

##redfish認證過程和獲取參數
class redfish_getinfo(object):
    def __init__(self,ipaddr,username,password):
        self.ip=ipaddr.strip()
        self.URLprefix='https://'+ipaddr.strip()
        self.username=username.strip()
        self.password=password.strip()
        global token
        token=0
        tokenurl=self.URLprefix+'/redfish/v1/SessionService/Sessions'
        print(tokenurl)
        data={
            "UserName":self.username,
            "Password":self.password
            }
        header={
            "Content-Type":"application/json"
            }
        re1=requests.post(tokenurl,json.dumps(data),headers=header,verify=False)
        print (re1.status_code)
        if re1.status_code == 201:
            print ('redfish_info',re1.json())
            print ('redfish_header',re1.headers)
            temp_header = re1.headers
            token= temp_header['X-Auth-Token']
            print('redfish_token', token)
        else:
            pass
    def redfish_info(self,URL_suffix):  #定義總獲取函數,傳參url的后半部分。如'/redfish/v1/Systems/1/Memory'
        urlset=self.URLprefix+URL_suffix
        if token !=0:
            header = {
                "Content-Type":"application/json",
                "X-Auth-Token":token
                }
            re1=requests.get(urlset,headers=header,verify=False)
            #print(re1.status_code)
            return (re1.json())
        else:
            pass
##restfull認證過程和獲取參數
class restfull_info(object):
    def __init__(self,ipaddr,username,password):
        self.ip=ipaddr.strip()
        self.username=username
        self.password=password
        self.URLprefix='http://' + ipaddr.strip()
        global CSRFToken   ##同時存在4-5個token鏈接,每個token鏈接時間為5分鐘,可以自己設置。
        global cookie
        CSRFToken=0
        cookie=0
        tokenurl=self.URLprefix+'/api/session'
        headers = {
            'Accept': 'application/json, text/javascript, */*; q=0.01',
            'Accept-Encoding': 'gzip, deflate',
            'Accept-Language': 'zh-CN,zh;q=0.9',
            'Connection': 'keep-alive',
            'Content-Length': '39',
            'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
            'Host': '' + self.ip + '',
            'Origin': 'http://' + self.ip + '',
            'Referer': 'http://' + self.ip + '/',
            'User-Agent': 'Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36',
            'X-Requested-With': 'XMLHttpRequest'
        }
        print(tokenurl)
        data={
            "username":self.username,
            "password":self.password
            }
        re1=requests.post(tokenurl,data=data,headers=headers,verify=False)
        print (re1.status_code)
        if re1.status_code == 200:
            #print (re1.json())
            # print(re1.status_code)
            # print(re1.json())
            # print('header:', re1.headers)
            # temp = re1.json()
            # print(temp['CSRFToken'])
            print(re1.headers)
            print (re1.json())
            temp_header=re1.headers
            cookie=temp_header['Set-Cookie']
            temp_token=re1.json()
            CSRFToken=temp_token['CSRFToken']
            print ('restfull_cookie',cookie)
            print ('restfull_CSRFToken',CSRFToken)
        else:
            pass
    def restfull_info(self,URL_suffix):  #定義總獲取函數,傳參url的后半部分。如'/redfish/v1/Systems/1/Memory'
        urlset=self.URLprefix + URL_suffix.strip()
        #print(urlset)
        # print ('token:',token)
        # print ('cookie:',cookie)
        if cookie != 0  and token != 0 :
            cook = cookie.split(";")[0].split("=")[1]
            print ('restfull_cook',cook)
            header = {
                'Accept': 'application/json, text/javascript, */*; q=0.01',
                'Accept-Encoding': 'gzip, deflate',
                'Accept-Language': 'zh-CN,zh;q=0.9',
                'Connection': 'keep-alive',
                'Cookie': 'refresh_disable=1; QSESSIONID='+cook+'',
                'Host': ''+self.ip+'',
                'Referer': 'http://'+self.ip+'/',
                'User-Agent': 'Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36',
                'X-CSRFTOKEN': ''+CSRFToken+'',
                'X-Requested-With': 'XMLHttpRequest'
                }
            re1=requests.get(urlset,headers=header,verify=False)
            print(re1.status_code)
            return (re1.json())
        else:
            pass

##合并restfull與redfish為一個類。
class GetHostInfo(object):
    def __init__(self,ipaddr,username,password):
        self.ip=ipaddr.strip()
        self.URLprefix='https://'+ipaddr.strip()
        self.username=username.strip()
        self.password=password.strip()
        global redfish_token
        global restfull_CSRFToken
        global restfull_cookie
        restfull_CSRFToken=0
        restfull_cookie=0
        redfish_token=0     ##該token是加密的
        ##以下是redfish的認證過程
        redfishurl=self.URLprefix+'/redfish/v1/SessionService/Sessions'
        print(redfishurl)
        data={
            "UserName":self.username,
            "Password":self.password
            }
        header={
            "Content-Type":"application/json"
            }
        re1=requests.post(redfishurl,json.dumps(data),headers=header,verify=False)
        print(re1.status_code)
        if re1.status_code == 201:
            print('redfish_info', re1.json())
            print('redfish_header', re1.headers)
            temp_header = re1.headers
            redfish_token = temp_header['X-Auth-Token']
            print('redfish_token', redfish_token)
        else:
            pass
        ##以下是restfull的認證過程
        restfullurl = self.URLprefix + '/api/session'
        restheaders = {
            'Accept': 'application/json, text/javascript, */*; q=0.01',
            'Accept-Encoding': 'gzip, deflate',
            'Accept-Language': 'zh-CN,zh;q=0.9',
            'Connection': 'keep-alive',
            'Content-Length': '39',
            'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
            'Host': '' + self.ip + '',
            'Origin': 'http://' + self.ip + '',
            'Referer': 'http://' + self.ip + '/',
            'User-Agent': 'Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36',
            'X-Requested-With': 'XMLHttpRequest'
        }
        print(restfullurl)
        data = {
            "username": self.username,
            "password": self.password
        }
        re1 = requests.post(restfullurl, data=data, headers=restheaders, verify=False)
        print(re1.status_code)
        if re1.status_code == 200:
            # print (re1.json())
            # print(re1.status_code)
            # print(re1.json())
            # print('header:', re1.headers)
            # temp = re1.json()
            # print(temp['CSRFToken'])
            print(re1.headers)
            print(re1.json())
            temp_header = re1.headers
            restfull_cookie = temp_header['Set-Cookie']
            temp_token = re1.json()
            restfull_CSRFToken = temp_token['CSRFToken']
            print('restfull_cookie', restfull_cookie)
            print('restfull_CSRFToken', restfull_CSRFToken)
        else:
            pass

    def redfish_info(self,URL_suffix):  #定義總獲取函數,傳參url的后半部分。如'/redfish/v1/Systems/1/Memory'
        urlset=self.URLprefix+URL_suffix
        if token !=0:
            header = {
                "Content-Type":"application/json",
                "X-Auth-Token":redfish_token
                }
            re1=requests.get(urlset,headers=header,verify=False)
            #print(re1.status_code)
            return (re1.json())
        else:
            pass
    def restfull_info(self,URL_suffix):
        urlset=self.URLprefix+URL_suffix
        if token !=0:
            cook = restfull_cookie.split(";")[0].split("=")[1]
            print('cookie__restfull',restfull_cookie)
            print('cook__restfull',cook)
            print('CSRFToken__restfull',restfull_CSRFToken)
            header = {
                'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
                'Accept-Encoding': 'gzip, deflate',
                'Accept-Language': 'zh-CN,zh;q=0.9',
                'Connection': 'keep-alive',
                'Cookie': 'refresh_disable=1; QSESSIONID='+cook+'',
                'Host': ''+self.ip+'',
                'Referer': 'https://'+self.ip+'/main.html',
                'X-CSRFTOKEN': ''+restfull_CSRFToken+'',
                'X-Requested-With': 'XMLHttpRequest'
                }
            re1=requests.get(urlset,headers=header,verify=False)
            print(re1.status_code)
            return re1.json()
        else:
            pass

##redfish獲取參數的測試
##使用一個system總的URL分別獲取到cpu、內存、存儲三個url.所以只修改system的URL即可
##sel日志單獨使用URL獲取
def Collect_Iredfish_getinfonfo(ipaddr,username,password):
    H3CR4900=redfish_getinfo(ipaddr,username,password)
    ####total_system_URL收集/redfish/v1/Systems/1
    select_system_total = '/redfish/v1/Systems/1'
    #print('cpu_total', hw2288HV5.Redfish_GetInfo(select_cpu_total))
    temp_system_result1= H3CR4900.redfish_info(select_system_total)
    if isinstance(temp_system_result1,dict) and ('error' not in  temp_system_result1.keys() ):
        ##處理cpu
        cpu = temp_system_result1['Processors']['@odata.id']  ##獲取CPU的URL
        #print ('Processors',H3CR4900.Redfish_GetInfo(cpu))
        cpu_result1 = H3CR4900.redfish_info(cpu)
        cpu_count = cpu_result1['Members@odata.count']
        cpu_URLsuffix_list = [x['@odata.id'] for x in cpu_result1['Members']]
        print('CPU count:', cpu_count)
        for single_cpuurl in cpu_URLsuffix_list:
            singlecpu_result2= H3CR4900.redfish_info(single_cpuurl)
            if isinstance(singlecpu_result2, dict) and ('error' not in singlecpu_result2.keys()):
                #print ('singlecpu_result2',singlecpu_result2)
                print('CPU single name:', singlecpu_result2['Name'])
                print('CPU single ID:', singlecpu_result2['Id'])
                print('CPU single TotalCores(cpus):', singlecpu_result2['TotalCores'])
                print('CPU single Model(cpus):', singlecpu_result2['Model'])

        ###處理內存
        memory = temp_system_result1['Memory']['@odata.id']  ##獲取內存的URL
        memory_result1 = H3CR4900.redfish_info(memory)
        memory_count = memory_result1['Members@odata.count']
        memory_URLsuffix_list = [x['@odata.id'] for x in memory_result1['Members']]
        print ('Memory count:',memory_count)
        for single_memoryurl in memory_URLsuffix_list:
            singlememory_result2 = H3CR4900.redfish_info(single_memoryurl)
            if isinstance(singlememory_result2, dict) and ('error' not in singlememory_result2.keys()):
                #print('singlecpu_result2', singlememory_result2)
                print('Memory name:', singlememory_result2['Name'])
                print('Memory ID:', singlememory_result2['Id'])
                print('Memory Size:', singlememory_result2['CapacityMiB'])
                print('Memory Type:', singlememory_result2['MemoryDeviceType'])

        ##處理存儲
        storage = temp_system_result1['Storage']['@odata.id']  ##獲取存儲URL
        #print ('storage',H3CR4900.Redfish_GetInfo(storage))
        storage_result1 = H3CR4900.redfish_info(storage)
        storage_URLsuffix_list = [x['@odata.id'] for x in storage_result1['Members']]
        for single_storageurl in storage_URLsuffix_list:
            singlestorage_result2 = H3CR4900.redfish_info(single_storageurl)
            if isinstance(singlestorage_result2, dict) and ('error' not in singlestorage_result2.keys()):
                #print('singlecpu_result2', singlestorage_result2)
                disk_count=singlestorage_result2['Drives@odata.count']
                print('disk count:',disk_count)
                print('storage name:',singlestorage_result2['Id'])
                if disk_count >0: ##有的URL中disk為0,不需要去獲取值
                    single_disk_URLsuffix_list = [x['@odata.id'] for x in singlestorage_result2['Drives']]
                    for disk_single in single_disk_URLsuffix_list:
                        single_disk_result1 = H3CR4900.redfish_info(disk_single)
                        if isinstance(single_disk_result1, dict) and ('error' not in single_disk_result1.keys()):
                            #print ('single_disk_result1',single_disk_result1)
                            print('disk name:', single_disk_result1['Name'])
                            print('disk ID:', single_disk_result1['Id'])
                            print('disk CapacityBytes:', single_disk_result1['CapacityBytes'])
                            print('disk MediaType:', single_disk_result1['MediaType'])
                        else:
                            pass
    ##獲取sel日志   需要四個url執行。
    # logurlsuffix = '/redfish/v1/Managers/iDRAC.Embedded.1/Logs/Sel'  ##日志sel
    # sellog=H3CR4900.Redfish_GetInfo(logurlsuffix)
    # if isinstance(sellog,dict) and ('error' not in  sellog.keys() ):
    #     print('SEL log:',sellog)

if __name__ == '__main__':
    # Collect_Info('10.251.214.12', 'username', 'password')
    ##以下是redfish和restfull的分別測試
    redfish_system ='/redfish/v1/Systems/1'
    restfull_cpuage='/api/system/cupsdata'
    restfull_info=restfull_info('10.251.214.12','username', 'password')
    redfish_info=redfish_getinfo('10.251.214.12','username', 'password')
    print ('restfull',restfull_info.restfull_info(restfull_cpuage))
    print ('redfish',redfish_info.redfish_info(redfish_system))
    ##以下是restful和redfish合并后
    redfish_resfull=GetHostInfo('10.251.214.11','username', 'password')
    print('restfull', redfish_resfull.restfull_info(restfull_cpuage))
    print('redfish', redfish_resfull.redfish_info(redfish_system))

以下單獨對redfish模塊進行測試。直接使用redfish模塊。

**import redfish**

login_host="https://10.251.214.11"  ##h4c
login_account="usename"
login_password="password"
REDFISH_OBJ = redfish.redfish_client(base_url=login_host,username=login_account, password=login_password, default_prefix='/redfish/v1')
REDFISH_OBJ.login(auth="session")
response = REDFISH_OBJ.get("/redfish/v1/Systems/1", None)
print(response)
REDFISH_OBJ.logout()
向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

蒙阴县| 六盘水市| 蓬溪县| 成安县| 镇坪县| 阿克陶县| 道孚县| 章丘市| 建德市| 专栏| 团风县| 两当县| 吉水县| 南江县| 萍乡市| 丰都县| 伊通| 保康县| 丹寨县| 文安县| 灌云县| 江陵县| 东方市| 长白| 体育| 扎囊县| 株洲县| 三门峡市| 池州市| 玉山县| 康保县| 岚皋县| 元谋县| 黎川县| 迁西县| 贵阳市| 顺昌县| 明星| 胶州市| 青海省| 枝江市|