lib/egon/undercloud/ssh-connection.rb in egon-0.2.0 vs lib/egon/undercloud/ssh-connection.rb in egon-0.3.0
- old
+ new
@@ -1,5 +1,6 @@
+require 'curb'
require 'net/ssh'
require 'stringio'
# TODO: Remove this class when this branch is merged,
# https://github.com/fusor/fusor/blob/deploy-cfme/server/app/lib/utils/fusor/command_utils.rb,
@@ -9,9 +10,36 @@
class SSHConnection
def initialize(host, user, password)
@host = host
@user = user
@password = password
+ end
+
+ def port_open?(port, local_ip="127.0.0.1", remote_ip="192.0.2.1", seconds=1)
+ t = Thread.new {
+ Net::SSH.start(@host, @user, :password => @password, :timeout => 2,
+ :auth_methods => ["password"],
+ :number_of_password_prompts => 0) do |session|
+ puts "Forwarding #{port} #{remote_ip} #{port}"
+ session.forward.local( port, remote_ip, port )
+ session.loop { true }
+ end
+ }
+
+ sleep 1
+ begin
+ url = "#{local_ip}:#{port}"
+ puts "Testing #{url}"
+ http = Curl.get(url)
+ puts http.body_str
+ puts "Port #{port} is open"
+ t.kill
+ true
+ rescue Curl::Err::GotNothingError, Curl::Err::ConnectionFailedError
+ puts "Port #{port} is closed"
+ t.kill
+ false
+ end
end
def call_complete
@on_complete.call if @on_complete
end