186 lines
4.1 KiB
Bash
Executable File
186 lines
4.1 KiB
Bash
Executable File
#!/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
|
|
|
|
|