#!/usr/bin/env bash do_with_root() { if [[ `whoami` = 'root' ]]; then $* elif [[ -x /bin/sudo || -x /usr/bin/sudo ]]; then echo "sudo $*" sudo $* else echo -e "${BOrange}Please run this script as root.${Color_Off}" exit 1 fi } EXIT1() { clear exit 0 } is_service_running() { local result=$(ssh root@${1} pgrep $2 | wc -l); echo $result } containsElement () { local e match="$1" shift for e; do [[ "$e" == "$match" ]] && return 0; done return 1 } function join_by { local IFS="$1"; shift; echo "$*"; } IPprefix_by_netmask () { c=0 x=0$( printf '%o' ${1//./ } ) while [ $x -gt 0 ]; do let c+=$((x%2)) 'x>>=1' done echo $c ; } IP_ALLOWED() { ALLOWED_NETWORKS="10.10.0.0/16 10.5.0.0/20 192.168.5.0/24" grepcidr "${ALLOWED_NETWORKS}" <(echo "${1}") >/dev/null && echo "true" || echo "false" } if type apt &>/dev/null; then ATYPE="apt" if [ "$EUID" -ne 0 ]; then APTFUNC='sudo apt' else APTFUNC='apt' fi elif type apt-get &>/dev/null; then ATYPE="apt-get" if [ "$EUID" -ne 0 ]; then APTFUNC='sudo apt-get' else APTFUNC='apt-get' fi elif type yum &>/dev/null; then ATYPE="yum" if [ "$EUID" -ne 0 ]; then APTFUNC='sudo yum' else APTFUNC='yum' fi else ATYPE="unknown" #exit 1 fi redhat_derivative () { local FILE=/etc/redhat-release grep -i 'red.*hat.*enterprise.*linux' $FILE 2>&1 > /dev/null && { echo DERIVATIVE=rhel; return; } grep -i 'red.*hat.*linux' $FILE 2>&1 > /dev/null && { echo DERIVATIVE=rh; return; } grep -i 'cern.*e.*linux' $FILE 2>&1 > /dev/null && { echo DERIVATIVE=cel; return; } grep -i 'scientific linux cern' $FILE 2>&1 > /dev/null && { echo DERIVATIVE=slc; return; } grep -i 'centos' $FILE 2>&1 > /dev/null && { echo DERIVATIVE=centos; return; } echo DERIVATIVE=unknown } detect_os () { #local -n OSinfo="$1" || return 1 declare -A OSinfo OSinfo[MACH]=`uname -m` if [ -f /etc/redhat-release ] ; then echo DISTRIBUTION=redhat local FILE=/etc/redhat-release grep -i 'red.*hat.*enterprise.*linux' $FILE 2>&1 > /dev/null && { echo DERIVATIVE=rhel; return; } grep -i 'red.*hat.*linux' $FILE 2>&1 > /dev/null && { echo DERIVATIVE=rh; return; } grep -i 'cern.*e.*linux' $FILE 2>&1 > /dev/null && { echo DERIVATIVE=cel; return; } grep -i 'scientific linux cern' $FILE 2>&1 > /dev/null && { echo DERIVATIVE=slc; return; } grep -i 'centos' $FILE 2>&1 > /dev/null && { echo DERIVATIVE=centos; return; } #echo DERIVATIVE=unknown echo RELEASE=`tr -d 'a-zA-Z [](){}' < /etc/redhat-release` elif [ -s /etc/slackware-version ]; then echo DISTRIBUTION="slackware" elif [ -f /etc/SUSE-release ] ; then # TODO - not tested echo DISTRIBUTION=suse echo PSUEDONAME=`cat /etc/SUSE-release | tr "\n" ' '| sed s/VERSION.*//` echo REV=`cat /etc/SUSE-release | tr "\n" ' ' | sed s/.*=\ //` echo VERSION=`cat /etc/SuSE-release | grep 'VERSION' | sed -e 's#[^0-9]##g'` elif [ -f /etc/mandrake-release ] ; then # TODO - not tested echo DISTRIBUTION=mandrake echo PSUEDONAME=`cat /etc/mandrake-release | sed s/.*\(// | sed s/\)//` echo REV=`cat /etc/mandrake-release | sed s/.*release\ // | sed s/\ .*//` echo FAMILY=rh elif [ -f /etc/debian_version ] ; then OSinfo[DISTRIBUTION]=debian if which lsb_release 2>&1 > /dev/null ; then OSinfo[DERIVATIVE]=`lsb_release --id --short 2> /dev/null` OSinfo[RELEASE]=`lsb_release --release --short 2> /dev/null` OSinfo[CODENAME]=`lsb_release --codename --short 2> /dev/null` else OSinfo[DERIVATIVE]=unknown OSinfo[RELEASE]=`cat /etc/debian_version` OSinfo[CODENAME]=unknown fi elif [ -f /etc/UnitedLinux-release ]; then echo DISTRIBUTION="united" echo VERSION=`cat /etc/UnitedLinux-release` elif [ -r /etc/init.d/functions.sh ]; then # TODO - not tested source /etc/init.d/functions.sh [ zz`type -t ebegin 2>/dev/null` == "zzfunction" ] && echo DISTRIBUTION="gentoo" fi #declare -p OSinfo echo "${OSinfo[@]}" } ENTER2CONTINUE(){ echo "Press ENTER to continue" while read -s answer; do if [ -z "$answer" ]; then break fi done } SHOW_TIME() { num=$1; min=0; hour=0; day=0 if((num>59));then ((sec=num%60)); ((num=num/60)) if((num>59));then ((min=num%60)); ((num=num/60)) if((num>23));then ((hour=num%24)); ((day=num/24)) else ((hour=num)); fi; else ((min=num)); fi; else ((sec=num)); fi t='' if [ $day != 0 ]; then if((min>29)); then ((hour=hour+1)) if((hour>23)); then ((hour=0)) ((day=day+1)) fi fi t+="${day} days"; if [ $hour != 0 ]; then t+=", "; fi fi if [ $hour != 0 ]; then t+="${hour} hours" fi if [ $min != 0 ] && [ $day = 0 ]; then if [ $hour != 0 ]; then t+=", "; fi t+="${min} mins" fi if [ "${2}" == "s" ]; then if [ $hour != 0 ] || [ $min != 0 ]; then t+=", "; fi t+="${sec} secs" fi echo ${t} } DIVIDER(){ if [ -z ${2+x} ]; then local clr='yellow' else local clr=$2 fi if [ -z ${3+x} ]; then local length=70 else local length=$3 fi local c=0; local dashes=''; until [ $c = ${length} ]; do local dashes="${dashes}-"; local c=`expr $c + 1`; done echo -e "${idsCL[${clr~}]}${dashes}${idsCL[Default]}" if [ "$1" = true ]; then echo "" fi } service_exists() { local n=$1 if [[ $(systemctl list-units --all -t service --full --no-legend "$n.service" | sed 's/^\s*//g' | cut -f1 -d' ') == $n.service ]]; then return 0 else return 1 fi } CHECK_HOST(){ if [ ! -z ${1+x} ]; then ping -c 3 ${1} > /dev/null 2>&1 if [ $? -ne 0 ]; then echo false else echo true fi else echo false fi } PUSH_TO_MOBILE(){ MESSAGE=$1 TITLE=$2 MSGSOUND=classical if [ "$3" = "2" ]; then PRIORITY="${3}&retry=60&expire=600" MSGSOUND=alien elif [ "$3" = "1" ]; then PRIORITY="${3}" MSGSOUND=siren elif [ "$3" != "" ]; then PRIORITY=$3 else PRIORITY=0 fi wget https://api.pushover.net/1/messages.json --post-data="token=$PUSHOVER_APP_TOKEN&user=$PUSHOVER_USER_TOKEN&message=$MESSAGE&title=$TITLE&priority=$PRIORITY&sound=$MSGSOUND" -qO- > /dev/null 2>&1 & } declare -A abet abet[0]="a" abet[1]="b" abet[2]="c" abet[3]="d" abet[4]="e" abet[5]="f" abet[6]="g" abet[7]="h" abet[8]="i" abet[9]="i" abet[10]="k" abet[11]="l" abet[12]="m" abet[13]="n" abet[14]="o" abet[15]="p" abet[16]="q" abet[17]="r" abet[18]="s" abet[19]="v" abet[20]="u" abet[21]="v" abet[22]="w" abet[23]="x" abet[24]="y" abet[25]="z"