Skip to content

Instantly share code, notes, and snippets.

@RichiH
Forked from ytti/xr2pcap.rb
Created March 11, 2016 13:21
Show Gist options
  • Select an option

  • Save RichiH/9497dd0366856642bcee to your computer and use it in GitHub Desktop.

Select an option

Save RichiH/9497dd0366856642bcee to your computer and use it in GitHub Desktop.
XR NPU counter to pcap
#!/usr/bin/env ruby
# put output of 'monitor np counter PUNT_IPV4_ADJ_NULL_RTE_EXCD np3 count 25 location 0/1/CPU0' to a file 'foox.txt'
# run './xr2pcap.rb foox.txt' and you'll have 'packets.txt' and 'packets.pcap'
class XRNPUCounter
PACKET_START = /byte packet/
LINE_RE = /^....: (.{48}).*/
def self.parse lines
c = new
packets = c.parse_lines lines
c.parse_packets packets
end
def parse_lines lines
packet = false
count = -1
packets = []
lines.each do |line|
next if not packet and not line.match PACKET_START
if line.match PACKET_START
packet = true
count += 1
packets[count] = ''
next
end
packets[count] += line
packet = false if not line.match(/^\d/)
end
packets
end
def parse_packets packets
parsed = []
packets.each_with_index do |packet, index|
hex = []
packet.lines.each do |line|
line = line.sub LINE_RE, '\1'
hex << line.split
end
parsed.push "0000000 " + hex.flatten.join(" ")
end
parsed
end
end
if $0 == __FILE__
begin
packets = XRNPUCounter.parse ARGF
File.write "packets.txt", packets.join("\n")
%x(text2pcap packets.txt packets.pcap)
rescue => error
warn error.class.to_s + ": " + error.message
raise if $DEBUG
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment