class ShellStrike

Constants

VERSION

Attributes

global_actions[R]
hosts[R]
passwords[R]
usernames[R]

Public Class Methods

new(hosts, usernames, passwords, global_actions = []) click to toggle source

Initialises a new ShellStrike instance @param hosts [Array<Host>] an array of Hosts @param usernames [Array<String>] an array of usernames to test; a username dictionary. @param passwords [Array<String>] an array of passwords to test; a password dictionary. @param global_actions [Array<String>] an array of shell commands to execute against every host. Interactive shell commands are NOT supported.

# 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

Public Instance Methods

failed_hosts() click to toggle source

An array of hosts for which valid credentials were not able to be identified. @return An array of Host objects

# File lib/shell_strike.rb, line 72
def failed_hosts
  @failed_hosts ||= []
end
identified_credentials() click to toggle source

A hash of hosts and their valid credentials. @return A hash of Host URIs and their valid credentials. @example

{ '192.168.1.100:22' => ['admin', 'password'] }
# File lib/shell_strike.rb, line 58
def identified_credentials
  @identified_credentials ||= {}
end
identify_credentials!() click to toggle source

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

# 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) click to toggle source

Subscribe to an event @param event_name [Symbol] The event to subscribe to. @yieldparam block The block to execute

# File lib/shell_strike.rb, line 79
def on(event_name, &block)
  event_bus.on(event_name, &block)
end
unreachable_hosts() click to toggle source

A hash of hosts which were unreachable. @return A hash of Host objects and their error messages. @example

#<ShellStrike::Host:*> => 'Unable to connect to *. No route to host'
# File lib/shell_strike.rb, line 66
def unreachable_hosts
  @unreachable_hosts ||= {}
end

Private Instance Methods

event_bus() click to toggle source
# File lib/shell_strike.rb, line 117
def event_bus
  @event_bus ||= EventBus.new
end
store_failed_host(host) click to toggle source

Stores the host (for which no valid credentials could be identified) into the failed hosts array. @param host [Host] the host for which no valid credentials could be identified.

# File lib/shell_strike.rb, line 113
def store_failed_host(host)
  failed_hosts << host
end
store_unreachable_host(host, message) click to toggle source

Stores the unreachable host into the unreachable hosts array @param host [Host] The unreachable host. @param message [String] A message with further information about the unreachability of the host.

# File lib/shell_strike.rb, line 107
def store_unreachable_host(host, message)
  unreachable_hosts[host.to_uri] = message
end
store_valid_credentials(host, username, password) click to toggle source

Stores valid credentials into the identified_credentials array @param host [Host] The host object for which to store the valid credentials @param username [String] The valid username for this host @param password [String] The valid password for this host

# File lib/shell_strike.rb, line 97
def store_valid_credentials(host, username, password)
  identified_credentials[host.to_uri] = [] unless identified_credentials.has_key? host.to_uri
  identified_credentials[host.to_uri] << [username, password]

  event_bus.emit(:credentials_identified, host, username, password)
end
username_password_combinations() click to toggle source

Creates an array of username and password combinations, using the previously supplied usernames and passwords. @return An array of (yet to be validated!) username and password combinations @example

[ ['root', 'letmein'], ['admin', 'password'] ]
# File lib/shell_strike.rb, line 89
def username_password_combinations
  @usernames.product(@passwords)
end