#!/bin/bash BAREOS_SD_IP=172.16.2.23 BAREOS_SD_PORT=9103 IPMI_IP=172.16.2.18 IPMI_USER=USER IPMI_PASS=PASSWORD SLEEP=5 NUM_TRIES=6 function check_bareos_sd { nc -w1 $BAREOS_SD_IP $BAREOS_SD_PORT < /dev/null 2>/dev/null if [ $? -eq 0 ]; then # if return value is 0 then port is reachable so we can exit the function with value 0 return 0 else return 1 fi } COUNT=0 while true; do check_bareos_sd if [ $? -eq 0 ]; then # We are online, so add 1 to counter, sleep for $SLEEP seconds and continue loop # $COUNT=0 then we can power off the machine using ssh if [ $COUNT -eq 0 ]; then ssh -t $BAREOS_SD_IP sudo /usr/sbin/shutdown -h now fi let COUNT=$COUNT+1 sleep $SLEEP else # We are offline, so we can exit the loop exit 0 fi # check if $COUNT reaches $NUM_TRIES and when it does do a hard power down if [ $COUNT -eq $NUM_TRIES ]; then ireset -d -N $IPMI_IP -U $IPMI_USER -P $IPMI_PASS >/dev/null 2>&1 if [ $? -eq 0 ]; then exit 0 else exit 1 fi fi done