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.



77
78
79
# File 'lib/bettercap/shell.rb', line 77

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

.available?(cmd) ⇒ Boolean

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

Returns:

  • (Boolean)


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

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

.change_mac(iface, mac) ⇒ Object

Change the iface mac address to mac using ifconfig.



61
62
63
64
65
66
67
68
69
# File 'lib/bettercap/shell.rb', line 61

def change_mac(iface, mac)
  if RUBY_PLATFORM =~ /.+bsd/ or RUBY_PLATFORM =~ /darwin/
    self.ifconfig( "#{iface} ether #{mac}")
  elsif RUBY_PLATFORM =~ /linux/
    self.ifconfig( "#{iface} hw ether #{mac}")
  else
   raise BetterCap::Error, 'Unsupported operating system'
  end
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
36
# File 'lib/bettercap/shell.rb', line 20

def execute(command)
  Logger.debug 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 ).



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

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

.ip(iface = '') ⇒ Object

Get the iface network interface configuration ( using iproute2 ).



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

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

.ndpObject

Get the NDP table cached on this computer.



82
83
84
# File 'lib/bettercap/shell.rb', line 82

def ndp
  self.execute( 'LANG=en && LANGUAGE=en_EN.UTF-8 && ip -6 neighbor show')
end

.which(cmd) ⇒ Object

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



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

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