Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a list of servers:

cat list.txt servera serverb

How can I check that I can log in via ssh to them or not? I mean by default I should be able to log in via ssh key auth.., so in short, I need a solution that sorts the lines (servers) in the list.txt like this:

  • servers that I can log in via ssh key
  • servers that prompts for password (of course password is unknown..)
  • servers that are unreachable
share|improve this question
with the help of ssh and ping commands u may achieve it – Balaswamy vaddeman Jan 27 '12 at 5:29
ping isn't really a good indicator. There are a lot of setups that allow ssh connections but don't reply to pings. – Mat Jan 27 '12 at 7:43
up vote 20 down vote accepted

You can do that with a combination of the BatchMode option and "parsing" the output. (ssh always returns 255 if it fails to connect for whatever reason, so you can't use the return code to distinguish between types of failures.)

With BatchMode on, no password prompt or other interaction is attempted, so a connect that requires a password will fail. (I also put a ConnectTimeout in there which should be adjusted to fit your needs. And picked really bad filenames.)

#! /bin/bash

rm good no_auth other
while read ip host ; do
    status=$(ssh -o BatchMode=yes -o ConnectTimeout=5 $ip echo ok 2>&1)
    if [[ $status == ok ]] ; then
        echo $ip $host >> good
    elif [[ $status == "Permission denied"* ]] ; then
        echo $ip $host $status >> no_auth
        echo $ip $host $status >> other
done < list.txt

You could detect other types of errors (like missing server public key) if you need more detailed classification. If you need the results in a single, sorted file, just cat the various output files together as you see fit.

share|improve this answer

Look up the various tools that automate running commands on multiple hosts through ssh. For example, with Mussh:

mussh -H hosts.txt -o ConnectTimeout=5 -P -d -c 'echo `hostname` is alive'

Massage the output as needed.

Side note: why are you storing IP addresses in list.txt? Server names are enough. If the names you want to use aren't the DNS names, use Host directives in ~/.ssh/config.

share|improve this answer
servers that I can log in via ssh key
servers that prompts for password (of course password is unknown..)

expect will provide input to your interactive commands. The ssh client with the -v flag will tell you what auth methods the server accepts. If you get to the interactive prompt, make it quit. Do what you wan, you have all you need.

servers that are unreachable

Same, one command to rule them all, and in the darkness, bind them... Hum.

share|improve this answer

The mussh command will output only errors so you can just give run it without debug.

$ cat list.txt       trustme       wobudong     failhost

$ awk '{print $2}' list.txt | mussh -H - -t 10 -m -c hostname
trustme: trustme
failhost: ssh: Could not resolve hostname failhost: Name or service not known
wobudong: ssh: connect to host wobudong port 22: Connection timed out
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.