最近知ったんだけど、かなり便利くね?もしかして常識?
installにはlibpcapがいる。
# yum install -y --enablerepo=epel ngrep
追記
今更知ったけど、ASCIIで表示するだけならtcpdump -s0 -A だけで良いので(ngrep -W byline とほぼ同じ?)、grep 的なことしないならtcpdump で十分な気がする。
man tcpdump
-A Print each packet (minus its link level header) in ASCII. Handy for capturing web pages.
ngrep
用途によってはtcpdumpよりお手軽にdebugできる。コマンドはtcpdumpのようなbpf(Berkeley Packet Filter)書式が使える(host、src、dst、portなどの指定)。さらに、名前の通り、grepライクな正規表現とオプションも使える。(-iでignore case、-wで単語検索、など)
interfaceを指定するときは-dで。(tcpdumpのときは-i)
実行例
192.168.24.101のサーバでapache2を動かして、ngrepする。
# ngrep -W byline -q port 80 interface: eth0 (192.168.24.0/255.255.255.0) filter: (ip or ip6) and ( port 80 ) T 192.168.24.100:59837 -> 192.168.24.101:80 [AP] GET / HTTP/1.1. Host: 192.168.24.101. User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; ja-JP-mac; rv:1.9.0.5) Gecko/2008120121 Firefox/3.0.5. Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8. Accept-Language: ja,en-us;q=0.7,en;q=0.3. Accept-Encoding: gzip,deflate. Accept-Charset: Shift_JIS,utf-8;q=0.7,*;q=0.7. Keep-Alive: 300. Connection: keep-alive. Pragma: no-cache. Cache-Control: no-cache. . T 192.168.24.101:80 -> 192.168.24.100:59837 [AP] HTTP/1.1 200 OK. Date: Tue, 27 Jan 2009 15:43:30 GMT. Server: Apache/2.2.11 (Unix). Last-Modified: Sat, 20 Nov 2004 20:16:24 GMT. ETag: "7c7fc-2c-3e9564c23b600". Accept-Ranges: bytes. Content-Length: 44. Keep-Alive: timeout=5, max=100. Connection: Keep-Alive. Content-Type: text/html. . <html><body><h1>It works!</h1></body></html>
ヘッダまで見えるので、cookieがうんぬんなどでdebugするときに便利かも?この例は、ただのindex.htmlへのGETアクセスなので面白くないですが、POSTした場合に威力を発揮するような気がします。あと、URLで絞り込みも出来るし。
# ngrep 'GET /hoge' port 80 # ngrep 'POST /fuga' port 8080