10

I have a program like this:

ssh -q [email protected] exit
echo "output value  -->$?"

In the above code, I am trying to SSH to the remote server, and tries to check if I can connect or not. I have few servers, which is password less is activated and few servers for which passwords are still not yet deactivated. So my concern, if there is a password, it will get stuck in the same screen asking for the password and it will be there for infinite time, without Exit logic.

Question : How to implement the timers for the above code, and if it stays in the same screen asking for the password. and exit with some error code

2) When I execute the above code, i get the following error codes:

127 -- > I guess its for success
225 -- > for any error.

are there any other error codes other than above?

6
  • eldos.com/documentation/sbb/documentation/…, found one link, Commented Apr 3, 2013 at 11:22
  • Don't bang out in case password is not supplied & key based auth is not setup. Use expect scripts. (Search for ssh+expect on google/stackoverflow, for a template script.) Commented Apr 3, 2013 at 11:23
  • 4
    use -o PasswordAuthentication=no Commented Apr 3, 2013 at 11:28
  • Thanks @Vorsprung, password issue is resolved, if you have the info about the error codes of SSH command, please share Commented Apr 3, 2013 at 11:37
  • @gmhk - In FreeBSD, you can man sysexits to see the recommended exit codes for programs. I don't know if they differ in other operating system. Generally, an exit value of 0 means "success", and anything else is some kind of failure. Commented Apr 3, 2013 at 11:40

2 Answers 2

17

You could wrap the call to ssh using the timeout command. The timeout command exits with code 124 if a timeout occurs.

timeout 10s ssh -q [email protected] exit
if [ $? -eq 124 ]; then
    echo "Timeout out"
fi

Or, as Vorsprung has commented on your question (as I was looking up the man page!):

ssh -oPasswordAuthentication=no -q [email protected] exit

which will disallow interactive password authentication. You'd then have to check the exit code.

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

2 Comments

ssh -oPasswordAuthentication=no -q [email protected] exit --> this worked
@gmhk - don't forget to click the checkmark for the answer that helped you the most.
3

It sounds like the question is asking about 'batch mode' processing against a number of hosts at once (e.g., in a loop). The following ssh command will simply fail if PKA is not configured for a given host (public key authentication -- the "password-less" login mentioned the original question), so it is good for scripts that you just want to continue running regardless of problems connecting to one or two hosts. It also won't bother with all the queries about "are you sure you want to continue connecting" to previously unknown hosts (or you can just add StrictHostKeyChecking no to your ~/.ssh/config for that). For example,

for host in host1 host2 host3  # etc
do
  ssh -o BatchMode=yes -o StrictHostKeyChecking=no -o ConnectTimeout=5 \
      user@$host  'uptime' || echo "problem: host=$host"
done

You'll have to check your (client) ssh man page to see if these options (BatchMode, ConnectTimeout) are supported, though.

1 Comment

note that nowadays it's possible to use -o StrictHostKeyChecking=accept-new instead of no, for slightly better security.

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.