lib/packetgen/utils.rb in packetgen-3.1.8 vs lib/packetgen/utils.rb in packetgen-3.2.0

- old
+ new

@@ -21,11 +21,11 @@ # interface name def self.arp_cache raw_cache = `/usr/sbin/arp -an` cache = {} - raw_cache.split(/\n/).each do |line| + raw_cache.split("\n").each do |line| match = line.match(/\((\d+\.\d+\.\d+\.\d+)\) at (([a-fA-F0-9]{2}:){5}[a-fA-F0-9]{2})(?: \[ether\])? on (\w+)/) cache[match[1]] = [match[2], match[4]] if match end cache @@ -44,24 +44,23 @@ # @return [String,nil] # @raise [RuntimeError] user don't have permission to capture packets on network device. def self.arp(ipaddr, options={}) unless options[:no_cache] local_cache = self.arp_cache - return local_cache[ipaddr].first if local_cache.key? ipaddr + return local_cache[ipaddr].first if local_cache.key?(ipaddr) end iface = options[:iface] || PacketGen.default_iface timeout = options[:timeout] || 1 my_hwaddr = Config.instance.hwaddr(iface) arp_pkt = Packet.gen('Eth', dst: 'ff:ff:ff:ff:ff:ff', src: my_hwaddr) .add('ARP', sha: Config.instance.hwaddr(iface), - spa: Config.instance.ipaddr(iface), tpa: ipaddr) + spa: Config.instance.ipaddr(iface), + tpa: ipaddr) capture = Capture.new(iface: iface, timeout: timeout, max: 1, filter: "arp src #{ipaddr} and ether dst #{my_hwaddr}") - cap_thread = Thread.new do - capture.start - end + cap_thread = Thread.new { capture.start } arp_pkt.to_w(iface) cap_thread.join return if capture.packets.empty?