From f66029fe95ffc2010b0d3e435dbebf9ef7b7f849 Mon Sep 17 00:00:00 2001 From: Christoph Date: Sat, 24 Feb 2018 13:56:55 +0100 Subject: [PATCH] Add script: 'check_ssh.sh'. --- check_ssh.sh | 185 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 185 insertions(+) create mode 100755 check_ssh.sh diff --git a/check_ssh.sh b/check_ssh.sh new file mode 100755 index 0000000..5485b16 --- /dev/null +++ b/check_ssh.sh @@ -0,0 +1,185 @@ +#!/usr/bin/env bash + +sshd_binary="$(which sshd)" + +check_string_ps="$sshd_binary" + +LOCK_DIR=`mktemp -d` + + +#--------------------------------------- +#----------------------------- +# Base Function(s) +#----------------------------- +#--------------------------------------- + +fatal(){ + echo "" + echo -e " [ Fatal ] $*" + echo "" + echo -e "\tScript terminated.." + echo "" + rm -rf $LOCK_DIR + exit 1 +} + +error (){ + echo "" + echo -e " [ Error ] $*" + echo "" +} + +warn (){ + echo "" + echo -e " [ Warn ] $*" + echo "" +} + +info (){ + echo "" + echo -e " [ Info ] $*" + echo "" +} + +ok (){ + echo "" + echo -e " [ Ok ] $*" + echo "" +} + +trim() { + local var="$*" + var="${var#"${var%%[![:space:]]*}"}" # remove leading whitespace characters + var="${var%"${var##*[![:space:]]}"}" # remove trailing whitespace characters + echo -n "$var" +} + + +#--------------------------------------- +#----------------------------- +# Check some prerequisites +#----------------------------- +#--------------------------------------- + +# - Running in a terminal? +# - +if [[ -t 1 ]] ; then + terminal=true + LOGGING=true +else + terminal=false + LOGGING=false +fi + +# - Is SSH Service installed ? +# - +if [[ -z "$sshd_binary" ]]; then + fatal "$(basename $0): SSH Service seems NOT to be installed" +else + check_string_ps="$sshd_binary" +fi + + +# - Systemd supported ? +# - +systemd=$(which systemd) +systemctl=$(which systemctl) + +systemd_supported=false +if [[ -n "$systemd" ]] && [[ -n "$systemctl" ]] ; then + systemd_supported=true +fi + +SSHD_SERVICE_FILE= +SSHD_SYSV_INIT_SCRIPT= + +if $systemd_supported ; then + if systemctl -t service list-unit-files \ + | grep -e "^ssh" \ + | grep -q -E "(enabled|disabled)" 2> /devnull ; then + + SSHD_SERVICE_FILE="$(systemctl -t service list-unit-files | grep -e "^ssh" | awk '{print$1}' | head -1)" + fi +fi + +if [[ -z "$SSHD_SERVICE_FILE" ]]; then + if [[ -x "/etc/init.d/ssh" ]]; then + SSHD_SYSV_INIT_SCRIPT="/etc/init.d/ssh" + elif [[ -x "/etc/init.d/sshd" ]]; then + SSHD_SYSV_INIT_SCRIPT="/etc/init.d/sshd" + fi +fi + +if [[ -z "$SSHD_SERVICE_FILE" ]] && [[ -z "$SSHD_SYSV_INIT_SCRIPT" ]] ; then + fatal 'Neither an init-script nor a service file for SSH found!' +fi + + +#--------------------------------------- +#----------------------------- +# Check if SSH service is running +#----------------------------- +#--------------------------------------- + +if $LOGGING ; then + echo -e "\n Check if SSH service is running.." + echo -e " =================================" +fi +if ! ps -e f | grep -E "[[:digit:]]\ ${check_string_ps}" | grep -v grep > /dev/null ; then + error "SSH service seems to be down! Trying to restart service now.." + + if [[ -n "$SSHD_SERVICE_FILE" ]] ; then + $systemctl daemon-reload > /dev/null 2> ${LOCK_DIR}/err_msg.log + if [[ $? -ne 0 ]]; then + error "$(cat ${LOCK_DIR}/err_msg.log)" + fi + sleep 2 + $systemctl stop $SSHD_SERVICE_FILE > /dev/null 2> ${LOCK_DIR}/err_msg.log + if [[ $? -ne 0 ]]; then + error "$(cat ${LOCK_DIR}/err_msg.log)" + fi + sleep 10 + $systemctl start $SSHD_SERVICE_FILE > /dev/null 2> ${LOCK_DIR}/err_msg.log + if [[ $? -ne 0 ]]; then + error "$(cat ${LOCK_DIR}/err_msg.log)" + fi + else + $SSHD_SYSV_INIT_SCRIPT stop > /dev/null 2>&1 + if [[ $? -ne 0 ]]; then + error "Stopping SSH service failed!" + fi + sleep 10 + $SSHD_SYSV_INIT_SCRIPT start > /dev/null 2>&1 + if [[ $? -ne 0 ]]; then + error "Starting SSH service failed!" + fi + fi + + declare -i counter=0 + PID=$(ps -e f | grep -E "[[:digit:]]\ ${check_string_ps}"| grep -v grep | awk '{print$2}') + while [[ "X${PID}" = "X" ]]; do + sleep 1 + PID=$(ps -e f | grep -E "[[:digit:]]\ ${check_string_ps}"| grep -v grep | awk '{print$2}') + if [[ $counter -gt 10 ]]; then + break + else + ((counter++)) + fi + done + + if [[ "X${PID}" = "X" ]] ; then + error "Restarting SSH service failed!" + else + ok "SSH service is up and running." + fi + +else + if $LOGGING ; then + ok "SSH service is up and running." + fi +fi + +rm -rf $LOCK_DIR +exit 0 + +