370 lines
11 KiB
Bash
Executable File
370 lines
11 KiB
Bash
Executable File
#!/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
|
|
read -sp "[Press ENTER to continue]"
|
|
echo -e "\e[1A\n\e[0K\r\n"
|
|
}
|
|
ANYKEY2CONTINUE(){
|
|
echo
|
|
read -n1 -sp "[Press any key to continue]"
|
|
echo -e "\e[1A\n\e[0K\r\n"
|
|
}
|
|
ROUND_NUMBER(){
|
|
[ "${2}" != "" ] && decimals_to_round=${2} || decimals_to_round=0
|
|
# echo $(echo $1 | awk '{print int($1+0.5)}')
|
|
case "${decimals_to_round}" in
|
|
4) echo $(echo $1 | awk '{printf "%.4f\n", $1}');;
|
|
3) echo $(echo $1 | awk '{printf "%.3f\n", $1}');;
|
|
2) echo $(echo $1 | awk '{printf "%.2f\n", $1}');;
|
|
1) echo $(echo $1 | awk '{printf "%.1f\n", $1}');;
|
|
*) echo $(echo $1 | awk '{printf "%.0f\n", $1}');;
|
|
esac
|
|
}
|
|
function ROUNDHALVES () {
|
|
[[ $1 =~ ^([\+-]?)([0-9]*)\.([0-9]+)$ ]] || { printf "%s\n" "$1" && return; }
|
|
s=${BASH_REMATCH[1]}
|
|
a=${BASH_REMATCH[2]}
|
|
(( b = 1${BASH_REMATCH[3]} * 2 ))
|
|
[[ $b =~ ^([0-9][0-9])[0-9]*$ ]] && b=${BASH_REMATCH[1]}
|
|
(( b < 25 ? (b = 0) : b >= 35 ? (a += 1, b = 0) : (b = 5) ))
|
|
printf "%s%s.%d\n" "$s" "$a" "$b"
|
|
}
|
|
IDS_NUMBER_FORMAT(){
|
|
VAL=${1}
|
|
[ "${2}" == "" ] && DEC=2 || DEC=${2}
|
|
ShowDecAlways=${3}
|
|
# ShowDecAlways=yes
|
|
|
|
if [ "${ShowDecAlways}" != "" ]; then
|
|
echo `printf "%'.${DEC}f\n" ${VAL}`
|
|
|
|
elif [ "${DEC}" == "0" ]; then
|
|
echo `printf "%'.0f\n" ${VAL}`
|
|
|
|
elif [[ "${VAL}" == *"."* ]] && [[ "${VAL}" != *".0"* ]]; then
|
|
echo `printf "%'.${DEC}f\n" ${VAL}`
|
|
|
|
else
|
|
echo `printf "%'.0f\n" ${VAL}`
|
|
|
|
fi
|
|
}
|
|
SHOW_TIME() {
|
|
num=$(ROUND_NUMBER ${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";
|
|
[ $hour != 0 ] && t+=", "
|
|
fi
|
|
if [ $hour != 0 ]; then
|
|
t+="${hour} hours"
|
|
fi
|
|
if [ $min != 0 ] && [ $day = 0 ]; then
|
|
[ $hour != 0 ] && t+=", "
|
|
t+="${min} mins"
|
|
fi
|
|
if [ ${sec} -ne 0 ] && ([ "${2}" == "s" ] || ([ $day -eq 0 ] && [ $hour -eq 0 ] && [ $min -eq 0 ])); then
|
|
([ $hour -ne 0 ] || [ $min -ne 0 ]) && t+=", "
|
|
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
|
|
[ "${clr}" == "." ] && echo ${dashes} || echo -e "${idsCL[${clr~}]}${dashes}${idsCL[Default]}"
|
|
[ "${1}" == "true" ] && echo
|
|
}
|
|
DIVIDER_C(){
|
|
[ -z ${2+x} ] && clr='yellow' || clr=$2
|
|
[ -z ${3+x} ] && length=70 || length=$3
|
|
c=0; dashes=''; until [ $c = ${length} ]; do dashes="${dashes}-"; c=`expr $c + 1`; done
|
|
[ "${clr}" == "." ] && echo ${dashes} || echo -e "${idsCL[${clr}]}${dashes}${idsCL[Default]}"
|
|
[ "$1" = true ] && echo
|
|
}
|
|
|
|
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
|
|
if [ "$(dpkg-query -W --showformat='${Status}\n' nmap 2>/dev/null | grep "install ok installed")" != "" ]; then
|
|
echo found
|
|
[ "$(nmap ${1} -p 22,80,443 -n | grep open)" != "" ] || [ "$(nmap -sU ${1} -p 161 -n | grep open)" != "" ] && echo true || echo false
|
|
else
|
|
echo not found
|
|
ping -c 3 ${1} > /dev/null 2>&1
|
|
[ $? -ne 0 ] && echo false || echo true
|
|
fi
|
|
else
|
|
echo false
|
|
fi
|
|
}
|
|
|
|
PUSH_TO_MOBILE(){
|
|
MESSAGE=$1
|
|
TITLE=$2
|
|
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
|
|
if [ "${4}" != "" ]; then
|
|
MSGSOUND=${4}
|
|
elif [ "${MSGSOUND}" = "" ]; then
|
|
MSGSOUND=classical
|
|
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 &
|
|
# echo "token=${PUSHOVER_APP_TOKEN}&user=${PUSHOVER_USER_TOKEN}&message=${MESSAGE}&title=${TITLE}&priority=${PRIORITY}&sound=${MSGSOUND}"
|
|
# 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}"
|
|
}
|
|
|
|
|
|
# Dynamically create an array by name
|
|
function arr() {
|
|
[[ ! "$1" =~ ^[a-zA-Z_]+[a-zA-Z0-9_]*$ ]] && { echo "Invalid bash variable" 1>&2 ; return 1 ; }
|
|
declare -g -a $1=\(\)
|
|
}
|
|
|
|
# Insert incrementing by incrementing index eg. array+=(data)
|
|
function arr_insert() {
|
|
[[ ! "$1" =~ ^[a-zA-Z_]+[a-zA-Z0-9_]*$ ]] && { echo "Invalid bash variable" 1>&2 ; return 1 ; }
|
|
declare -p "$1" > /dev/null 2>&1
|
|
[[ $? -eq 1 ]] && { echo "Bash variable [${1}] doesn't exist" 1>&2 ; return 1 ; }
|
|
declare -n r=$1
|
|
r[${#r[@]}]=$2
|
|
}
|
|
|
|
# Update an index by position
|
|
function arr_set() {
|
|
[[ ! "$1" =~ ^[a-zA-Z_]+[a-zA-Z0-9_]*$ ]] && { echo "Invalid bash variable" 1>&2 ; return 1 ; }
|
|
declare -p "$1" > /dev/null 2>&1
|
|
[[ $? -eq 1 ]] && { echo "Bash variable [${1}] doesn't exist" 1>&2 ; return 1 ; }
|
|
declare -n r=$1
|
|
r[$2]=$3
|
|
}
|
|
|
|
# Get the array content ${array[@]}
|
|
function arr_get() {
|
|
[[ ! "$1" =~ ^[a-zA-Z_]+[a-zA-Z0-9_]*$ ]] && { echo "Invalid bash variable" 1>&2 ; return 1 ; }
|
|
declare -p "$1" > /dev/null 2>&1
|
|
[[ $? -eq 1 ]] && { echo "Bash variable [${1}] doesn't exist" 1>&2 ; return 1 ; }
|
|
declare -n r=$1
|
|
echo ${r[@]}
|
|
}
|
|
|
|
# Get the value stored at a specific index eg. ${array[0]}
|
|
function arr_at() {
|
|
[[ ! "$1" =~ ^[a-zA-Z_]+[a-zA-Z0-9_]*$ ]] && { echo "Invalid bash variable" 1>&2 ; return 1 ; }
|
|
declare -p "$1" > /dev/null 2>&1
|
|
[[ $? -eq 1 ]] && { echo "Bash variable [${1}] doesn't exist" 1>&2 ; return 1 ; }
|
|
[[ ! "$2" =~ ^(0|[-]?[1-9]+[0-9]*)$ ]] && { echo "Array index must be a number" 1>&2 ; return 1 ; }
|
|
declare -n r=$1
|
|
local max=${#r[@]}
|
|
# Array has items and index is in range
|
|
if [[ $max -gt 0 && $i -ge 0 && $i -lt $max ]]
|
|
then
|
|
echo ${r[$2]}
|
|
fi
|
|
}
|
|
|
|
# Get the value stored at a specific index eg. ${array[0]}
|
|
function arr_count() {
|
|
[[ ! "$1" =~ ^[a-zA-Z_]+[a-zA-Z0-9_]*$ ]] && { echo "Invalid bash variable " 1>&2 ; return 1 ; }
|
|
declare -p "$1" > /dev/null 2>&1
|
|
[[ $? -eq 1 ]] && { echo "Bash variable [${1}] doesn't exist" 1>&2 ; return 1 ; }
|
|
declare -n r=$1
|
|
echo ${#r[@]}
|
|
}
|
|
|
|
GET_CPU_USAGE(){
|
|
CPU_USAGE=$(awk '{u=$2+$4; t=$2+$4+$5; if (NR==1){u1=u; t1=t;} else print ($2+$4-u1) * 100 / (t-t1) "%"; }' <(grep 'cpu ' /proc/stat) <(sleep 1;grep 'cpu ' /proc/stat) | sed -e 's/%//g')
|
|
echo ${CPU_USAGE}
|
|
}
|
|
|
|
|
|
|
|
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"
|