Skip to content

Instantly share code, notes, and snippets.

@mgreenly
Created August 28, 2011 15:21
Show Gist options
  • Save mgreenly/1176783 to your computer and use it in GitHub Desktop.
Save mgreenly/1176783 to your computer and use it in GitHub Desktop.
robust net/ssh connection attempt
tries, connection = 0, nil
print "connecting to #{dns_name}"
until connection || ((tries += 1) > 10)
begin
Timeout::timeout(5) do
begin
connection = Net::SSH.start(dns_name, user_name, ssh_options)
rescue Exception => e
connection = nil
end
end
rescue Timeout::Error => e
end
end
if connection
begin
puts "success"
connection.exec! "uptime"
ensure
connection.close
end
else
puts "failed!"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment