Add initial rc-power-management script

This commit is contained in:
Timothée Floure 2020-10-04 18:28:28 +02:00
parent db2fbdebe4
commit 2f661126e7
2 changed files with 56 additions and 0 deletions

55
rc-power-management Executable file
View file

@ -0,0 +1,55 @@
#!/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

View file

@ -0,0 +1 @@
demo,2c:3a:fd:f6:13:0e,user,password