欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  科技

详解Linux系统中字符串搜索命令ngrep的用法

程序员文章站 2023-09-04 21:09:54
这篇文章主要介绍了Linux系统中字符串搜索命令ngrep的用法,ngrep在grep的基础功能上又有所扩展,需要的朋友可以参考下... 15-12-17...

ngrep 是grep的网络版,他力求更多的grep特征,用于搜寻指定的数据包。由于安装ngrep需用到ibpcap库, 所以支持大量的操作系统和网络协议,能识别tcp、udp和icmp包。

安装ngrep
下载地址

复制代码
代码如下:

git clone git://git.code.sf.net/p/ngrep/code ngrep-code

进入目录

复制代码
代码如下:

cd ngrep-code
./configure --with-pcap-includes=/usr/local/include/pcap
make
make install

选项

-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


应用举例:

捕获所有post请求(加个-w byline 参数后,将解析包中的换行符):


复制代码
代码如下:

ranger@ranger:~$ sudo ngrep -q -w byline "(post).*"
interface: eth0 (192.168.122.0/255.255.254.0)
match: (post).*

t 192.168.122.74:46048 -> 140.207.228.58:80 [a]
post /hotel/ota_hotelsearch.asmx?wsdl http/1.1.
content-type: text/xml; charset=utf-8.
soapaction: http://ctrip.com/request.
accept-encoding: gzip, deflate.
content-length: 1330.
accept: */*.
accept-language: zh-cn.
user-agent: mozilla/4.0 (compatible; msie 8.0; windows nt 5.1; trident/4.0).
ua-cpu: x86.
accept-encoding: gzip, deflate.
connection: close.
host: openapi.ctrip.com.
.
<?xml version="1.0" encoding="utf-8"?><soap:envelope xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:xsd="http://www.w3.org/2001/xmlschema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:body> <request xmlns="http://ctrip.com/"> <requestxml>&lt;request&gt;
&lt;header allianceid="***" sid="***" timestamp="1393554304685" requesttype="ota_hotelsearch" signature="b166cdf5422a6da5ba81a08036e938e7"/&gt;
&lt;hotelrequest&gt;
&lt;requestbody xmlns:ns="http://www.opentravel.org/ota/2003/05" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
xmlns:xsd="http://www.w3.org/2001/xmlschema"&gt;
&lt;ns:ota_hotelsearchrq version="1.0" primarylangid="zh"
xsi:schemalocation="http://www.opentravel.org/ota/2003/05 ota_hotelsearchrq.xsd"