LoginSignup
8
8

More than 5 years have passed since last update.

WiresharkのCLI版tsharkを使ってrubyにキャプチャデータを渡すある一つの方法

Posted at

その方法はパイプ。 IO.popenでtsharkを起動させ、標準出力に表示されるデータをrubyに渡します。 今回はtsharkのオプション設定でxmlを標準出力に出力させています。 (パケットの始まりと終わりがよくわからなかったので、XMLのタグで識別する方法をとりました) XMLになれば、ノードを検索し煮るなり焼くなり好きにできます。 スレッドにしているのはキー入力待ち状態にして、キーが押されたらキャプチャ終了というようにしたかっただけなので、こうしなければ動かないということではありません。

rshark.rb
# coding : windows-31j
require 'rexml/document'
include REXML

cmd = [
  'C:/temp/Wireshark/tshark.exe',
  '-i 1',
  '-T pdml',
].join(" ")

io = IO.popen(cmd, "rb")
Thread.new do
  loop do
    if (_str = io.gets) =~ /^<packet>/
      str = _str
      until (_str =~ /<\/packet>/)
        str << io.gets
      end
      xml_doc = REXML::Document.new(str)
      search_text = 'packet/proto[@name="geninfo"]'
      puts xml_doc.elements[search_text].attributes['size']
    end
  end
end

loop do
  if gets
    io.close
    exit
  end
end
8
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
8