该脚本针对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(t