我的问题是 ruby 识别需要'packetfu'并在 IRB 中包含 PacketFu,但是当我将它作为脚本运行时不起作用。我想知道这是否是由于我的 gem 目录的组织方式,或者当我以 sudo 签入以运行应用程序时是否发生了一些奇怪的事情。
这是我以普通用户身份运行时产生的错误:
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>'
不适用于 sudo。为什么?:
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>'
简单嗅探器程序的 packetfu 文档中的示例代码:
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)
任何帮助表示赞赏。感谢您的关注!