From 2f661126e7f7573150175e01e6e7aedbcf6e715a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Floure?= Date: Sun, 4 Oct 2020 18:28:28 +0200 Subject: [PATCH] Add initial rc-power-management script --- rc-power-management | 55 ++++++++++++++++++++++++++++++++ rc-power-management.example.conf | 1 + 2 files changed, 56 insertions(+) create mode 100755 rc-power-management create mode 100644 rc-power-management.example.conf diff --git a/rc-power-management b/rc-power-management new file mode 100755 index 0000000..1d88738 --- /dev/null +++ b/rc-power-management @@ -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 diff --git a/rc-power-management.example.conf b/rc-power-management.example.conf new file mode 100644 index 0000000..a09fd67 --- /dev/null +++ b/rc-power-management.example.conf @@ -0,0 +1 @@ +demo,2c:3a:fd:f6:13:0e,user,password