http://www.tuicool.com/articles/MzErUbu
2014
背景:当时有用tcpdump抓包的(https://justwinit.cn/post/6406/),这儿主要是能看到mysql的查询,这个功能不错,特转载。
这个还是依赖libpcap的,如下:
Dependency Installed: libpcap.x86_64 14:1.4.0-1.20130826git2dbcaa1.el6 libsmi.x86_64 0:0.4.8-4.el6 Failed: wireshark.x86_64 0:1.8.10-7.el6_5 Complete![root@localhost htdocs]# tshark -s 512 -i eth0 -n -f 'tcp dst port 3306' -R 'mysql.query' -T fields -e mysql.queryRunning as user "root" and group "root". This could be dangerous.Capturing on eth00 packets captured[root@localhost htdocs]# iduid=0(root) gid=0(root) groups=0(root),1(bin),2(daemon),3(sys),4(adm),6(disk),10(wheel)[root@localhost htdocs]# tshark -s 512 -i eth0 -n -f 'tcp dst port 3306' -R 'mysql.query' -T fields -e mysql.queryRunning as user "root" and group "root". This could be dangerous.Capturing on eth0SET NAMES utf8SELECT *\x0aFROM (`tv_nav`)\x0aWHERE `enabled` = 1\x0aORDER BY `order` DESC\x0aLIMIT 0, 20SET NAMES utf8SELECT *\x0aFROM (`tv_nav`)\x0aWHERE `enabled` = 1\x0aORDER BY `order` DESC\x0aLIMIT 0, 20
在Linux下,当我们需要抓取网络数据包分析时,通常是使用tcpdump抓取网络raw数据包存到一个文件,然后下载到本地使用wireshark界面网络分析工具进行网络包分析。
最近才发现,原来wireshark也提供有Linux命令行工具-tshark。tshark不仅有抓包的功能,还带了解析各种协议的能力。下面我们以两个实例来介绍tshark工具。
1、安装方法
CentOS: yum install -y wireshark
Ubuntu: apt-get install -y tshark
2、实时打印当前http请求的url(包括域名)
tshark -s 512 -i eth0 -n -f 'tcp dst port 80' -R 'http.host and http.request.uri' -T fields -e http.host -e http.request.uri -l | tr -d '\t'
下面介绍参数含义:
-s 512 :只抓取前512个字节数据
-i eth0 :捕获eth0网卡
-n :禁止网络对象名称解析
-f 'tcp dst port 80' :只捕捉协议为tcp,目的端口为80的数据包
-R 'http.host and http.request.uri' :过滤出http.host和http.request.uri
-T fields -e http.host -e http.request.uri :打印http.host和http.request.uri
-l :输出到标准输出
3、实时打印当前mysql查询语句
tshark -s 512 -i eth0 -n -f 'tcp dst port 3306' -R 'mysql.query' -T fields -e mysql.query
下面介绍参数含义:
-s 512 :只抓取前512个字节数据
-i eth0 :捕获eth0网卡
-n :禁止网络对象名称解析
-f 'tcp dst port 3306' :只捕捉协议为tcp,目的端口为3306的数据包
-R 'mysql.query' :过滤出mysql.query
-T fields -e mysql.query :打印mysql查询语句
tshark使用-f来指定捕捉包过滤规则,规则与tcpdump一样,可以通过命令man pcap-filter来查得。
tshark使用-R来过滤已捕捉到的包,与界面板wireshark的左上角Filter一致。
转自:https://www.centos.bz/2014/07/linux-commandline-capture-packets-tshark-wireshark/
联系客服