BASH – Script that reports failed SSH connections

It will scan for servers in the network you specified and detect which servers have the SSH port open thus avoiding any non SSH running servers such as Windows (saves a huge amount of time).  In other words you can only SSH to a server by actually typing your password.

The script assumes that ssh passwordless access is not setup. In order for this to work you will need to install the sshpass utility found in the EPEL yum repository and also have nmap installed.  The script will only report on which servers a specific username failed to connect.

 

>SSH_failure
ADMINLOGIN="gmastrokostas"
ADMINPASS="SomePassword"
#It is useless to scan host ".1" and ".255". In fact it is borderline dangerous to scan 255 because the router will broadcast the command on all servers which will lead to a lot of traffic 
nmap_network=`nmap -n -P0 -p22 192.168.1.2-254 | grep Interesting | awk '{print $4}' | awk -F: '{print $1}'`

for loop in $nmap_network;
do
/usr/bin/sshpass -p $ADMINPASS ssh -oStrictHostKeyChecking=no -oCheckHostIP=no -t $ADMINLOGIN@$loop exit;
if [[ $? -gt 0 ]];
then
echo "Failed to connecit to server "$loop"" >> SSH_failure
else
echo "Success on $loop";
fi
done
echo "Here are the servers user name $ADMINLOGIN cannot login"
for loop in `awk '{print $6}' SSH_failure`; do nslookup $loop | grep name | awk '{print$4}'; done;
Share Button