Class: CmdLine

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

Class Method Summary collapse

Class Method Details

.parse(args) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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/cmdline.rb', line 5

def self.parse(args)
    @config = {}

    opts = OptionParser.new do |opt|
        opt.on('-i', '--interface=NIC', 'Network interface to sniff (required)') do |nic|
            @config[:nic] = nic
        end

        opt.on '-d', '--discard=THRESH', Float, 'Discard keys with request/sec rate below THRESH' do |discard_thresh|
            @config[:discard_thresh] = discard_thresh
        end

        opt.on '-r', '--refresh=MS', Float, 'Refresh the stats display every MS milliseconds' do |refresh_rate|
            @config[:refresh_rate] = refresh_rate
        end

        opt.on_tail '-h', '--help', 'Show usage info' do 
            puts opts
            exit
        end
    end

    opts.parse!

    # bail if we're not root
    unless Process::Sys.getuid == 0
        puts "** ERROR: needs to run as root to capture packets"
        exit 1
    end

    # we need need a nic to listen on
    unless @config.has_key?(:nic)
        puts "** ERROR: You must specify a network interface to listen on"
        puts opts
        exit 1
    end

    # we can't do 'any' interface just yet due to weirdness with ruby pcap libs
    if @config[:nic] =~ /any/i
        puts "** ERROR: can't bind to any interface due to odd issues with ruby-pcap"
        puts opts
        exit 1
    end

    @config
end