Skip to content

Instantly share code, notes, and snippets.

@samson4649
Last active March 6, 2020 12:28
Show Gist options
  • Save samson4649/b860fd0f951e19b61923742ef73c6dfc to your computer and use it in GitHub Desktop.
Save samson4649/b860fd0f951e19b61923742ef73c6dfc to your computer and use it in GitHub Desktop.
Used to control super micro servers power state via the IPMI controller remotely
#!/bin/bash
##############################################################################
# _________ _____ .__ #
# / _____/__ ________ ___________ / \ |__| ___________ ____ #
# \_____ \| | \____ \_/ __ \_ __ \ / \ / \| |/ ___\_ __ \/ _ \ #
# / \ | / |_> > ___/| | \/ / Y \ \ \___| | \( <_> ) #
# /_______ /____/| __/ \___ >__| \____|__ /__|\___ >__| \____/ #
# \/ |__| \/ \/ \/ #
# Authored by: Samuel Lock (github.com/samson4649) #
# #
# This script is to control hard power options from the IPMI interface #
# usually accessed via the web interface ( on, off, reset ) #
# #
##############################################################################
DATE=$(date +'%a %b %d %Y %T GMT %z (%Z)')
PWR_ON="(1,1)"
PWR_OFF="(1,5)"
PWR_RST="(1,3)"
PWR_EXEC=""
ADMIN_USER=ADMIN
ADMIN_PASS=ADMIN
COOKIE_JAR=$(mktemp)
# usage
function usage(){
echo "Usage: sm-pwr-control.sh [ --on | --off | --reset ] <HOST1> [ <HOST2>...] "
exit 0
}
# control exit code
function badexit(){
if [ "$#" -eq "0" ]; then
e=99
else
e=$1
fi
echo "bad exit"
eatcookies
exit ${e}
}
# clean up temp files
function eatcookies(){
rm -f ${COOKIE_JAR} &>/dev/null
}
# log breaks
function stormbreaker(){
echo "host loop break for: $@"
}
# parse arguments
POSITIONAL=()
while [[ $# -gt 0 ]];do
key="$1"
case $key in
--usage|--help|-h)
usage
shift
;;
--start|--on)
PWR_EXEC="${PWR_ON}"
shift
;;
--stop|--off)
PWR_EXEC="${PWR_OFF}"
shift
;;
--reset)
PWR_EXEC="${PWR_RST}"
shift
;;
*)
POSITIONAL+=("$1") # save it in an array for later
shift
;;
esac
done
# restore positional arguments - hosts
set -- "${POSITIONAL[@]}"
# if no action specified - crash
if [ -z "${PWR_EXEC}" ]; then
echo "no action provided" && badexit 4
fi
# loop over each host to be actioned
for host in $@; do
# post login
curl -sSL "http://${host}/cgi/login.cgi" \
--compressed \
-H 'Content-Type: application/x-www-form-urlencoded' \
-H "Origin: http://${HOST}" \
-H 'Connection: keep-alive' \
-H 'Upgrade-Insecure-Requests: 1' \
--data "name=${ADMIN_USER}&pwd=${ADMIN_PASS}" \
-c ${COOKIE_JAR} >/dev/null
if [ "$?" -eq "0" ]; then
curl -sSL "http://${host}/cgi/ipmi.cgi" \
--compressed \
-H 'X-Requested-With: XMLHttpRequest' \
-H 'X-Prototype-Version: 1.5.0' \
-H 'Content-type: application/x-www-form-urlencoded; charset=UTF-8' \
-H 'Connection: keep-alive' \
--cookie ${COOKIE_JAR} \
--data "POWER_INFO.XML=${PWR_EXEC}&time_stamp=${DATE}" >/dev/null
if [ "$?" -eq "0" ]; then
echo "${host} -> Action completed successfully"
else
stormbreaker ${host} action
fi
else
stormbreaker ${host} login
fi
done
eatcookies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment