该脚本针对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" ##h3c

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()

服务器采集协议,H3C设备服务器采集参数认证过程(包含redfish和restfull协议)相关推荐

  1. H3C批量收集服务器信息,H3C设备服务器采集参数认证过程(包含redfish和restfull协议)...

    该脚本针对H3C服务器分别对redfish和restfull两种协议的认证方式进行测试,并合并. 有三个类,分别是redfish协议测试.restfull协议测试.以及两个合并测试 文章最后使用red ...

  2. 服务器中显示存储设备,服务器节点信息集中显示方法、系统、设备及存储介质...

    1. 一种服务器节点信息集中显示方法,其特征在于,应用于CMC,包括: 获取M个BMC各自收集的参数信息数据,得到目标数据: 将所述目标数据发送至N个BMC,以使所述N个BMC中的任一BMC显示所述目 ...

  3. protal服务器获取不到设备信息,Poral网页认证提示portal服务器获取不到设备信息或者设备没有回应req_info报文的解决办法...

    portal网页认证时提示获取不到设备信息或者没有回应req_info报文 文档名称 文档密级 问题描述: Portal网页认证场景,用户在浏览器输入账号名.密码点击登录时,系统提示portal se ...

  4. H3C设备之RIP v2认证

        配置RIP 认证: RIP V2同上个实验RIP V2配置相同. [RTA-S6/0]rip authentication-mode md5 rfc2453 h3c12345678 此时RTA ...

  5. h3c虚拟刀片服务器,产品技术-H3C B16000-刀片服务器-新华三集团-H3C

    H3C UniServer B16000塑合智能刀片服务器是H3C自主研发的塑合智能刀片服务器系统. H3C刀片服务器的基础架构-B16000刀箱采用了一系列全新的技术,提供了简化的管理.强大的处理能 ...

  6. 华为服务器光盘引导,华为RH2288H服务器引导ServiceCD安装Windows Server操作系统

    安装准备 ServiceCD光盘. Windows操作系统安装光盘. 物理光驱. 使用虚拟控制台远程安装操作系统时,需要准备以下软件: ServiceCD光盘或ServiceCD ISO文件. Win ...

  7. 安防互联网无插件直播如何将支持Ehome协议的设备接入新版安防视频云服务平台EasyCVR?

    EasyCVR是由TSINGSEE青犀视频团队研发的一套安防视频云服务融合平台.它主要适合多品牌.多协议.多通道的安防网络设备的接入,可以将RTSP协议.RTMP协议.GB28181国标协议.海康SD ...

  8. VNC协议-认证过程

    0x00 VNC是什么 VNC(Virtual Network Computing),是一种使用RFB协议的屏幕画面分享及远程操作软件.此软件借由网络,可发送键盘与鼠标的动作及即时的屏幕画面. VNC ...

  9. Esp8266 进阶之路19 【外设篇①】esp8266驱动 ds18b20、dht11 温湿度传感器,采集温湿度传感器到服务器。(附带Demo)

    本系列博客学习由非官方人员 半颗心脏 潜心所力所写,不做开发板.仅仅做个人技术交流分享,不做任何商业用途.如有不对之处,请留言,本人及时更改. 序号 SDK版本 内容 链接 1 nonos2.0 搭建 ...

  10. 博科b8网络版定位服务器位置,如何在企业服务器中采集奥维GPS定位设备的位置...

    在企业服务服务器中,可集中采集野外用户所持奥维GPS定位设备的位置信息,实现在管理控制台中统一查看定位设备的实时位置及轨迹. 当然,您需要部署奥维服务端或者购买奥维云服务. 购买奥维云服务请参考使用帮 ...

最新文章

  1. Java gdal .mif/.mid文件读取
  2. 设计1.0 -- iterator 和const_iterator底层的模拟实现
  3. 30 个Python代码实现的常用功能,精心整理版
  4. PHP学习记录第一篇:Ubuntu14.04下LAMP环境的搭建
  5. 【Qt】简单QT文本编辑器
  6. Jmeter当获取正则表达式匹配数字为负数时获取所有匹配的值
  7. 「分布式系统理论」系列专题
  8. 单调栈 leetcode整理(三)
  9. letsencrypt 自动续期不关闭nginx
  10. python装饰器哪个好用变女生_Python女神分享教程之Python 装饰器
  11. 收藏 | 自监督视觉Transformer
  12. wireshark抓包怎么找访问网址
  13. jmeter 加密解密_AES加密的安全问题
  14. scala 判断字段 是不是 日期类型_举个栗子!Tableau 技巧(147):使用 动态参数 筛选到最新日期值...
  15. vscode jupyter补全_Cern ROOT 在jupyter里的使用
  16. windows录屏_Windows电脑录屏制作gif神器
  17. wifi物理地址怎么改_安卓手机修改wifi物理mac地址的三种方法
  18. @GuardedBy注解
  19. PMP|项目管理过程中,怎么识别风险?
  20. 有房间匹配和无房间匹配

热门文章

  1. Himall商城Api签名帮助类
  2. ORAN专题系列-5:5G O-RAN 一体式小基站硬件白盒化的参考架构
  3. slickedit使用簡介
  4. Javaweb - JSP章节 - MVC和三层架构案例总练习(下) - “回显数据”-“修改数据”功能实现
  5. 微星X79主板修改BIOS支持NVMe
  6. 一文全记录斐讯K3刷机+打印服务器+私人云盘+frp内网穿透+ftp远程上传下载
  7. ESP-AT SSL 单向认证指令操作以及问题分析
  8. 匹配区县代码_全国区县代码1
  9. ubuntu boot修复
  10. Python 之 pip安装 及 使用详解