Skip to content

Instantly share code, notes, and snippets.

@flagranterror
Created August 13, 2012 13:22
Show Gist options
  • Save flagranterror/3340701 to your computer and use it in GitHub Desktop.
Save flagranterror/3340701 to your computer and use it in GitHub Desktop.
Tcpdump output parsing
#!/usr/bin/env ruby
f = File.open("tcpdump.log", 'r')
endpoints = {}
conversations = {}
f.each_line do |l|
if l =~ /([0-9]{2}:[0-9]{2}:[0-9]{2}\..*?) IP.*/
print $1 + ": "
end
if l =~ /.*?([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\..*?) > ([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\..*?):.*/
puts "Src: #{$1} Dst: #{$2}"
endpoints[$1] ? endpoints[$1] += 1 : endpoints[$1] = 1
endpoints[$2] ? endpoints[$2] += 1 : endpoints[$2] = 1
conversations["#{$1} -> #{$2}"] ? conversations["#{$1} -> #{$2}"] += 1 : conversations["#{$1} -> #{$2}"] = 1
end
end
puts
puts "Endpoints:"
puts
endpoints.each_pair do |k,v|
puts " #{k} : #{v} packets"
end
puts
puts "Conversations: "
puts
conversations.each_pair do |k,v|
puts " #{k} : #{v} packets"
end
puts
f.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment