Module: BetterCap::Shell

Defined in:
lib/bettercap/shell.rb

Overview

Class responsible of executing various shell commands.

Class Method Summary collapse

Class Method Details

.arpObject

Get the ARP table cached on this computer.


65
66
67
# File 'lib/bettercap/shell.rb', line 65

def arp
  self.execute( 'LANG=en && arp -a -n' )
end

.available?(cmd) ⇒ Boolean

Cross-platform way of finding an executable in the $PATH.

Returns:

  • (Boolean)

50
51
52
# File 'lib/bettercap/shell.rb', line 50

def available?(cmd)
  !which(cmd).nil?
end

.execute(command) ⇒ Object

Execute command and return its output. Raise BetterCap::Error if the return code is not 0.


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/bettercap/shell.rb', line 20

def execute(command)
  r = ''
  10.times do
    begin
      r=%x(#{command})
      if $? != 0
        raise BetterCap::Error, "Error, executing #{command}"
      end
      break
    rescue Errno::EMFILE => e
      Logger.debug "Retrying command '#{command}' due to Errno::EMFILE error ..."
      sleep 1
    end
  end
  r
end

.ifconfig(iface = '') ⇒ Object

Get the iface network interface configuration ( using ifconfig ).


55
56
57
# File 'lib/bettercap/shell.rb', line 55

def ifconfig(iface = '')
  self.execute( "LANG=en && ifconfig #{iface}" )
end

.ip(iface = '') ⇒ Object

Get the iface network interface configuration ( using iproute2 ).


60
61
62
# File 'lib/bettercap/shell.rb', line 60

def ip(iface = '')
  self.execute( "LANG=en && ip addr show #{iface}" )
end

.which(cmd) ⇒ Object

Cross-platform way of finding an executable in the $PATH.


38
39
40
41
42
43
44
45
46
47
# File 'lib/bettercap/shell.rb', line 38

def which(cmd)
  exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
  ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
    exts.each { |ext|
      exe = File.join(path, "#{cmd}#{ext}")
      return exe if File.executable?(exe) && !File.directory?(exe)
    }
  end
  return nil
end