Skip to content

Instantly share code, notes, and snippets.

@sween
Created June 1, 2009 02:09
Show Gist options
  • Save sween/121138 to your computer and use it in GitHub Desktop.
Save sween/121138 to your computer and use it in GitHub Desktop.
# Ron Sweeney
# Dev Arboretum
# Original Post: http://www.clubpacswestmi.net/articles/2007/4/1/a-ruby-hl7-sniffer
require 'pcap'
require 'Ethernet'
require 'rubygems'
require 'ruby-hl7'
def printable(str)
return "" unless str
str.strip.dump.gsub(/\\[0-9]/,'')
end
dev = Pcap.lookupdev
capture = Pcap::Capture.open_live( dev )
capture.loop do |packet|
eth = Ethernet.new( packet.raw_data )
if eth.payload =~ /\vMSH(.*)\r/
puts "==== HL7 Message Detected on Wire ====\n"
puts "Src: #{eth.src} --=> Dest: #{eth.dest}\n"
woot = printable(eth.payload.to_s)
gar2 = woot.split("\\v")
msg = gar2[1].split("\\r")
msg.pop
hl7msg = HL7::Message.new(msg)
ele = hl7msg[:MSH].element_delim
ite = hl7msg[:MSH].item_delim
puts "Message type: %s\n" % hl7msg[:MSH].message_type
puts "HL7 Version: %s\n\n" % hl7msg[:MSH].version_id
hl7msg.each do |liner|
puts liner
#elements = liner.to_s.split(ele)
#elements.each do |eleprint|
#puts "\t" + eleprint
end
puts "\n" + "=" * 38 + "\n"
end
end
capture.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment