3

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.

2
  • 1
    Running a perl program that shells out to run ssh manually and try to understand the error conditions will give you a lot of headaches. At least have a look at the system() call where you can pass a list of parameters, so that you do not use the shell. Modules like IPC::Run may also help you write this more cleanly, specially when needing to deal with STDOUT/STDERR. Also, you have various Perl libraries that gives you SSH protocol features, either as rewritten or as a layer on top of it.Have a look at them, even if the question "do we have the proper keys" is complicated(depends on config) Commented Aug 29, 2018 at 20:15
  • 3
    You can use -o BatchMode=yes to check if passwordless login is possible. For example: ssh -o BatchMode=yes userName@server true >/dev/null 2>&1 || echo "Passwordless login is not possible". See this answer for more information Commented Aug 29, 2018 at 20:40

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.