From: Dan Nachbar Date: 2011-08-27T02:49:42+09:00 Subject: Re: ssh login to a host Being able to pass #gets a length limit parameter is new in ruby 1.9 The key point is that an active SSH port will spontaneously spit out "SSH" followed by a bunch of other stuff when socket 22 is opened. There are any number of techniques to collect the string and make the appropriate comparison. Given that TCPSocket has a whopping 190 different instance methods in 1.9.2, there are a great many ways to skin this particular cat. PS - From the shell you might also try "telnet myfavoritehost.com 22" to get an idea of what's going on. Dan Nachbar On Aug 26, 2011, at 1:16 PM, Darryl L. Pierce wrote: > On Sat, Aug 27, 2011 at 02:03:12AM +0900, Dan Nachbar wrote: >> >> On Aug 26, 2011, at 11:59 AM, Rick Tan wrote: >> >>> Hi, >>> >>> Can someone give me some advice on how to reliably check that a server >>> is alive and accepts ssh login? Have tried ping but that opens a small >>> window that ssh login is still not available. >> >> With 1.9.2 you can say - >> >> require 'socket' >> s = TCPSocket.open('myfavoritehost.com', 22) >> >> s.gets(3).eql? "SSH" > > Using the following in IRB: > > irb(main):008:0> s = TCPSocket.open('localhost', 22) > => # > irb(main):009:0> s.gets.eql? "SSH" > => false > > Retrying with s.gets(3).eql? "SSH" theres: > > irb(main):011:0> s.gets(3).eql? "SSH" > TypeError: can't convert Fixnum into String > from (irb):11:in `gets' > from (irb):11 > from :0 > > Instead try: > > s.gets.include? "SSH" > > though I'd suspect that an established connection on port 22 should be > sufficient to say it's enabled and working, and just use the response to > check what versions (SSH1 or SSH2) are supported. > > -- > Darryl L. Pierce > http://mcpierce.multiply.com/ > "What do you care what people think, Mr. Feynman?"