How to determine if user has ssh keys in a perl script

Viewed 174

I have the following script in tcsh that looks like this:

df=`ssh $some_server 2>/dev/null df $2 -k` 
echo $df

Perl script runs this tcsh script and I have the two following questions:

  1. What does the first command do?
  2. How can I determine if I have the ssh keys, before running this command (from the perl script for example, or the tcsh script?

As I understand, If I don't have the ssh keys, it will ask for the password. I would like to print a basic warning to the user if he doesn't have the ssh keys.

1 Answers

When ssh fails to establish a connection to the server it doesn't tell you the reason, but you can run it in verbose mode and analyze the output afterwards.

For instance, use the following command:

ssh -vvv -i $path_to_key -o BatchMode=yes \
    -l $remote_user $remote_host echo ok 2>error.log

And afterwards, if it fails, check error.log for the cause of the problem.

Note that the debugging output may change between different ssh versions, so any solution that depends on it is never going to be very robust. You should consider it just a hint.

Related