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
  • with the help of ssh and ping commands u may achieve it Jan 27, 2012 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, 2012 at 7:43

4 Answers 4


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)
    case $status in
        ok) echo $ip $host >> good ;;
        *"Permission denied"*) 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.


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.

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.


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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .