#!/bin/bash
max_number_of_ssh_client=3
if [[ ! -x `which lsof` ]]; then
yum install lsof -y
RETVAL=$?
if [[ "$RETVAL" -ne "0" ]]; then
echo "ERROR: can NOT use lsof command, please check your internet connection or install lsof by manual! "
exit $RETVAL
fi
else
echo "SUCCESS: This shell script will knock out extra ssh connection "
fi
while : ; do
ssh_port=`netstat -anopt | grep sshd | awk '{print $4}' | awk -F ':' '{print $2}' | grep -v ^$ | uniq `
ssh_clients=`lsof -i:$ssh_port | grep \> | awk '{print $9}' | awk -F ':' '{print $(NF-1)}' | uniq | awk -F '>' '{print $2}'`
for ssh_client in $ssh_clients; do
number_of_ssh_client=`lsof -i:$ssh_port | grep $ssh_client | wc -l`
if [[ $number_of_ssh_client -gt $max_number_of_ssh_client ]]; then
number_pts=`w -hs | grep $ssh_client | awk '{print $2}' | awk -F '/' '{print $2}' | awk 'BEGIN {max=0} {if ($1>max) max=$1 fi} END {print max}'`
pkill -kill -t pts/$number_pts
if [[ $? -eq 0 ]]; then
echo "SUCCESS: extra connections $ssh_client@pts/$number_pts has been knocked out! "
else
echo "WARNNING: can NOT knock out extra connections! "
fi
else
echo "INFO: number of ssh connections is NORMAL! "
sleep 1
fi
done
done
Recent Comments