2 # Sniffer is a very dumb wrapper to start and stop tcpdumps instances, possibly
3 # with customized filters. Captured traffic is stored in files whose name
4 # depends on the sniffer name. The resulting captured packets for each sniffers
5 # can be accessed as an array through its `packets` method.
7 # Use of more rubyish internal ways to sniff a network like with pcap-able gems
8 # is waaay to much resource consumming, notmuch reliable and soooo slow. Let's
9 # not bother too much with that. :)
11 # Should put all that in a Module.
15 attr_reader :name, :pcap_file, :pid
17 def initialize(name, bridge_name)
19 @bridge_name = bridge_name
20 @bridge_mac = File.open("/sys/class/net/#{@bridge_name}/address", "rb").read.chomp
21 @pcap_file = "#{$tmp_dir}/#{name}.pcap"
24 def capture(filter="not ether src host #{@bridge_mac} and not ether proto \\arp and not ether proto \\rarp")
25 job = IO.popen("/usr/sbin/tcpdump -n -i #{@bridge_name} -w #{@pcap_file} -U '#{filter}' >/dev/null 2>&1")
31 Process.kill("TERM", @pid)
38 if File.exist?(@pcap_file)
39 File.delete(@pcap_file)