[英]How to track required time to ssh into the system using net-ssh gem
Currently I am working on a Rails application. 目前,我正在开发Rails应用程序。 I have to ssh into the system and return the system information like the Ubuntu version, bios version etc.
我必须进入系统并返回系统信息,例如Ubuntu版本,BIOS版本等。
I am using the net-ssh gem and it is working fine. 我正在使用net-ssh gem,它工作正常。
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. 问题是,有时ssh需要花费太多时间。
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".
或者,是否还有其他方法可以添加时间检查,例如,如果ssh会话的时间大于1分钟,则返回“无法登录”。
I am using Ruby 1.8.7 and Rails 3.2.14. 我正在使用Ruby 1.8.7和Rails 3.2.14。
You can use the timeout
module like so: 您可以像这样使用
timeout
模块 :
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. 如果超时,则会引发
Timeout::Error
,您可以捕获该Timeout::Error
以便打印出“无法登录”消息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.