Class: GuessOS::Conn
- Inherits:
-
Object
- Object
- GuessOS::Conn
- Defined in:
- lib/guess_os/conn.rb
Instance Attribute Summary collapse
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#last_output ⇒ Object
readonly
Returns the value of attribute last_output.
-
#ok ⇒ Object
readonly
Returns the value of attribute ok.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
Instance Method Summary collapse
- #exec(command) ⇒ Object
-
#initialize(host) ⇒ Conn
constructor
A new instance of Conn.
- #local_exec(command) ⇒ Object
- #remote_exec(command) ⇒ Object
Constructor Details
#initialize(host) ⇒ Conn
Returns a new instance of Conn.
10 11 12 13 14 15 |
# File 'lib/guess_os/conn.rb', line 10 def initialize(host) @host = host @last_output = "" @status = nil @ok = nil end |
Instance Attribute Details
#host ⇒ Object (readonly)
Returns the value of attribute host.
5 6 7 |
# File 'lib/guess_os/conn.rb', line 5 def host @host end |
#last_output ⇒ Object (readonly)
Returns the value of attribute last_output.
8 9 10 |
# File 'lib/guess_os/conn.rb', line 8 def last_output @last_output end |
#ok ⇒ Object (readonly)
Returns the value of attribute ok.
6 7 8 |
# File 'lib/guess_os/conn.rb', line 6 def ok @ok end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
7 8 9 |
# File 'lib/guess_os/conn.rb', line 7 def status @status end |
Instance Method Details
#exec(command) ⇒ Object
17 18 19 20 21 22 23 |
# File 'lib/guess_os/conn.rb', line 17 def exec(command) @ok = :unkown @status = :unkown return local_exec(command) if host.local? remote_exec(command) end |
#local_exec(command) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/guess_os/conn.rb', line 25 def local_exec(command) begin output = `#{command}` rescue @ok = false @status = "Error: Command not found!" end if $?.nil? @ok = false @status = "Error: Command not found!" elsif $?.exitstatus.zero? @ok = true @status = "Ok" else @ok = false @status = "Exit status: #{$?.exitstatus}" end @last_output = if output.nil? "" else output.encode(Encoding::UTF_8, invalid: :replace) end end |
#remote_exec(command) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 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 |
# File 'lib/guess_os/conn.rb', line 50 def remote_exec(command) output = "Error" begin session = Net::SSH.start(@host.ip, @host.username, port: @host.port, password: @host.password, keepalive: true, timeout: 30, non_interactive: true) if session.instance_of?(Net::SSH::Connection::Session) output = session.exec!(command) @status = :ok @ok = true end rescue Errno::EHOSTUNREACH @status = "[ERROR] Host #{@host.ip} unreachable!" rescue Net::SSH::AuthenticationFailed @status = "[ERROR] SSH::AuthenticationFailed!" rescue Net::SSH::HostKeyMismatch @status = "SSH::HostKeyMismatch!" puts("* The destination server's fingerprint is not matching " \ "what is in your local known_hosts file.") puts("* Remove the existing entry in your local known_hosts file") puts("* Try this => ssh-keygen -f '/home/USERNAME/.ssh/known_hosts' " \ "-R #{@host.ip}") rescue SocketError @status = "[ERROR] SocketError with IP/port [#{@host.ip}:#{@host.port}]" rescue Errno::ECONNREFUSED @status = "[ERROR] ConnRefused: SSH on [#{@host.username}@#{@host.ip}:#{@host.port}]" rescue => e @status = "[#{e.class}] SSH on <#{@host.username}@#{@host.ip}:#{@host.port}>" \ " exec: #{command}" end @last_output = if output.nil? "" else output.encode(Encoding::UTF_8, invalid: :replace) end end |