Created
June 15, 2011 06:56
-
-
Save smokeymonkey/1026611 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby | |
require 'pcap' | |
def cap_data(dev,filstr,count) | |
# [device],[snaplen],[promisc?],[read timeout(ms)] | |
pcaplet = Pcap::Capture.open_live(dev,1460,true,1000) | |
access = Pcap::Filter.new(filstr,pcaplet) | |
pcaplet.setfilter(access) | |
pcaplet.each_packet(count) do |pkt| | |
odata = String.new | |
if pkt.tcp? == true then | |
odata = "[TCP]" | |
odata = odata + pkt.ip_src.to_s + ":" + pkt.tcp_sport.to_s + " -> " | |
odata = odata + pkt.ip_dst.to_s + ":" + pkt.tcp_dport.to_s + " " | |
odata = odata + pkt.tcp_data.to_s | |
p odata | |
elsif pkt.udp? == true then | |
odata = "[UDP]" | |
odata = odata + pkt.ip_src.to_s + ":" + pkt.udp_sport.to_s + " -> " | |
odata = odata + pkt.ip_dst.to_s + ":" + pkt.udp_dport.to_s + " " | |
odata = odata + pkt.udp_data.to_s | |
p odata | |
elsif pkt.ip? == true then | |
odata = "[IP]" | |
odata = odata + pkt.ip_src.to_s + " -> " | |
odata = odata + pkt.ip_dst.to_s ; " " | |
odata = odata + pkt.ip_data.to_s | |
p odata | |
end | |
end | |
pcaplet.close | |
end | |
# [capture device],[filter strings],[count(負の数だと無限繰り返し)] | |
# SSH通信をFilter(リモート作業の関係で) | |
cap_data("eth0","!src port 22 and !dst port 22",-1); | |
exit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment