#!/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 : 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.178.100 192.168.178.113 192.168.178.31 192.168.178.201" ## CLIENTS="192.168.178.117" d=`date +%d-%m-%Y-%H-%M` ########################################################## # 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 -h +1 fi else echo $d 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 echo $d echo "No client(s) active. System will be shutdown now!" /sbin/shutdown -h now ## End of script