Report this

What is the reason for this report?

Validate password less authentication with n number of hosts.

Posted on April 5, 2017

I need to check plenty of servers everyday. because of few servers down or changes happend… password less authentication is failing on few hosts. I need a command or script to verify that password less authentication is working fine or not on all the servers.

Usually… I get the server name on all the servers using **ssh -q <servername> uname -a **
If servers are down or key is not working… my script is getting stuck in the middle. I want to avoid it.



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

@Gowtham

If you’re using a bash script to check the status, you could use something relatively simple, such as:

checkSsh()
{
    local username="${1}"
    local hostname="${2}"
    local privatekey="${3}"
    local checkConn=$(ssh ${username}@${hostname} -i ${privatekey} "echo 2>&1" && echo UP || DOWN)

    if [ "${checkConn}" == "DOWN" ];
    then
        # Action taken when host is down.
        echo DOWN
    else
        # Action taken when host is up.
        echo UP
    fi
}

checkSsh "${1}" "${2}" "${3}"

You’d then pass on the details:

./checkssh.sh root 111.222.333.444 /path/to/key

You could even reduce that down to:

if ssh ${1}@${2} -i ${3} "echo 2>&1"; then
    echo UP
else
    echo DOWN
fi

And replace the echo UP|DOWN with whatever action you needed to perform. Or, if the user you’re trying to login as to test is always the same:

if ssh root@${1} -i ${2} "echo 2>&1"; then
    echo UP
else
    echo DOWN
fi

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.