tools/rc-power-management

56 lines
1.2 KiB
Text
Raw Normal View History

2020-10-04 18:28:28 +02:00
#!/bin/sh
###
# Initialization.
for file in arp ipmitool; do
if ! command -v $file > /dev/null; then
echo "Could not find the 'arp' command. Exiting." >&2
exit 1
fi
done
###
# Command-line argument handling.
if [ "$#" -le 2 ]; then
echo "Usage: rc-power-management MACHINE_NAME IPMI_COMMAND" >&2
exit 1
fi
machine_name=$1
ipmi_command=$(echo "$@" | cut --delimiter ' ' --fields 1 --complement )
###
# Configuration handling.
if [ -f 'rc-power-management.conf' ]; then
conf=rc-power-management.conf
elif [ -f '/etc/rc-power-management.conf' ]; then
conf=/etc/rc-power-management.conf
fi
if [ "$conf" = "" ]; then
echo "Could not find configuration file. Exiting." >&2
exit 1
fi
match=$(grep --max-count 1 "$NAME" < $conf)
if [ "$match" = "" ]; then
echo "Could not find machine $machine_name in $conf. Exiting." >&2
exit 1
fi
macaddr=$(echo "$match" | cut -d ',' -f 2)
ipmi_user=$(echo "$match" | cut -d ',' -f 3)
ipmi_password=$(echo "$match" | cut -d ',' -f 4)
###
# Executing IPMI call.
ipaddr=$(arp -a | grep "$macaddr" | sed -n 's/^.*(\(.*\)) .*$/\1/p')
echo "Executing '$ipmi_command' for $machine_name against $ipaddr..."
# shellcheck disable=SC2086
ipmitool -I lanplus -H "$ipaddr" -U "$ipmi_user" -P "$ipmi_password" $ipmi_command