forked from ya-mouse/redirector
-
Notifications
You must be signed in to change notification settings - Fork 2
/
redirector-ILO
273 lines (256 loc) · 9.71 KB
/
redirector-ILO
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#!/bin/sh -eu
extract_json()
{
local json="$1" shift
local key="$2"
[ -n "$json" ] && printf '%s\n' "$json" | jq -r ."$key"
}
extract_ilo2()
{
local data="$1" shift
local key=$2
[ -n "$data" ] && printf "%s" "$data" | $sed -n -e 's/.*"'"$key"'","\(.*\)".*/\1/gp'
}
ILO_guess()
{
local ilo_json=$($curl https://$hostip/json/login_session)
ilo=
if fgrep -q 'Copyright 2001, 2015 Hewlett-Packard Development Company, L.P.' $1; then \
if fgrep -q '<TITLE>HP Integrated Lights-Out 2 Login</TITLE>' $1; then
ilo='iLO 2'
hpsession=$(cat $1 | $sed -n -e 's/.*sessionkey=\([^;]\+\);.*/\1/gp')
hpsindex=$(cat $1 | $sed -n -e 's/.*sessionindex=\([^;]\+\);.*/\1/gp')
[ -n $hpsession ] && hpsession=$(echo $hpsession|$sed -e 's/"//g')
[ -n $hpsindex ] && hpsindex=$(echo $hpsindex|$sed -e 's/"//g')
return 0
fi
fi
if egrep -q 'Hewlett[\ -]Packard.*[LP\.]' $1; then \
ilo='iLO 3'
fgrep -q '<title>iLO 3</title>' $1 && return 0
# iLO 4 is little bit tricky
ilo='iLO 4'
fgrep -q '<title>iLO 4</title>' $1 && return 0
[ -n "$ilo_json" ] || return 1
# another json source found in iLO 4 v2.20
ilo="$(extract_json "$ilo_json" alt.bmcgen)"
[ -n "$ilo" -a "$ilo" != 'null' ] && return 0
ilo="$(extract_json "$ilo_json" moniker.PRODGEN)"
[ -n "$ilo" -a "$ilo" != 'null' ] || return 1 \
&& return 0
fi
# everything fail, just exit
return 1
}
ILO_session_open()
{
cookie=
# iLO2 uses completely different approach
if [ "$ilo" == 'iLO 2' ]; then
opt_user=$(printf '%s' $opt_user|base64)
opt_passwd=$(printf '%s' $opt_passwd|base64)
cookie="hp-iLO-Login=$hpsindex:$opt_user:$opt_passwd:$hpsession"
session="$($curl -L -b $cookie -e "https://$hostip/login.htm" "https://$hostip/index.htm")"
echo "$session" | fgrep -q 'ie_index.htm' && cookie="hp-iLO-Session=$hpsindex:::$hpsession" || session=
else
session="$($curl -d "{\"method\":\"login\",\"user_login\":\"$opt_user\",\"password\":\"$opt_passwd\"}" https://$hostip/json/login_session| jq -r '.session_key')"
[ -n "$session" ] && cookie="sessionUrl=https%253A%2F%2F$hostip%2F;sessionLang=en;sessionKey=$session;irc=last%3Djrc" || session=
fi
}
ILO_session_close()
{
if [ "$ilo" == 'iLO 2' ]; then
$curl -b "$cookie" -o /dev/null https://$hostip/logout.htm
else
$curl -b "$cookie" -o /dev/null -d "{\"method\":\"logout\",\"session_key\"=\"$session\"}:\"\"" \
https://$hostip/json/login_session
fi
}
ILO_mc_info()
{
local license=
local uefi=
local rom=
local rack_sn=
local rack_pos=
local rack_name=
local rack_ip=
local rack_mac=
local rack_state=
if [ "$ilo" == 'iLO 2' ]; then
sysinfo=$($curl -b $cookie -e "https://$hostip/ie_index.htm" "https://$hostip/dqstat.htm" | \
$sed -n -e '/currentUser=\"Administrator\"/,/function ButtonManager/{s/^function\ .*//g;s/<.*>//g;p;}')
eval `printf %s "$sysinfo" | $sed -n -e '/[[:print:]]\+\="\?[[:print:]][^"]\+\?"\?;/{s/^\(.*\)=\([[:print:]]\+"\{0\}\)"\?;/\1=\2/g;s/UID/_UID/g;p;}'`
cat <<EOF
Product Name : $(extract_ilo2 "$sysinfo" 'Product\ Name')
Serial Number : $serverSerNum
Product ID : $serverProdID
BIOS Version : $(extract_ilo2 "$sysinfo" Family) $(extract_ilo2 "$sysinfo" Date)
iLO License Type : $productVersion
iLO License Status : ${licenseExpires:-N/A}
iLO Firmware Version : $firmwareVersion $firmwareRevision
System GUID : $(extract_ilo2 "$sysinfo" UUID)
Power State : $serverPower
Device ID : 18
Manufacturer ID : 11
Manufacturer Name : Hewlett-Packard
UEFI supported : N/A
EOF
return 0
fi
json="$($curl -b "$cookie" https://$hostip/json/overview)"
license_json="$($curl -b "$cookie" https://$hostip/json/license)"
[ -n "$license_json" ] || \
license_json="$($curl -b "$cookie" https://$hostip/json/license_info)"
license="$(extract_json "$license_json" type)"
[ $license == 'null' ] && license="$(extract_json "$license_json" eval)"
uefi="$(extract_json "$json" isUEFI)"
[ $uefi == 'null' ] && uefi='N/A'
rom="$(extract_json "$json" system_rom)"
printf '%s' "$rom" | egrep -q '[0-9]{2}/[0-9]{2}/[0-9]{4}' || \
rom="$(extract_json "$json" system_rom) $(extract_json "$json" system_rom_date)"
cat <<EOF
Product Name : $(extract_json "$json" product_name)
Serial Number : $(extract_json "$json" serial_num)
Product ID : $(extract_json "$json" product_id)
BIOS Version : $rom
iLO License Type : $(extract_json "$json" license)
iLO License Status : $license
iLO Firmware Version : $(extract_json "$json" ilo_fw_version)
System GUID : $(extract_json "$json" uuid)
System Health : $(extract_json "$json" system_health)
Power State : $(extract_json "$json" power)
Device ID : 18
Manufacturer ID : 11
Manufacturer Name : Hewlett-Packard
UEFI supported : $uefi
EOF
json="$($curl -b "$cookie" https://$hostip/json/rck_info)"
if [ -n "$json" ]; then
[ "$ilo" == 'iLO 4' ] && rack_sn=$(extract_json "$json" enc_sn)
rack_pos=$(extract_json "$json" srv_loc)
[ "$rack_pos" == 'Unknown' -o "$rack_ip" == 'Unknown' ] && return 0
rack_ip=$(extract_json "$json" ip_addr)
rack_mac=$(extract_json "$json" mac_addr)
rack_name=$(extract_json "$json" rck_name)
rack_status=$(extract_json "$json" sys_health)
cat <<EOF
=== Chassis information ===
Chassis name : ${rack_name:-N/A}
Chassis S/N : ${rack_sn:-N/A}
Chassis Health : ${rack_status:-N/A}
Chassis IP : ${rack_ip:-N/A}
Chassis MAC : ${rack_mac:-N/A}
Blade position : ${rack_pos:-N/A}
EOF
fi
}
ILO_mc_reset()
{
$curl -b "$cookie" -d "{\"method\":\"reset_ilo\",\"cause\":\"diags\",\"session_key\":\"$session\"}" \
-o /dev/null https://$hostip/json/ilo_status && echo 'OK!' || error 'Failed!'
}
ILO_mc_reset_cold()
{
ipmitool -U $opt_user -P $opt_passwd -I lanplus -H $hostip_nb mc reset cold
}
ILO_kvm()
{
$curl -b "$cookie" https://$hostip/html/jnlp_template.html | \
$sed -n -e '/<jnlp /,/<\/jnlp>/{ s/<%\=[[:space:]]\+\(this.[^[:space:]]\+\)[[:space:]]%>/\1/g; p; }' >> $1
$sed -i -n -e "{s/this\.baseUrl/https:\/\/$hostip\//g;s/this\.sessionKey/$session/g;s/this\.langId/en/g; p;}" $1
}
ILO_lan_print()
{
ipmitool -U $opt_user -P $opt_passwd -I lanplus -H $hostip_nb lan print
}
fwup_ilo4()
{
local fw="$1"; shift
local get="$curl -G -b $cookie https://$hostip"
local post="$curl -b $cookie https://$hostip"
local version=$(extract_json "$($get/json/version_info)" number)
local status=$(extract_json "$($get/json/flash_status)" state)
local fw_json=
local progress=0
local flags=
[ -n "$version" -a "$version" != null ] && echo "Going to update $ilo -> $version..." || error 'Unable to get version info, exiting!'
case "$status" in
IDLE) echo "Got status $status, guess we could start..."
;;
*) echo -n "BMC in ${status:-N/A} state, trying to reset..."
ILO_mc_reset
return 1
;;
esac
echo -n 'Preparing BMC to flash...'
$post/json/flash_status -d "{\"method\":\"clear_status\",\"session_key\":\"$session\"}" && echo done || error 'Failed to set status, exiting!'
echo -n "Uploading FW image..."
$post/cgi-bin/uploadFile -m 300 -H 'Expect:' -f --form-string "sessionKey=$session" -F "file=@\"$fw\"" && echo done || error failed
while sleep 1; do
fw_json=$($get/sse/flash|$sed -ne '/{/,/}$/{s/^data:[[:space:]]\(.*\)/\1/g;p;}')
[ -n "$fw_json" ] || error "Got no status?, weird.."
status=$(extract_json "$fw_json" state)
progress=$(extract_json "$fw_json" progress)
flags=$(extract_json "$fw_json" flags)
[ $progress -eq 100 ] && break
case "$status" in
UPLOADING) printf "\r==> %s%%" $progress
;;
PROGRESSING|VALIDATING) printf "\r%s: w/ FLAGS %s == Completed %s%%" $status ${flags:-N/A} $progress
;;
COMPLETED) break
;;
esac
done
}
fwup_ilo3()
{
local fw="$1"; shift
local get="$curl -G -b $cookie https://$hostip"
local post="$curl -b $cookie https://$hostip"
local version=$(extract_json "$($get/json/version_info)" number)
local status=$(extract_json "$($get/json/flash_status)" state)
local fw_json=
local progress=0
[ -n "$version" -a "$version" != null ] && echo "Going to update $ilo -> $version..." || error 'Unable to get version info, exiting!'
[ -n "$status" -a "$status" == IDLE ] || error 'BMC not in ready state, exiting!'
echo -n "Uploading FW image..."
$post/cgi-bin/uploadFile -m 300 -H 'Expect:' -f --form-string "sessionKey=$session" -F "file=@\"$fw\"" | fgrep -q sessionKey && echo done || error failed
while sleep 1; do
fw_json=$($get/json/flash_status)
[ -n "$fw_json" ] || error "Got no status?, weird.."
status=$(extract_json "$fw_json" state)
progress=$(extract_json "$fw_json" progress)
[ $progress -eq 100 ] && break
case "$status" in
UPLOADING) printf "\r==> %s%%" $progress
;;
PROGRESSING|VALIDATING) printf "\r%s: Completed %s%%" $status $progress
;;
COMPLETED) break
;;
esac
done
printf '\rFinished, finalizing things...\n'
sleep 30
}
ILO_lan_set()
{
case "$1" in
fwup) shift
[ -n "$1" ] || error 'Image file not specified!'
[ -e "$1" ] || error "File \`$1' doesn't exists!"
case "$ilo" in
iLO*3) fwup_ilo3 $1
;;
iLO*4) fwup_ilo4 $1
;;
*) echo "$ilo is unsupported!"
;;
esac
printf "\nFlash complete!\n"
session=
;;
esac
}