-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathamt_power.py
158 lines (119 loc) · 9.03 KB
/
amt_power.py
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
import subprocess
## Requirements: wsman (apt install wsmancli)
## https://www.dmtf.org/sites/default/files/standards/documents/DSP1027_2.0.0.pdf
## Functioning of this script probably depends on the specific AMT hardware (implemented cim schema)
power_down_command = '<p:RequestPowerStateChange_INPUT xmlns:p="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_PowerManagementService"> \
<p:PowerState>8</p:PowerState> \
<p:ManagedElement xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:wsman="http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd"> \
<wsa:Address>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</wsa:Address> \
<wsa:ReferenceParameters><wsman:ResourceURI>http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ComputerSystem</wsman:ResourceURI> \
<wsman:SelectorSet><wsman:Selector Name="CreationClassName">CIM_ComputerSystem</wsman:Selector> \
<wsman:Selector Name="Name">ManagedSystem</wsman:Selector> \
</wsman:SelectorSet></wsa:ReferenceParameters></p:ManagedElement></p:RequestPowerStateChange_INPUT>'
power_up_command = '<p:RequestPowerStateChange_INPUT xmlns:p="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_PowerManagementService"> \
<p:PowerState>2</p:PowerState> \
<p:ManagedElement xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:wsman="http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd"> \
<wsa:Address>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</wsa:Address> \
<wsa:ReferenceParameters><wsman:ResourceURI>http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ComputerSystem</wsman:ResourceURI> \
<wsman:SelectorSet><wsman:Selector Name="CreationClassName">CIM_ComputerSystem</wsman:Selector> \
<wsman:Selector Name="Name">ManagedSystem</wsman:Selector> \
</wsman:SelectorSet></wsa:ReferenceParameters></p:ManagedElement></p:RequestPowerStateChange_INPUT>'
power_hard_reset_command = '<p:RequestPowerStateChange_INPUT xmlns:p="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_PowerManagementService"> \
<p:PowerState>10</p:PowerState> \
<p:ManagedElement xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:wsman="http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd"> \
<wsa:Address>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</wsa:Address> \
<wsa:ReferenceParameters><wsman:ResourceURI>http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ComputerSystem</wsman:ResourceURI> \
<wsman:SelectorSet><wsman:Selector Name="CreationClassName">CIM_ComputerSystem</wsman:Selector> \
<wsman:Selector Name="Name">ManagedSystem</wsman:Selector> \
</wsman:SelectorSet></wsa:ReferenceParameters></p:ManagedElement></p:RequestPowerStateChange_INPUT>'
force_pxe_boot_command = '<p:ChangeBootOrder_INPUT xmlns:p="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_BootConfigSetting"> \
<p:Source xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:wsman="http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd"> \
<wsa:Address>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</wsa:Address> \
<wsa:ReferenceParameters><wsman:ResourceURI>http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_BootSourceSetting</wsman:ResourceURI> \
<wsman:SelectorSet><wsman:Selector wsman:Name="InstanceID">Intel(r) AMT: Force PXE Boot</wsman:Selector></wsman:SelectorSet> \
</wsa:ReferenceParameters></p:Source></p:ChangeBootOrder_INPUT>'
force_local_disk_command = '<p:ChangeBootOrder_INPUT xmlns:p="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_BootConfigSetting"> \
<p:Source xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:wsman="http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd"> \
<wsa:Address>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</wsa:Address> \
<wsa:ReferenceParameters><wsman:ResourceURI>http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_BootSourceSetting</wsman:ResourceURI> \
<wsman:SelectorSet><wsman:Selector wsman:Name="InstanceID">Intel(r) AMT: Force Hard-drive Boot</wsman:Selector></wsman:SelectorSet> \
</wsa:ReferenceParameters></p:Source></p:ChangeBootOrder_INPUT>'
wsman_boot_device_method = ''' 'ChangeBootOrder' 'http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_BootConfigSetting?InstanceID="Intel(r) AMT: Boot Configuration 0"' '''
wsman_power_method = ''' 'RequestPowerStateChange' 'http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_PowerManagementService?SystemCreationClassName="CIM_ComputerSystem"&SystemName="Intel(r) AMT"&CreationClassName="CIM_PowerManagementService"&Name="Intel(r) AMT Power Management Service"' '''
def get_power_state(targetip:str = "", port:str = "16992", password:str = "", username:str = "admin"):
if(targetip == ""):
print("You need to provide the parameter targetip!")
return
if(password == ""):
print("You need to provide the parameter password!")
return
command = "wsman --port " + port + " --hostname " + targetip + " --username " + username + " --password " + password + " --noverifypeer --noverifyhost --optimize --encoding utf-8 enumerate 'http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_AssociatedPowerManagementService'"
print(command)
subprocess.call(command , shell=True)
# Look for that line: <h:PowerState>X</h:PowerState>
return
def hard_reset(targetip:str = "", port:str = "16992", password:str = "", username:str = "admin"):
if(targetip == ""):
print("You need to provide the parameter targetip!")
return
if(password == ""):
print("You need to provide the parameter password!")
return
command = "wsman --port " + port + " --hostname " + targetip + " --username " + username + " --password " + password + " --noverifypeer --noverifyhost --input - invoke --method "
command = command + wsman_power_method
command = "echo '" + power_hard_reset_command + "' | " + command
print(command)
subprocess.call(command , shell=True)
return
def power_down(targetip:str = "", port:str = "16992", password:str = "", username:str = "admin"):
if(targetip == ""):
print("You need to provide the parameter targetip!")
return
if(password == ""):
print("You need to provide the parameter password!")
return
command = "wsman --port " + port + " --hostname " + targetip + " --username " + username + " --password " + password + " --noverifypeer --noverifyhost --input - invoke --method "
command = command + wsman_power_method
command = "echo '" + power_down_command + "' | " + command
print(command)
subprocess.call(command , shell=True)
return
def power_up(targetip:str = "", port:str = "16992", password:str = "", username:str = "admin"):
if(targetip == ""):
print("You need to provide the parameter targetip!")
return
if(password == ""):
print("You need to provide the parameter password!")
return
command = "wsman --port " + port + " --hostname " + targetip + " --username " + username + " --password " + password + " --noverifypeer --noverifyhost --input - invoke --method "
command = command + wsman_power_method
command = "echo '" + power_up_command + "' | " + command
print(command)
subprocess.call(command , shell=True)
return
def set_boot_from_disk(targetip:str = "", port:str = "16992", password:str = "", username:str = "admin"):
if(targetip == ""):
print("You need to provide the parameter targetip!")
return
if(password == ""):
print("You need to provide the parameter password!")
return
command = "wsman --port " + port + " --hostname " + targetip + " --username " + username + " --password " + password + " --noverifypeer --noverifyhost --input - invoke --method "
command = command + wsman_boot_device_method
command = "echo '" + force_local_disk_command + "' | " + command
print(command)
subprocess.call(command , shell=True)
return
def set_boot_from_network(targetip:str = "", port:str = "16992", password:str = "", username:str = "admin"):
if(targetip == ""):
print("You need to provide the parameter targetip!")
return
if(password == ""):
print("You need to provide the parameter password!")
return
command = "wsman --port " + port + " --hostname " + targetip + " --username " + username + " --password " + password + " --noverifypeer --noverifyhost --input - invoke --method "
command = command + wsman_boot_device_method
command = "echo '" + force_pxe_boot_command + "' | " + command
print(command)
subprocess.call(command , shell=True)
return