Skip to content

Instantly share code, notes, and snippets.

@ashrithr
Last active December 17, 2015 18:09
Show Gist options
  • Save ashrithr/5651057 to your computer and use it in GitHub Desktop.
Save ashrithr/5651057 to your computer and use it in GitHub Desktop.
ruby function to check if passwordless is setup
def sshable?(nodes_arr, ssh_user, ssh_key, port=22)
nodes_arr.each do |instance|
`ssh -t -t -o ConnectTimeout=2 -o StrictHostKeyChecking=no -o BatchMode=yes -i #{ssh_key} #{ssh_user}@#{instance} "echo" &>/dev/null`
#ssh options used:
#ConnectTimeout => timeout the ssh session
#StrictHostKeyChecking => dont check for host key
#BatchMode => dont ask for password, of prompted for password exit out
#-t -t => Force pseudo-tty allocation, Multiple -t options force tty allocation, even if ssh has no local tty
unless $?.success?
puts "[Error]: cannot ssh into instance: #{instance}"
exit 1
end
end
end
#Usage:
nodes = %w(localhost 127.0.0.1)
sshable(nodes, 'root', '~/.ssh/id_rsa')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment