Jump to content
XPEnology Community

Shutdown per Skript wenn kein Client online ist


thorsten_de

Recommended Posts

Hallo!

 

Zur Zeit teste ich XPEnology 4.3 V3 auf einem Standart PC bevor mein HP N54L kommt.

Das WOL habe bereits eingerichtet und funktioniert sehr gut.

 

Zusätzlich würde ich gern den Server per Skript herunterfahren wenn kein Client mehr eingeschaltet ist.

Für Openmediavault und Freenas habe ich 2 Skripte gefunden.

Die Frage ist jetzt lassen die sich auch in das XPEnology einbinden und wer hat Erfahrung mit so etwas und kann helfen?

Anbei mal die 2 Skripte als Code.

 

1. Skript Freenas

 

#!/bin/bash

 

# IP Adressen entsprechend anpassen

HOST1=192.168.178.20 # Client 1

HOST2=192.168.178.21 # Client 2

HOST3=192.168.178.22 # Client 3

HOST4=192.168.178.23 # Client 4

HOST5=192.168.178.24 # Client 5

HOST6=192.168.178.25 # Client 6

 

_exit () {

case $1 in

1) echo "Kein Shutdown - Mindestens einer der PCs ist eingeschaltet" ;;

2) echo "Kein PC online - Shutdown" ; shutdown -p now ;;

esac

exit $1;

}

 

# Checken ob PC's an sind

if [ `ping -c 1 -i 1 $HOST1 | grep -wc 100.0%` -eq 0 ] || [ `ping -c 1 -i 1 $HOST2 | grep -wc 100.0%` -eq 0 ] || [ `ping -c 1 -i 1 $HOST3 | grep -wc 100.0%` -eq 0 ] || [ `ping -c 1 -i 1 $HOST4 | grep -wc 100.0%` -eq 0 ] || [ `ping -c 1 -i 1 $HOST5 | grep -wc 100.0%` -eq 0 ] ; then _exit 1;

 

# Wenn kein PC an ist, 10 sec warten falls gerade einer Neu startet

else

echo "Kein PC online - Warte 90 Sekunden"

sleep 90

 

# noch mal PC's checken

if [ `ping -c 1 -i 1 $HOST1 | grep -wc 100.0%` -eq 0 ] || [ `ping -c 1 -i 1 $HOST2 | grep -wc 100.0%` -eq 0 ] || [ `ping -c 1 -i 1 $HOST3 | grep -wc 100.0%` -eq 0 ] || [ `ping -c 1 -i 1 $HOST4 | grep -wc 100.0%` -eq 0 ] || [ `ping -c 1 -i 1 $HOST5 | grep -wc 100.0%` -eq 0 ] ; then _exit 1;

 

# Wenn kein PC an ist, gehe zu exit case 2 (Shutdown)

else

_exit 2

fi

fi

 

Oder hier das 2. Skript für Openmediavault

 

#!/bin/bash

#

# title : shutdown_check.sh

# description: This script can be used to search active network clients.

# If there is no client active, the system will be stopped.

# author : http://www.markuswochele.com

# date : 2013-12-22

# version : 0.7

# usage : bash shutdown_check.sh

# notes : Nmap binary is needed to use this script.

 

##########################################################

# CONFIG

##########################################################

 

# Please define the client IP addresses (single IP, range or list), e.g.:

# 192.168.1.100

# 192.168.1.1-254

# 192.168.1.0/24

# 192.168.1.*

# 192.168.1.10 192.168.1.20 192.168.1.100

 

CLIENTS="192.168.1.100-254"

 

##########################################################

# GLOBAL VARIABLES - DO NOT EDIT !!!

##########################################################

 

OPTION=""

TEST=""

VERBOSE=""

NMAP=""

UP=""

 

##########################################################

# PERMANENT FUNCTIONS

##########################################################

 

function f_usage {

cat << EOF

Usage: $0 [OPTION]

 

OPTIONS:

-h display this help and exit

-c [iP] overwrite client IP addresses

-t test mode (without shutdown)

-v verbose

EOF

}

 

function f_exit {

if [ -z "$1" ]

then

echo "No client(s) active. System will be shutdown now!"

 

if [[ -z "$TEST" ]]

then

shutdown -hP +1

fi

else

echo "$1 client(s) active. Nothing to do."

fi

exit;

}

 

function f_scan {

NMAP=$(nmap -sP $CLIENTS)

UP=$(echo -e "$NMAP" | grep 'host' | awk -F'(' '{print $2}' | awk '{print $1}')

 

if [[ ! -z "$VERBOSE" ]]

then

echo -e "$NMAP\n"

fi

}

 

##########################################################

# BEGINNING OF MAIN SCRIPT

##########################################################

 

while getopts "hc:tv" OPTION

do

case $OPTION in

h)

f_usage

exit 1

;;

c)

unset CLIENTS

CLIENTS=$OPTARG

;;

t)

TEST=1

echo "*** TEST MODE ***"

;;

v)

VERBOSE=1

#set -x

;;

?)

f_usage

exit

;;

esac

done

 

if [ ! -x `which nmap` ]

then

echo "Nmap binary not found. Please check your system or install Nmap."

exit 1

fi

 

## First check

f_scan

 

if [[ "$UP" -gt 0 ]]

then

f_exit $UP

fi

 

## Waiting for the second check

echo "Waiting for the second check!"

sleep 60

 

## Second check

unset NMAP

unset UP

f_scan

 

if [[ "$UP" -gt 0 ]]

then

f_exit $UP

fi

 

## System shutdown

f_exit

 

## End of script

 

Viele Grüße, Thorsten :smile:

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...