I am using KSH and I need to check whether the remote host has been configured with ssh public key. Is there any way we can check inside a script?
man test
host=host
ssh $host "echo 2>&1"
test $? -eq 0 && echo $host OK || echo $host NOK
Of course, that's a Useless Use of Test $?
ssh $host "echo 2>&1" && echo $host OK || echo $host NOK
Absence of a public key is by no means the only way the ssh command can fail, so this is an approximation at best; but maybe it's sufficient.
This time I use test only as example to show him how to use the exit status.
Great thanks , This is helpfull.
I got a problem here
I removed the public key config on the ssh server and then tried
ssh root@$host "echo 2>&1" && echo "OK" || echo "NOK"
But it came out asking password . which I dont want , what I wanted is even if its asked passwd its should come out with a non-zero exit satus.
--------------------------------------------------------------------
Now let say how I got it implemented , even though a twisted way
if [ -f $HOME/.ssh2/ssh2_config ]
then
mv $HOME/.ssh2/ssh2_config $HOME/.ssh2/ssh2_config.bkp
echo "QuietMode yes\nBatchMode yes\nConnectTimeout 4" > $HOME/.ssh2/ssh2_config
else
echo "QuietMode yes\nBatchMode yes\nConnectTimeout 4" > $HOME/.ssh2/ssh2_config
fi
ssh -l root $R_HOSTNAME "date" > /dev/null
CONN_STATUS=$?
And if the RC is 66 , then it mean it had asked passwd and not got passwd within in the timeout period. Andas usual 0 as success.
ssh -q -o "BatchMode=yes" user@host "echo 2>&1" && echo "OK" || echo "NOK"
...will solve your problem, by running quietly and in batch mode (no user present to enter a password). See man ssh_config for more details. Obviously the two echo statements can be replaced with any action you like...