Class: Bilbo::Capture
- Inherits:
-
Object
- Object
- Bilbo::Capture
- Defined in:
- lib/bilbo/capture.rb
Overview
Capture packets simply.
Instance Attribute Summary collapse
-
#iface ⇒ String
interface to capture on.
-
#packet_stream ⇒ PCAPRUB::Pcap
readonly
packet stream.
-
#promisc ⇒ Boolean
capture packets in promiscuous mode.
-
#snaplen ⇒ Integer
amount of data for each frame that is actually captured.
-
#timeout ⇒ Integer
capture packet timeout.
Instance Method Summary collapse
-
#initialize(args = {}) ⇒ Capture
constructor
Create a new Bilbo capture instance.
-
#packets(args = {}) ⇒ Object
Simple interface to work with the packet stream.
-
#start(args = {}) ⇒ PCAPRUB::Pcap
Start the actual packet capturing.
Constructor Details
#initialize(args = {}) ⇒ Capture
Create a new Bilbo capture instance.
Simple Defaults
You can let bilbo take care of things for you with its defaults:
cap = Bilbo::Capture.new(start: true)
Custom Options
You can also set you own options with defaults and check them easily:
cap = Bilbo::Capture.new(iface: "eth0")
cap.iface
# => "eth0"
38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/bilbo/capture.rb', line 38 def initialize(args={}) @iface = args[:iface] || Pcap.lookupdev # lookup default interface @snaplen = args[:snaplen] || 65535 # default snaplen @promisc = args[:promisc] || false # default promisc @timeout = args[:timeout] || 0 # default timeout start(args) if args[:start] if block_given? start @packet_stream.each { |packet| yield packet } end end |
Instance Attribute Details
#iface ⇒ String
interface to capture on
12 13 14 |
# File 'lib/bilbo/capture.rb', line 12 def iface @iface end |
#packet_stream ⇒ PCAPRUB::Pcap (readonly)
packet stream
8 9 10 |
# File 'lib/bilbo/capture.rb', line 8 def packet_stream @packet_stream end |
#promisc ⇒ Boolean
capture packets in promiscuous mode
20 21 22 |
# File 'lib/bilbo/capture.rb', line 20 def promisc @promisc end |
#snaplen ⇒ Integer
amount of data for each frame that is actually captured
16 17 18 |
# File 'lib/bilbo/capture.rb', line 16 def snaplen @snaplen end |
#timeout ⇒ Integer
capture packet timeout
24 25 26 |
# File 'lib/bilbo/capture.rb', line 24 def timeout @timeout end |
Instance Method Details
#packets(args = {}) ⇒ Object
Simple interface to work with the packet stream.
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/bilbo/capture.rb', line 112 def packets(args={}) if block_given? @packet_stream.each { |packet| yield packet } else if args[:count] packets = [] @packet_stream.each do |packet| packets << packet break if packets.count.eql? args[:count] end return packets end @packet_stream end end |
#start(args = {}) ⇒ PCAPRUB::Pcap
70 71 72 73 74 75 76 77 78 79 |
# File 'lib/bilbo/capture.rb', line 70 def start(args={}) iface = args[:iface] || @iface # default interface snaplen = args[:snaplen] || @snaplen # default snaplen promisc = args[:promisc] || @promisc # default promisc timeout = args[:timeout] || @timeout # default timeout @packet_stream = Pcap.open_live(iface, snaplen, promisc, timeout) if block_given? @packet_stream.each { |packet| yield packet } end end |