#!/usr/bin/env bash script_name="$(basename $(realpath $0))" working_dir="$(dirname $(realpath $0))" conf_file="${working_dir}/conf/${script_name%%.*}.conf" LOCK_DIR="$(mktemp -d)" # ---------- # Base Function(s) # ---------- clean_up() { # Perform program exit housekeeping rm -rf "$LOCK_DIR" blank_line exit $1 } echononl(){ if $terminal ; then echo X\\c > /tmp/shprompt$$ if [ `wc -c /tmp/shprompt$$ | awk '{print $1}'` -eq 1 ]; then echo -e -n " $*\\c" 1>&2 else echo -e -n " $*" 1>&2 fi rm /tmp/shprompt$$ fi } fatal(){ echo "" if $terminal ; then echo -e " [ \033[31m\033[1mFatal\033[m ] $*" else echo -e " [ Fatal ] $*" fi echo "" if $terminal ; then echo -e " \033[1mScript terminated\033[m.." else echo -e " Script terminated.." fi echo "" rm -rf $LOCK_DIR exit 1 } error (){ echo "" if $terminal ; then echo -e " [ \033[31m\033[1mError\033[m ] $*" else echo " [ Error ] $*" fi echo "" } warn (){ if $LOGGING || $terminal ; then echo "" if $terminal ; then echo -e " [ \033[33m\033[1mWarn\033[m ] $*" else echo " [ Warn ] $*" fi echo "" fi } info (){ if $LOGGING || $terminal ; then echo "" if $terminal ; then echo -e " [ \033[32m\033[1mInfo\033[m ] $*" else echo " [ Info ] $*" fi echo "" fi } ok (){ if $LOGGING || $terminal ; then echo "" if $terminal ; then echo -e " [ \033[32m\033[1mOk\033[m ] $*" else echo " [ Ok ] $*" fi echo "" fi } echo_done() { if $terminal ; then echo -e "\033[75G[ \033[32mdone\033[m ]" fi } echo_ok() { if $terminal ; then echo -e "\033[75G[ \033[32mok\033[m ]" fi } echo_failed(){ if $terminal ; then echo -e "\033[75G[ \033[1;31mfailed\033[m ]" fi } echo_skipped() { if $terminal ; then echo -e "\033[75G[ \033[33m\033[1mskipped\033[m ]" fi } trim() { local var="$*" var="${var#"${var%%[![:space:]]*}"}" # remove leading whitespace characters var="${var%"${var##*[![:space:]]}"}" # remove trailing whitespace characters echo -n "$var" } blank_line() { if $terminal ; then echo "" fi } is_number() { return $(test ! -z "${1##*[!0-9]*}" > /dev/null 2>&1); # - also possible # - #[[ ! -z "${1##*[!0-9]*}" ]] && return 0 || return 1 #return $([[ ! -z "${1##*[!0-9]*}" ]]) } ## - Check if a given array (parameter 2) contains a given string (parameter 1) ## - containsElement () { local e for e in "${@:2}"; do [[ "$e" == "$1" ]] && return 0; done return 1 } # ---------- # - Jobhandling # ---------- # - Run 'clean_up' for signals SIGHUP SIGINT SIGTERM # - trap clean_up SIGHUP SIGINT SIGTERM # ---------- # - Some checks .. # ---------- # - Running in a terminal? # - if [[ -t 1 ]] ; then terminal=true else terminal=false fi # ========== # - Begin Main Script # ========== # ---------- # - Headline # ---------- if $terminal ; then echo "" echo -e "\033[1m----------\033[m" echo -e "\033[32m\033[1mRunning script \033[m\033[1m$script_name\033[32m .. \033[m" echo -e "\033[1m----------\033[m" fi # ---------- # Read Configurations from $conf_file # ---------- if [[ -f "$conf_file" ]]; then source "$conf_file" else fatal "No configuration file '$conf_file' present!" fi if [[ -z "$LOCAL_DOMAIN" ]] ; then fatal "Local domain (Variable 'LOCAL_DOMAIN') not given!" fi if [[ -z "$HOSTNAME_PREFIX" ]] ; then fatal "Hostname prefix not given. (Variable 'HOSTNAME_PREFIX')" fi if [[ -z "$FIRST_PC_NUM" ]] ; then fatal "First PC number not given (Variable 'FIRST_PC_NUM')" fi if [[ -z "$LAST_PC_NUM" ]] ; then fatal "Last PC number not given (Variable 'LAST_PC_NUM')" fi declare -a pc_numbers_arr=() if [[ -n "$PC_NUMBERS_PRESENT" ]] ; then for _number in $PC_NUMBERS_PRESENT ; do if is_number $_number ; then pc_numbers_arr+=($_number) fi done fi declare -i nr=$FIRST_PC_NUM blank_line while [[ $nr -le $LAST_PC_NUM ]] ; do if [[ ${#pc_numbers_arr[@]} -gt 0 ]] ; then if ! containsElement $nr ${pc_numbers_arr[@]} ; then let nr++ continue fi fi ping -n -c 2 -q ${HOSTNAME_PREFIX}${nr}.$LOCAL_DOMAIN > /dev/null 2>&1 if [[ $? -eq 0 ]]; then if $terminal ; then echo -e "\t${HOSTNAME_PREFIX}${nr}.${LOCAL_DOMAIN}: \033[32m\033[1monline\033[m" else echo " ${HOSTNAME_PREFIX}${nr}.${LOCAL_DOMAIN}: online" fi else if $terminal ; then echo -e "\t${HOSTNAME_PREFIX}${nr}.${LOCAL_DOMAIN}: \033[31m\033[1moffline\033[m" else echo " ${HOSTNAME_PREFIX}${nr}.${LOCAL_DOMAIN}: offline" fi fi let nr++ done blank_line clean_up 0