My problem is ruby recognizes require ‘packetfu’ and include PacketFu in IRB but does not work when I run it as a script. I want to know if it’s due to the way my gem directories are organized or if something odd is going on when I check-in as sudo to run the app.
Here is the error generated when I run it as a regular user:
1 tMobile:~ cj3kim$ ruby packetfu.rb
2 Simple sniffer for PacketFu 1.1.5
3 /Users/cj3kim/.rvm/gems/ruby-1.9.3-p0/gems/packetfu-1.1.5/lib/packetfu/capture.rb:73:in `capture': Not root, so can't capture packets. Error: (RuntimeError)
4 from /Users/cj3kim/.rvm/gems/ruby-1.9.3-p0/gems/packetfu-1.1.5/lib/packetfu/capture.rb:46:in `setup_params'
5 from /Users/cj3kim/.rvm/gems/ruby-1.9.3-p0/gems/packetfu-1.1.5/lib/packetfu/capture.rb:39:in `initialize'
6 from packetfu.rb:9:in `new'
7 from packetfu.rb:9:in `sniff'
8 from packetfu.rb:20:in `<main>'
Does not work with sudo. Why?:
9 The-BatMobile:~ cj3kim$ sudo ruby packetfu.rb
10 /Users/cj3kim/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- packetfu (LoadError)
11 from /Users/cj3kim/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
12 from packetfu.rb:1:in `<main>'
Sample code from packetfu documentation for the simple sniffer program:
1 require 'packetfu'
2 include PacketFu
3
4 puts "Simple sniffer for PacketFu #{PacketFu.version}"
5
6 iface = ARGV[0] || "en0"
7
8 def sniff(iface)
9 cap = Capture.new(:iface => iface, :start => true)
10 cap.stream.each do |p|
11 pkt = Packet.parse p
12 if pkt.is_ip?
13 next if pkt.ip_saddr == Utils.ifconfig[:ip_saddr]
14 packet_info = [pkt.ip_saddr, pkt.ip_daddr, pkt.size, pkt.proto.last]
15 puts "%-15s -> %-15s %-4d %s" % packet_info
16 end
17 end
18 end
19
20 sniff(iface)
Any help is appreciated. Thanks for looking!
Usually, it’s because the sudo environment can’t find any of your gems.
If you’re using RVM (and you should: rvm.io ), you can use
rvmsudoinstead, and that will preserve your Ruby environment.If you can’t use RVM for whatever, reason, but you are using a modern version of sudo, you can try
sudo -Eto retain the environment.