user2622247
user2622247

Reputation: 1089

How to track required time to ssh into the system using net-ssh gem

Currently I am working on a Rails application. I have to ssh into the system and return the system information like the Ubuntu version, bios version etc.

I am using the net-ssh gem and it is working fine.

Here is my sample code:

require 'net/ssh'
def check_bios_version
  Net::SSH.start('localhost','ubuntu', :password => '1234') do |session|
    return vid = session.exec!("echo 'ubuntu1234' | sudo -S dmidecode -s bios-version")  
  end
end

The problem is, sometimes ssh is taking too much time.

Is there a utility or function I can use to track the time? Or, is there any other way for adding a time check, such that if the time for the ssh session is greater than 1 minute then return "Unable to login".

I am using Ruby 1.8.7 and Rails 3.2.14.

Upvotes: 1

Views: 57

Answers (1)

Sam Starling
Sam Starling

Reputation: 5378

You can use the timeout module like so:

require 'net/ssh'
require 'timeout'

def check_bios_version
  Timeout::timeout(60) {
    Net::SSH.start('localhost','ubuntu', :password => '1234') do |session|
      return vid = session.exec!("echo 'ubuntu1234' | sudo -S dmidecode -s bios-version")  
    end
  }  
end

If it times out, it'll raise Timeout::Error, which you can catch in order to print out an "Unable to login" message.

Upvotes: 4

Related Questions