Reputation: 18411
I have connected two servers using SSH key exchange. Now I am able to login into remote server without password.
Now , I want to validate if connection is really working without password .How can I do that ?
I tried following approach where SSH should timeout after 10 seconds, I will capture its Return code. but it never does. Once it timeout. It remain on the password prompt.
ssh -o ServerAliveInterval=10 -o ConnectTimeout=15 user@ip.ip.ip.ip exit
if [ "$?" -ne 0 ];then
echo "SSH keys no more working,you need to initiate keys exchange again!!"
exit 1;
fi
However,when SSH keys are not working then and it never time-out and remain on password prompt. So cannot capture return code.
ssh -o ServerAliveInterval=10 -o ConnectTimeout=15 user@ip.ip.ip.ip exit
Enter Windows password:
When I provide IP address which is not pingable then this approch works.
ssh -o ServerAliveInterval=10 -o ConnectTimeout=15 user@ip1.ip1.ip1.ip1 exit
ssh: connect to host ip1.ip1.ip1.ip1 port 22: Connection timed out
Need help in determining if SSH connection is password-less working or not.
Upvotes: 1
Views: 771
Reputation: 26006
You can disable password authentication using PasswordAuthentication=no
. It will fail immediately, when the pubkey authentication fail.
ssh -o BatchMode=yes -o ConnectTimeout=15 user@ip.ip.ip.ip exit
Edit: Using BatchMode
should certainly work.
Upvotes: 1