-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsupermicro_ipmi_api.py
More file actions
183 lines (128 loc) · 5.09 KB
/
supermicro_ipmi_api.py
File metadata and controls
183 lines (128 loc) · 5.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import requests as rq
import secrets as s
# ----------------------------------------------------------------------------------------------------------------------
# GLOBALS
# ----------------------------------------------------------------------------------------------------------------------
session = rq.session()
logged_in = False
# urls
login_url = f"http://{s.ip_address}/cgi/login.cgi"
logout_url = f"http://{s.ip_address}/cgi/logout.cgi"
impi_url = f"http://{s.ip_address}/cgi/ipmi.cgi"
# ----------------------------------------------------------------------------------------------------------------------
# FUNCTIONS
# ----------------------------------------------------------------------------------------------------------------------
def login(url: str = None, usr_name: str = None, password: str = None):
"""
Performs a login to get the session cookie from the IPMI Interface.
:param url: url to send post request to. default is login_url in globals
:param usr_name: user name for the login. default secrets.server_usr
:param password: password for the login. default secrets.server_pwd
:return:
"""
_url = login_url
_usr = s.server_usr
_pwd = s.server_pwd
global logged_in
# overwrite populated entries
if url is not None:
_url = url
if usr_name is not None:
_usr = usr_name
if password is not None:
_pwd = password
_login = session.request("POST", _url, data={"name": _usr, "pwd": _pwd})
# check response
if not _login.ok:
raise ValueError(f"Wrong Credentials or URL: code {_login.status_code}")
logged_in = True
def logout(url: str = None):
"""
Performs logout.
:param url: url for logout. default logout_url
:return:
"""
_url = logout_url
global logged_in
if url is not None:
_url = url
_logout = session.request("GET", _url)
logged_in = False
if not _logout.ok:
raise ValueError(f"Wrong Credentials or URL: code {_logout.status_code}")
def power_on(logout_after: bool = False):
"""
Performs power on. If the user is not logged in, a login with the defaults is attempted.
:param logout_after: logs user out after power_on
:return:
"""
if not logged_in:
login()
_power_on = session.request("POST", impi_url, data={"POWER_INFO.XML": "(1, 1)"})
if not _power_on.ok:
raise ValueError(f"Wrong Credentials or URL: code {_power_on.status_code}")
if logout_after:
logout()
def power_off_orderly(logout_after: bool = False):
"""
Performs orderly poweroff. If the user is not logged in, a login with the defaults is attempted.
:param logout_after: logs user out after orderly power off
:return:
"""
if not logged_in:
login()
_power_off_o = session.request("POST", impi_url, data={"POWER_INFO.XML": "(1, 5)"})
if not _power_off_o.ok:
raise ValueError(f"Wrong Credentials or URL: code {_power_off_o.status_code}")
if logout_after:
logout()
def power_off_immediately(logout_after: bool = False):
"""
Performs immediate power off. If the user is not logged in, a login with the defaults is attempted.
:param logout_after: logs user out after immediate power off
:return:
"""
if not logged_in:
login()
_power_off_i = session.request("POST", impi_url, data={"POWER_INFO.XML": "(1, 0)"})
if not _power_off_i.ok:
raise ValueError(f"Wrong Credentials or URL: code {_power_off_i.status_code}")
if logout_after:
logout()
def reset_power(logout_after: bool = False):
"""
Performs power reset. If the user is not logged in, a login with the defaults is attempted.
:param logout_after: logs user out after power reset
:return:
"""
if not logged_in:
login()
_power_off_r = session.request("POST", impi_url, data={"POWER_INFO.XML": "(1, 3)"})
if not _power_off_r.ok:
raise ValueError(f"Wrong Credentials or URL: code {_power_off_r.status_code}")
if logout_after:
logout()
def power_cycle(logout_after: bool = False):
"""
Performs power cycle. If the user is not logged in, a login with the defaults is attempted.
:param logout_after: logs user out after power cycle
:return:
"""
if not logged_in:
login()
_power_off_ps = session.request("POST", impi_url, data={"POWER_INFO.XML": "(1, 2)"})
if not _power_off_ps.ok:
raise ValueError(f"Wrong Credentials or URL: code {_power_off_ps.status_code}")
if logout_after:
logout()
def get_power_status(logout_after: bool = False):
power = None
if not logged_in:
login()
_get_power_status = session.request("POST", impi_url, data={"POWER_INFO.XML": "0, 0"})
if _get_power_status.ok:
et = ET.fromstring(_get_power_status.content)
power = et.find("POWER_INFO").find("POWER").get("STATUS")
if logout_after:
logout()
return power