Class: ShellStrike

Inherits:
Object
  • Object
show all
Defined in:
lib/shell_strike.rb,
lib/shell_strike/version.rb,
lib/shell_strike/exceptions.rb

Defined Under Namespace

Modules: Ssh Classes: EventBus, Host, HostsNotDefined, InvalidEvent, PasswordsNotDefined, Result, UsernamesNotDefined

Constant Summary collapse

VERSION =
"1.0.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hosts, usernames, passwords, global_actions = []) ⇒ ShellStrike

Initialises a new ShellStrike instance

Parameters:

  • hosts (Array<Host>)

    an array of Hosts

  • usernames (Array<String>)

    an array of usernames to test; a username dictionary.

  • passwords (Array<String>)

    an array of passwords to test; a password dictionary.

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

    an array of shell commands to execute against every host. Interactive shell commands are NOT supported.

Raises:



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

def initialize(hosts, usernames, passwords, global_actions = [])
  raise HostsNotDefined if hosts.nil? || hosts.empty?
  raise UsernamesNotDefined if usernames.nil? || usernames.empty?
  raise PasswordsNotDefined if passwords.nil? || passwords.empty?

  @hosts = hosts
  @usernames = usernames
  @passwords = passwords
  @global_actions = global_actions
end

Instance Attribute Details

#global_actionsObject (readonly)

Returns the value of attribute global_actions.



9
10
11
# File 'lib/shell_strike.rb', line 9

def global_actions
  @global_actions
end

#hostsObject (readonly)

Returns the value of attribute hosts.



9
10
11
# File 'lib/shell_strike.rb', line 9

def hosts
  @hosts
end

#passwordsObject (readonly)

Returns the value of attribute passwords.



9
10
11
# File 'lib/shell_strike.rb', line 9

def passwords
  @passwords
end

#usernamesObject (readonly)

Returns the value of attribute usernames.



9
10
11
# File 'lib/shell_strike.rb', line 9

def usernames
  @usernames
end

Instance Method Details

#failed_hostsObject

An array of hosts for which valid credentials were not able to be identified.

Returns:

  • An array of Host objects



72
73
74
# File 'lib/shell_strike.rb', line 72

def failed_hosts
  @failed_hosts ||= []
end

#identified_credentialsObject

A hash of hosts and their valid credentials.

Examples:

{ '192.168.1.100:22' => ['admin', 'password'] }

Returns:

  • A hash of Host URIs and their valid credentials.



58
59
60
# File 'lib/shell_strike.rb', line 58

def identified_credentials
  @identified_credentials ||= {}
end

#identify_credentials!Object

Identifies valid credentials for each host and populates the identified_credentials, failed_hosts and unreachable_hosts arrays.



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

def identify_credentials!
  @hosts.each do |host|
    is_reachable, explanation = Ssh.check_host_reachable(host)

    unless is_reachable
      store_unreachable_host(host, explanation)
      next
    end

    credential_failure_count = 0

    username_password_combinations.each do |username, password|
      if Ssh.valid_credentials?(host, username, password)
        store_valid_credentials(host, username, password)
      else
        credential_failure_count += 1
        event_bus.emit(:credentials_failed, host, username, password)
      end
    end
    
    store_failed_host(host) if credential_failure_count == username_password_combinations.length
  end
end

#on(event_name) {|block| ... } ⇒ Object

Subscribe to an event

Parameters:

  • event_name (Symbol)

    The event to subscribe to.

Yield Parameters:

  • block

    The block to execute



79
80
81
# File 'lib/shell_strike.rb', line 79

def on(event_name, &block)
  event_bus.on(event_name, &block)
end

#unreachable_hostsObject

A hash of hosts which were unreachable.

Examples:

#<ShellStrike::Host:*> => 'Unable to connect to *. No route to host'

Returns:

  • A hash of Host objects and their error messages.



66
67
68
# File 'lib/shell_strike.rb', line 66

def unreachable_hosts
  @unreachable_hosts ||= {}
end