Class: Egon::Undercloud::SSHConnection
- Inherits:
-
Object
- Object
- Egon::Undercloud::SSHConnection
- Includes:
- PortCheckMixin
- Defined in:
- lib/egon/undercloud/ssh-connection.rb
Instance Method Summary collapse
- #call_complete ⇒ Object
- #call_failure ⇒ Object
- #execute(commands, stringio = nil) ⇒ Object
-
#initialize(host, user, password) ⇒ SSHConnection
constructor
A new instance of SSHConnection.
- #on_complete(hook) ⇒ Object
- #on_failure(hook) ⇒ Object
- #remote_port_open?(port, stringio = nil, local_ip = "127.0.0.1", remote_ip = "192.0.2.1", seconds = 1) ⇒ Boolean
- #stringio_write(stringio, text) ⇒ Object
Methods included from PortCheckMixin
Constructor Details
#initialize(host, user, password) ⇒ SSHConnection
Returns a new instance of SSHConnection.
14 15 16 17 18 |
# File 'lib/egon/undercloud/ssh-connection.rb', line 14 def initialize(host, user, password) @host = host @user = user @password = password end |
Instance Method Details
#call_complete ⇒ Object
46 47 48 |
# File 'lib/egon/undercloud/ssh-connection.rb', line 46 def call_complete @on_complete.call if @on_complete end |
#call_failure ⇒ Object
54 55 56 |
# File 'lib/egon/undercloud/ssh-connection.rb', line 54 def call_failure @on_failure.call if @on_failure end |
#execute(commands, stringio = nil) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/egon/undercloud/ssh-connection.rb', line 62 def execute(commands, stringio = nil) begin # :timeout => how long to wait for the initial connection to be made Net::SSH.start(@host, @user, :password => @password, :timeout => 2, :auth_methods => ["password"], :number_of_password_prompts => 0) do |ssh| # open a new channel and configure a minimal set of callbacks, then run # the event loop until the channel finishes (closes) channel = ssh.open_channel do |ch| ch.request_pty do |ch, success| if !success puts "Error: could not obtain pty" call_failure call_complete end end ch.exec commands do |ch, success| call_failure unless success # "on_data" is called when the process writes something to stdout ch.on_data do |c, data| stringio_write(stringio, data) end # "on_extended_data" is called when the process writes something to stderr ch.on_extended_data do |c, type, data| $stderr.print data if stringio.nil? stringio_write(stringio, data) call_failure end ch.on_close { call_complete } end end channel.wait end call_complete rescue Exception => e puts e. puts e.backtrace.inspect call_failure call_complete stringio_write(stringio, e.) e. if stringio.nil? end end |
#on_complete(hook) ⇒ Object
50 51 52 |
# File 'lib/egon/undercloud/ssh-connection.rb', line 50 def on_complete(hook) @on_complete = hook end |
#on_failure(hook) ⇒ Object
58 59 60 |
# File 'lib/egon/undercloud/ssh-connection.rb', line 58 def on_failure(hook) @on_failure = hook end |
#remote_port_open?(port, stringio = nil, local_ip = "127.0.0.1", remote_ip = "192.0.2.1", seconds = 1) ⇒ Boolean
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/egon/undercloud/ssh-connection.rb', line 25 def remote_port_open?(port, stringio=nil, local_ip="127.0.0.1", remote_ip="192.0.2.1", seconds=1) t = Thread.new { begin Net::SSH.start(@host, @user, :password => @password, :timeout => seconds, :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 rescue => e stringio_write(stringio, e.) end } sleep 1 port_status = port_open?(local_ip, port, stringio) t.kill port_status end |
#stringio_write(stringio, text) ⇒ Object
20 21 22 23 |
# File 'lib/egon/undercloud/ssh-connection.rb', line 20 def stringio_write(stringio, text) $stdout.puts text if stringio.nil? stringio.puts text unless stringio.nil? end |