#!/bin/bash
#### NO SSH CONNECTION 1FUNCATION AND SSH HAI TO 2FUNCATION RUN.
# Check for missing parameters
if [[ -z "$1" ]] || [[ -z "$2" ]] || [[ -z "$3" ]] || [[ -z "$4" ]]; then
echo "Missing parameters! Syntax: ./check_ssh.sh <HOSTNAME> <PORT_NO> <WARNING_THRESHOLD> <CRITICAL_THRESHOLD>"
exit 2
fi
host=$1
port=$2
Warning=$3
Critical=$4
########### check Port ############################################
netstatus=$(netstat -nlp | grep :$port)
if [[ $? == 1 ]]; then
echo "CRITICAL - PORT NOT ESTABLISHED"
exit 2
fi
############ ESTABLISHED connection ###############################
command=$(netstat -alntp | grep :$port | grep ESTABLISHED | awk '{print $5}' | cut -d ":" -f1 | sort |uniq)
check_command=$(echo $command | wc -l)
if [[ $command = "" ]];then
echo "OK - ESTABLISHED"
exit 0
fi
if [ -f /tmp/.user_count ];then
$(rm -rf /tmp/.user_count)
fi
######### looping ############################
if [ $check_command -gt 0 ];then
loop_ip=$(for ip in $command;
do
command1=$(who | grep $ip | awk {'print $1'})
com=$(who | grep $ip | awk {'print $1'} >> /tmp/.user_count)
echo -e "$(echo $command1) $ip"
done)
fi
##############################################
if [ -f /tmp/.user_count ];then
count=$(wc -l < /tmp/.user_count)
fi
# Connection is Establish---OK;
if [[ "$count"<="$Warning" ]]; then
echo "OK - ESTABLISHED/Total_user $count "
echo "$loop_ip"
exit 0
fi
# Connection is WARNING---WARNING;
if [[ "$count"=>"$Warning" ]] && [[ "$count"<="$Critical" ]]; then
echo "WARNING - ESTABLISHED/Total_user $count"
echo "$loop_ip "
exit 1
fi
# Connection is CRITICAL---CRITICAL;
if [[ "$count"=>"$Critical" ]]; then
echo "CRITICAL - ESTABLISHED/Total_user $count"
echo "$loop_ip "
exit 2
fi
=====================================
./check_ssh.sh localhost 22 4 5
#### NO SSH CONNECTION 1FUNCATION AND SSH HAI TO 2FUNCATION RUN.
# Check for missing parameters
if [[ -z "$1" ]] || [[ -z "$2" ]] || [[ -z "$3" ]] || [[ -z "$4" ]]; then
echo "Missing parameters! Syntax: ./check_ssh.sh <HOSTNAME> <PORT_NO> <WARNING_THRESHOLD> <CRITICAL_THRESHOLD>"
exit 2
fi
host=$1
port=$2
Warning=$3
Critical=$4
########### check Port ############################################
netstatus=$(netstat -nlp | grep :$port)
if [[ $? == 1 ]]; then
echo "CRITICAL - PORT NOT ESTABLISHED"
exit 2
fi
############ ESTABLISHED connection ###############################
command=$(netstat -alntp | grep :$port | grep ESTABLISHED | awk '{print $5}' | cut -d ":" -f1 | sort |uniq)
check_command=$(echo $command | wc -l)
if [[ $command = "" ]];then
echo "OK - ESTABLISHED"
exit 0
fi
if [ -f /tmp/.user_count ];then
$(rm -rf /tmp/.user_count)
fi
######### looping ############################
if [ $check_command -gt 0 ];then
loop_ip=$(for ip in $command;
do
command1=$(who | grep $ip | awk {'print $1'})
com=$(who | grep $ip | awk {'print $1'} >> /tmp/.user_count)
echo -e "$(echo $command1) $ip"
done)
fi
##############################################
if [ -f /tmp/.user_count ];then
count=$(wc -l < /tmp/.user_count)
fi
# Connection is Establish---OK;
if [[ "$count"<="$Warning" ]]; then
echo "OK - ESTABLISHED/Total_user $count "
echo "$loop_ip"
exit 0
fi
# Connection is WARNING---WARNING;
if [[ "$count"=>"$Warning" ]] && [[ "$count"<="$Critical" ]]; then
echo "WARNING - ESTABLISHED/Total_user $count"
echo "$loop_ip "
exit 1
fi
# Connection is CRITICAL---CRITICAL;
if [[ "$count"=>"$Critical" ]]; then
echo "CRITICAL - ESTABLISHED/Total_user $count"
echo "$loop_ip "
exit 2
fi
=====================================
./check_ssh.sh localhost 22 4 5
No comments:
Post a Comment