SSH Connectivity script

Hi all,

I have developed a script to list out the servers that is not able to connect to the remote host.

The problem with this script is it, if the remote server ask for a password it gets struck there. It works only if the server name is invalid

Ex: Lets say ssh -q test@test "exit" < /dev/null the script will throw that test server is invalid.

Lets say now UserA and ServerA is valid but when we ssh to the server, it ask for a password and script hangs

ssh -q UserA@ServerA "exit" < /dev/null

Code

check_ssh()
{
        ssh -q $1@$2 "exit" < /dev/null
        [ $? -ne "0" ] && return 1 || return 0
}

INP_FILE=Config.ini

for line in $(grep -v "^#" $INP_FILE | cut -d',' -f2,5 | sort | uniq)
do
        myUser=$(echo $line| cut -d',' -f2)
        myServer=$(echo $line| cut -d',' -f1)
        check_ssh $myUser $myServer
        [ $? -eq "1" ] && echo "Please check !!! $myServer - $myUser"
done
echo " *** END *** "
exit

i understand you basic idea is to use ssh do a dummy connect to figure out if an sshd is running on the other side ?

why do you not use nmap ? it simply cheks if there is a service available,

I will check that option, any other ??

... but what are you exacly testing for?
Whether you can connect or not?
Whether sshd is running or not?
If you can connect to the hosts without a pasword using ssh keys?

I want to check if we can ssh to a remote host and execute our statements. This is just a function block which checks for whether we can ssh into a server before executing our statements.

I just want to capture the server names that we are not able to ssh, so that we can skip those servers from executing the command.

Hope this makes clear !!

check_ssh()
{
        ssh -q -o "BatchMode=yes" $1@$2 "exit" < /dev/null
        [ $? -ne "0" ] && return 1 || return 0
}

INP_FILE=Config.ini

for line in $(grep -v "^#" $INP_FILE | cut -d',' -f2,5 | sort | uniq)
do
        myUser=$(echo $line| cut -d',' -f2)
        myServer=$(echo $line| cut -d',' -f1)
        check_ssh $myUser $myServer
        [ $? -eq "1" ] && echo "Please check !!! $myServer - $myUser"
done
echo " *** END *** "
exit