2011年9月8日木曜日

rotating tcpdump

tcpdumpでネットワークのダンプを取り続ける際、任意のタイミングでダンプを保存するファイルを切り替えるシェルスクリプトです。なんか5回ぐらい同じシェルスクリプト書いて、さすがに飽きてきたので、そこそこ使いまわせるように書いたものをgistにおいておきました。

冒頭部分のNIC, FILTERを編集すればあとは深く考えずに使えるはずです。まあ、一瞬で読めるので、解説も何も無いですけど。。

2011.9.9追記
最近のtcpdumpにもrotate機能があると某氏からのご指摘。

tcpdump version 4.1.1のmanより。
-C     Before  writing  a  raw  packet to a savefile, check whether the file is currently larger
       than file_size and, if so, close the current savefile and  open  a  new  one.   Savefiles
       after  the  first  savefile  will have the name specified with the -w flag, with a number
       after it, starting at 1 and continuing upward.  The units of file_size  are  millions  of
       bytes (1,000,000 bytes, not 1,048,576 bytes).

-G     If  specified,  rotates  the  dump file specified with the -w option every rotate_seconds
       seconds.  Savefiles will have the name specified by -w which should include a time format
       as  defined by strftime(3).  If no time format is specified, each new file will overwrite
       the previous.

-Cオプションがファイルサイズベースのrotate、-Gオプションが時間ベースのrotateとのことです。-Gオプションはちゃんとファイル名に時間までいれてくれるらしいが、time formatは自分でstrftime書式で指定しないとダメらしいですね。たとえば

tcpdump -w /tmp/timed_dumps.%s -G 10

のように書くと

% ls /tmp/
timed_dumps.1315521248
timed_dumps.1315521259
timed_dumps.1315521270

というようにファイルが生成されていくようです。任意のタイミングではrotateできないけど、とても便利そうです。

0 件のコメント:

コメントを投稿