Skip to content

Instantly share code, notes, and snippets.

@jworl
Last active September 8, 2022 07:53
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jworl/fd11b2b941627a12b6f6c1e1ad8715dc to your computer and use it in GitHub Desktop.
Save jworl/fd11b2b941627a12b6f6c1e1ad8715dc to your computer and use it in GitHub Desktop.
Issue reset, power off, or power on for SuperMicro BMC via web interface
#!/usr/bin/env bash
# written by: Joshua Worley
# received great help from DavidWittman here:
# https://gist.github.com/DavidWittman/98281cdf1d32ac795b74
# This version's improvements:
# - allows reset, power on, or power off
# - sanity check conditionals
# - sends pushover alert for notification
#
# usage: ./ipmi_power.sh [ipmi-address] [off|on|reset]
# example: ./ipmi_power.sh 192.168.1.100 reset
_COOKIE(){
curl -d "name=$2&pwd=$3" "https://$1/cgi/login.cgi" \
--silent --insecure -i | awk '/Set-Cookie/ && NR != 2 { print $2 }'
}
_EVENT(){
curl -s --form-string "token=put your application token here" \
--form-string "user=put your user or group token here" \
--form-string "message=$1" \
https://api.pushover.net/1/messages.json &> /dev/null
}
_POWER(){
curl "https://$1/cgi/ipmi.cgi" \
-XPOST \
-H 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8' \
-H "Referer: http://$1/cgi/url_redirect.cgi?url_name=sys_info" \
-H 'Accept: text/javascript, text/html, application/xml, text/xml, */*' \
-H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/603.2.5 (KHTML, like Gecko) Version/10.1.1 Safari/603.2.5' \
-H "Cookie: $2" \
-H "Origin: https://$1" \
-H 'X-Prototype-Version: 1.5.0' \
-H 'X-Requested-With: XMLHttpRequest' \
--data "POWER_INFO.XML=($3)&time_stamp=Tue%20Jun%2027%202017%2010%3A30%3A57%20GMT-0400%20(EDT)&_=" \
--insecure \
--silent &> /dev/null
echo $?
}
# set -x
IPMI_HOST="$1"
IPMI_ACTION="$2"
IPMI_USER=${IPMI_USER:-ADMIN}
IPMI_PASS=${IPMI_PASS:-ADMIN}
if [[ $# -ne 2 ]]; then
echo "usage: $0 [ipmi-address] [cycle|off|on|reset|shutdown]"
exit 1
fi
if [[ ${IPMI_ACTION} =~ ^shutdown$ ]]; then
POWER_INFO="1%2C5"
elif [[ ${IPMI_ACTION} =~ ^off$ ]]; then
POWER_INFO="1%2C0"
elif [[ ${IPMI_ACTION} =~ ^on$ ]]; then
POWER_INFO="1%2C1"
elif [[ ${IPMI_ACTION} =~ ^reset$ ]]; then
POWER_INFO="1%2C3"
elif [[ ${IPMI_ACTION} =~ ^cycle$ ]]; then
POWER_INFO="1%2C2"
else
echo "invalid selection: $2; please choose [cycle|off|on|reset|shutdown]"
exit 1
fi
SESSION_ID=$(_COOKIE "${IPMI_HOST}" "${IPMI_USER}" "${IPMI_PASS}")
if ! [[ ${SESSION_ID} =~ ^SID=[a-z]{16}\;$ ]]; then
MESSAGE="Unexpected SESSION_ID from ${IPMI_HOST} - ${SESSION_ID}"
_EVENT "${MESSAGE}"
exit 2
fi
EXIT_CODE=$(_POWER "${IPMI_HOST}" "${SESSION_ID}" "${POWER_INFO}")
if [[ ${EXIT_CODE} -eq 0 ]]; then
STATUS="successful"
else
STATUS="failed"
fi
MESSAGE="${IPMI_HOST} ${STATUS} ${IPMI_ACTION}"
_EVENT "${MESSAGE}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment