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

语法

 
1
2
3
ngrep <-LhNXViwqpevxlDtTRM> <-IO pcap_dump> <-n num> <-d dev> <-A num>
                        <-s snaplen> <-S limitlen> <-W normal|byline|single|none> <-c cols>
                        <-P char> <-F file> <match expression> <bpf filter>

选项

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
-h     is help/usage
-V    is version information
-q    is be quiet (don't print packet reception hash marks)静默模式,如果没有此开关,未匹配的数据包都以“#”显示
-e    is show empty packets 显示空数据包
-i     is ignore case 忽略大小写
-v    is invert match 反转匹配
-R   is don't do privilege revocation logic
-x    is print in alternate hexdump format 以16进制格式显示
-X   is interpret match expression as hexadecimal 以16进制格式匹配
-w   is word-regex (expression must match as a word) 整字匹配
-p   is don't go into promiscuous mode 不使用混杂模式
-l     is make stdout line buffered
-D   is replay pcap_dumps with their recorded time intervals
-t     is print timestamp every time a packet is matched在每个匹配的包之前显示时间戳
-T    is print delta timestamp every time a packet is matched显示上一个匹配的数据包之间的时间间隔
-M   is don't do multi-line match (do single-line match instead)仅进行单行匹配
-I     is read packet stream from pcap format file pcap_dump 从文件中读取数据进行匹配
-O   is dump matched packets in pcap format to pcap_dump 将匹配的数据保存到文件
-n    is look at only num packets 仅捕获指定数目的数据包进行查看
-A   is dump num packets after a match匹配到数据包后Dump随后的指定数目的数据包
-s    is set the bpf caplen
-S   is set the limitlen on matched packets
-W  is set the dump format (normal, byline, single, none) 设置显示格式byline将解析包中的换行符
-c    is force the column width to the specified size 强制显示列的宽度
-P   is set the non-printable display char to what is specified
-F   is read the bpf filter from the specified file 使用文件中定义的bpf(Berkeley Packet Filter)
-N   is show sub protocol number 显示由IANA定义的子协议号
-d   is use specified device (index) instead of the pcap default

Allowable primitives are:
dst host host
True if the IP destination field of the packet is host, which may be either an address or a name.

src host host
True if the IP source field of the packet is host.

host host
True if either the IP source or destination of the packet is host. Any of the above host expressions can be prepended with the
keywords, ip, arp, or rarp as in:
ip host host
which is equivalent to:

ether dst ehost
True if the ethernet destination address is ehost. Ehost may be either a name from /etc/ethers or a number (see ethers(3N) for
numeric format).

ether src ehost
True if the ethernet source address is ehost.

ether host ehost
True if either the ethernet source or destination address is ehost.

gateway host
True if the packet used host as a gateway. I.e., the ethernet source or destination address was host but neither the IP source
nor the IP destination was host. Host must be a name and must be found in both /etc/hosts and /etc/ethers. (An equivalent
expression is
ether host ehost and not host host
which can be used with either names or numbers for host / ehost.)

dst net net
True if the IP destination address of the packet has a network number of net. Net may be either a name from /etc/networks or a
network number (see networks(4) for details).

src net net
True if the IP source address of the packet has a network number of net.

net net
True if either the IP source or destination address of the packet has a network number of net.

net net mask mask
True if the IP address matches net with the specific netmask. May be qualified with src or dst.

net net/len
True if the IP address matches net a netmask len bits wide. May be qualified with src or dst.

dst port port
True if the packet is ip/tcp or ip/udp and has a destination port value of port. The port can be a number or a name used in
/etc/services (see tcp(4P) and udp(4P)). If a name is used, both the port number and protocol are checked. If a number or
ambiguous name is used, only the port number is checked (e.g., dst port 513 will print both tcp/login traffic and udp/who traf-
fic, and port domain will print both tcp/domain and udp/domain traffic).
src port port
True if the packet has a source port value of port.

port port
True if either the source or destination port of the packet is port. Any of the above port expressions can be prepended with
the keywords, tcp or udp, as in:
tcp src port port
which matches only tcp packets whose source port is port.

less length
True if the packet has a length less than or equal to length. This is equivalent to:
len <= length.

greater length
True if the packet has a length greater than or equal to length. This is equivalent to:
len >= length.

ip proto protocol
True if the packet is an ip packet (see ip(4P)) of protocol type protocol. Protocol can be a number or one of the names tcp,
udp or icmp. Note that the identifiers tcp and udp are also keywords and must be escaped via backslash (\), which is \\ in the
C-shell.

ip broadcast
True if the packet is an IP broadcast packet. It checks for both the all-zeroes and all-ones broadcast conventions, and looks
up the local subnet mask.

ip multicast
True if the packet is an IP multicast packet.

ip Abbreviation for:
ether proto ip

tcp, udp, icmp
Abbreviations for:
ip proto p
where p is one of the above protocols.

实例

抓本机eth0 与192.168.1.9的通信信息,并且以行来打印出来

 
1
#ngrep -d eth0 -W byline host 192.168.1.9

抓本机与192.168.1.8的通信端口为80(本机)的信息

 
1
# ngrep -W byline host 192.168.1.8 and port 80

抓本机与192.168.1.8和192.168.1.9的通信,并且本地端口为80

 
1
#ngrep -W byline host 192.168.1.8 or host 192.168.1.9 port 80

抓udp包

 
1
#ngrep host 192.168.1.8 udp

统计请求头长度

 
1
# ngrep -W byline 'GET /' 'tcp and dst port 80' -d eth1 | awk -v RS="#+" -v FS="\n" '{ print length() }'

查询一下大于 1K 的请求头

 
1
# ngrep -W byline 'GET /' 'tcp and dst port 80' -d eth1 |  awk -v RS="#+" -v FS="\n" 'length() > 1000'

ngrep命令用法的更多相关文章

  1. systemctl命令用法详解

    systemctl命令用法详解系统环境:Fedora 16binpath:/bin/systemctlpackage:systemd-units systemctl enable httpd.serv ...

  2. cpio命令用法

    [转自]流浪妖精のSKY    http://www.cnitblog.com/flutist1225/articles/18974.html cpio命令用法 cpio命令     利用cpio 可 ...

  3. shutdown命令用法

    首先我们先创建一个txt文件,添加shutdown -r -f -t 0 ,文件点击另存为,选择所有类型,保存格式为“重启.bat”文件. 说明:shutdown命令用法: /r         关闭 ...

  4. linux中comm命令用法

    linux系统中comm命令用法详解 linux系统下的comm命令是一个非常实用的文件对比命令. comm命令功能:   选择或拒绝两个已排序的文件的公共的行. comm命令语法:comm [-12 ...

  5. Ubuntu kill命令用法详解

    转自:Ubuntu kill命令用法详解 1. kill   作用:根据进程号杀死进程   用法: kill [信号代码] 进程ID   root@fcola:/# ps -ef | grep sen ...

  6. install 命令用法详解

    install 命令用法详解 http://man.linuxde.net/install install命令的作用是安装或升级软件或备份数据,它的使用权限是所有用户.install命令和cp命令类似 ...

  7. which、whereis、locate、find 命令用法

    which.whereis.locate.find 命令用法   大部分转自http://312788172.iteye.com/blog/730280,有修改 我们经常在linux要查找某个文件,但 ...

  8. sed命令用法详解

    sed命令用法 sed是一种流编辑器,它是文本处理中非常有用的工具,能够完美的配合正则表达式使用,功能不同凡响.处理时,把当前处理的行存储在临时缓冲区中,称为『模式空间』(pattern space) ...

  9. linux的strace命令用法

    strace命令用法 调用:strace [ -dffhiqrtttTvxx ] [ -acolumn ] [ -eexpr ] …[ -ofile ] [ -ppid ] … [ -sstrsize ...

随机推荐

  1. 61. 旋转链表-leetcode

    给定一个链表,旋转链表,将链表每个节点向右移动 k 个位置,其中 k 是非负数. 示例 1: 输入: 1->2->3->4->5->NULL, k = 2 输出: 4-& ...

  2. Java运行时,指定程序文件的编码

    在命令行cmd里面运行 java -jar test.jar的时候,发现里面执行的汉字发生乱码.原来指定的是UTF-8. 解决如下: java -Dfile.encoding=UTF-8 -jar - ...

  3. 面试题----gcc的编译流程

    gcc编译流程 一.    编译与处理指令: gcc -E hello.c -o a.c 如果不使用-o指定输出的文件,会默认输出到终端.所以建议使用同时使用-o选项. 还要注意:编译时会保留#pra ...

  4. Hibernate关联关系映射之一对一(主键关联)

    在业务成的域模型中,类和类之间最普遍的关系就是关联关系,而关联也是有方向的. 就以例子来说明:一个人对应一张身份证.对其进行增删改. 对于人在数据创建表的时候,我们就给他两个字段,一个是id号,一个就 ...

  5. Java 8 新特性-菜鸟教程 (0) -Java 8 新特性

    Java 8 新特性 Java 8 (又称为 jdk 1.8) 是 Java 语言开发的一个主要版本. Oracle 公司于 2014 年 3 月 18 日发布 Java 8 ,它支持函数式编程,新的 ...

  6. java虚拟机学习-Java常量池理解与总结(13-2)

    一.相关概念 什么是常量用final修饰的成员变量表示常量,值一旦给定就无法改变!final修饰的变量有三种:静态变量.实例变量和局部变量,分别表示三种类型的常量. Class文件中的常量池在Clas ...

  7. docker部署jenkinsci blueocean

    1.使用docker-compose # cat docker-compose.ymlversion: '2'services: jenkinsci: image: jenkinsci/blueoce ...

  8. mysql LAST_INSERT_ID详解

    http://blog.sina.com.cn/s/blog_5b5460eb0100nwvo.html LAST_INSERT_ID() LAST_INSERT_ID(expr) 自动返回最后一个I ...

  9. js闭包应用

    先来看一个例子: function foo() { var a = 10; function bar() { a *= 2; return a; } return bar; } var baz = f ...

  10. Netty 内存回收之 noCleaner 策略

    前言 对于堆外内存,使用 System.gc() 是不靠谱的,依赖老年代 FGC 也是不靠谱的,而且大部分调优指南都设置了 -DisableExplicitGC 禁用 System.gc().所以主动 ...