Class: ShellStrike::Host

Inherits:
Object
  • Object
show all
Defined in:
lib/shell_strike/host.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, port = 22, connection_timeout = 30, actions = []) ⇒ Host

Initialises a new Host object.

Parameters:

  • host (String)

    the hostname or IP address of the host.

  • port (Number) (defaults to: 22)

    the port number to use for the connection.

  • connection_timeout (Number) (defaults to: 30)

    how long to wait before timing out connection attempts (in seconds).

  • actions (Array<String>) (defaults to: [])

    Shell commands to execute against the server, upon successful connection. Interactive commands are NOT supported.


9
10
11
12
13
14
# File 'lib/shell_strike/host.rb', line 9

def initialize(host, port = 22, connection_timeout = 30, actions = [])
  @host = host
  @port = port
  @connection_timeout = connection_timeout
  @actions = actions
end

Instance Attribute Details

#actionsObject (readonly)

Returns the value of attribute actions


2
3
4
# File 'lib/shell_strike/host.rb', line 2

def actions
  @actions
end

#connection_timeoutObject (readonly)

Returns the value of attribute connection_timeout


2
3
4
# File 'lib/shell_strike/host.rb', line 2

def connection_timeout
  @connection_timeout
end

#hostObject (readonly)

Returns the value of attribute host


2
3
4
# File 'lib/shell_strike/host.rb', line 2

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port


2
3
4
# File 'lib/shell_strike/host.rb', line 2

def port
  @port
end

Instance Method Details

#reachable?Boolean

Whether the host is able to be reached on the specified port.

Returns:

  • (Boolean)

17
18
19
20
21
22
23
24
# File 'lib/shell_strike/host.rb', line 17

def reachable?
  begin
    Socket.tcp(@host, @port, connect_timeout: @connection_timeout) { |s| s.close }
    return true
  rescue
    return false  
  end
end

#test_credentials(username, password) ⇒ Result

Tests the specified username and password.

Parameters:

  • username (String)

    the username to test.

  • password (String)

    the password to test.

Returns:

  • (Result)

    a result object indicating whether the credentials are valid


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/shell_strike/host.rb', line 30

def test_credentials(username, password)
  valid = false
  message = ''
  exception = nil

  begin
    Net::SSH.start(@host, username, password: password, port: @port, non_interactive: true, timeout: @connection_timeout)

    valid = true
  rescue Net::SSH::AuthenticationFailed
    message = 'Invalid Credentials'
  rescue Net::SSH::ConnectionTimeout => e
    message = "Connection timed out whilst attempting to connect to #{@host} on port #{@port}"
    exception = e
  rescue Net::SSH::Exception => e
    message = "An unexpected error occurred whilst connecting to #{@host} on port #{@port} with username #{username} and password #{@password}: #{e.message}"
    exception = e
  rescue Errno::EHOSTUNREACH => e
    message = "Unable to connect to #{@host}: #{e.message}"
    exception = e
  end

  ShellStrike::Result.new(valid, message, exception)
end

#to_uriString

Returns the current host's address in URI form.

Examples:

192.168.1.200:22
172.20.16.20:200
example.com:22

Returns:

  • (String)

    the current host's address in URI form. (host:port)


61
62
63
# File 'lib/shell_strike/host.rb', line 61

def to_uri
  "#{self.host}:#{self.port}"
end