Tuesday, March 03, 2009

Ruby Net-SSH connection with just a password

I've been trying to use Net-SSH for Ruby on Windows for little while but I couldn't easily find anything useful for my specific usage needs. What I want to do is to connect to a server using just a password and no keys. I found the information over here. http://net-ssh.rubyforge.org/ssh/v1/chapter-2.html. The tutorials out there showing password based connection didn't work for me because the behavior I was seeing (at least on Windows) was that net-ssh was looking for a key file even when I didn't want to use one. Of course using a dss based key file didn't work for me either so I'm set on connecting using just a password. So here's what worked for me.


Net::SSH.start( 'host', 'user',
:password => 'passwd', :auth_methods => ["password"]) do |session|
session.exec("ls -l")
end


From the documentation on the page mentioned above.. (These are also the only authorization methods that are supported.) If you want them to be tried in a different order, or if you don’t want certain methods to be used, you can specify your own list via this option.. Apparently the other methods would not fail-over to password based auth and just die on key auth. Specifying password authorization as the only method to try worked great for me. Keep in mind, this is only something I've observed on my Windows system so this might be a non-issue for a lot of you. If you are stuck on this like I was, I hope this was useful.