Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a list of servers:

cat list.txt
10.10.10.10 servera
10.11.10.10 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
add comment

3 Answers 3

up vote 14 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
    else
        echo $ip $host $status >> other
    fi
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
add comment

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
add comment
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
add comment

Your Answer

 
discard

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.