ngrep

    ngrep命令 是grep命令的网络版,他力求更多的grep特征,用于搜寻指定的数据包。正由于安装ngrep需用到libpcap库, 所以支持大量的操作系统和网络协议。能识别TCP、UDP和ICMP包,理解bpf的过滤机制。

    ngrep命令的下载地址: install libpcap`完全安装libpcap,注意有时候用libpcap安装包安装的不完整会影响ngrep的使用。

    如果yum无法安装就用以下步骤安装libpcap:

    注:configure时是遇到 please wipe out all unused pcap installations,添加以下选项:

    在安装后输入ngrep来验证下安装是否成功。

    1. ngrep <-LhNXViwqpevxlDtTRM> <-IO pcap_dump> <-n num> <-d dev> <-A num>
    2. <-P char> <-F file> <match expression> <bpf filter>

    捕捉cloudian:18080端口的request和response,-W byline用来解析包中的换行符,否则包里的所有数据都是连续的,可读性差。-d lo是监听本地网卡:

    1. ngrep -W byline -d lo port 18080

    可以用-d any来捕捉所有的包:

    捕获字符串.flv,比如要查看在Web Flash 视频中的.flv文件的下载地址:

    1. ngrep -d3 -N -q \.flv
    2. interface: \Device\TNT_40_1_{670F6B50-0A13-4BAB-9D9E-994A833F5BA9} (10.132.0.0/2
    3. 55.255.192.0)
    4. match: \.flv

    打开一个视频页面:

    1. 97.flv HTTP/1.1..accept: */*..Referer: http://www.56.com/flashApp/v_player_
    2. site.swf..x-flash-version: 9,0,45,0..UA-CPU: x86..Accept-Encoding: gzip, de
    3. flate..User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET
    4. CLR 2.0.50727; .NET CLR 3.0.04506.30)..host: f59.r.56.com..Connection: Keep
    5. -Alive..Cookie: whistoryview=23423759-23635627-23423344-23171935-23058374-2
    6. 3081156-23207350-22395727-; geoip=............; wl_all_s=y....

    加个-W byline参数后,将解析包中的换行符: