linux 的 grep 命令 和 ngrep 命令
基本 grep 使用
[root@www ~]# grep [-acinv] [--color=auto] '搜尋字符串' filename 選項與參數(shù): -a :將 binary 文件以 text 文件的方式搜尋數(shù)據(jù) -c 只輸出匹配行的計數(shù)。計算找到 '搜尋字符串' 的次數(shù) -h 查詢多文件時不顯示文件名。 -i 不區(qū)分大小寫(只適用于單字符)。所以大小寫視為相同 -l 查詢多文件時只輸出包含匹配字符的文件名。 -n 顯示匹配行及行號。 -s 不顯示不存在或無匹配文本的錯誤信息。 -v :反向選擇,亦即顯示出沒有 '搜尋字符串' 內(nèi)容的那一行!顯示不包含匹配文本的所有行。 --color=auto :可以將找到的關(guān)鍵詞部分加上顏色的顯示喔!將/etc/passwd,有出現(xiàn) root 的行取出來 # grep root /etc/passwd將/etc/passwd,有出現(xiàn) root 的行取出來,同時顯示這些行在/etc/passwd的行號 # grep -n root /etc/passwd 在關(guān)鍵字的顯示方面,grep 可以使用 --color=auto 來將關(guān)鍵字部分使用顏色顯示。 這可是個很不錯的功能啊!但是如果每次使用 grep 都得要自行加上 --color=auto 又顯的很麻煩~ 此時那個好用的 alias 就得來處理一下啦!你可以在 ~/.bashrc 內(nèi)加上這行:『alias grep='grep --color=auto'』再以『 source ~/.bashrc 』來立即生效即可喔! 這樣每次運行 grep 他都會自動幫你加上顏色顯示啦將/etc/passwd,將沒有出現(xiàn) root 的行取出來 # grep -v root /etc/passwd將/etc/passwd,將沒有出現(xiàn) root 和nologin的行取出來 # grep -v root /etc/passwd | grep -v nologin用 dmesg 列出核心信息,再以 grep 找出內(nèi)含 eth 那行,要將捉到的關(guān)鍵字顯色,且加上行號來表示: [root@www ~]# dmesg | grep -n --color=auto 'eth'用 dmesg 列出核心信息,再以 grep 找出內(nèi)含 eth 那行,在關(guān)鍵字所在行的前兩行與后三行也一起捉出來顯示 [root@www ~]# dmesg | grep -n -A3 -B2 --color=auto 'eth'根據(jù)文件內(nèi)容遞歸查找目錄# grep ‘energywise’ * #在當前目錄搜索帶'energywise'行的文件 # grep -r ‘energywise’ * #在當前目錄及其子目錄下搜索'energywise'行的文件 # grep -l -r ‘energywise’ * #在當前目錄及其子目錄下搜索'energywise'行的文件,但是不顯示匹配的行,只顯示匹配的文件 這幾個命令很使用,是查找文件的利器。grep與正規(guī)表達式
字符類 字符類的搜索:如果我想要搜尋 test 或 taste 這兩個單字時,可以發(fā)現(xiàn)到,其實她們有共通的 't?st' 存在~這個時候,我可以這樣來搜尋: [root@www ~]# grep -n 't[ae]st' regular_express.txt字符類的反向選擇 [^] :如果想要搜索到有 oo 的行,但不想要 oo 前面有 g,如下 [root@www ~]# grep -n '[^g]oo' regular_express.txt [root@www ~]# grep -n '[^a-z]oo' regular_express.txt [root@www ~]# grep -n '[0-9]' regular_express.txt行首與行尾字節(jié) ^ $ 行首字符:如果我想要讓 the 只在行首列出呢? 這個時候就得要使用定位字節(jié)了!我們可以這樣做: [root@www ~]# grep -n '^the' regular_express.txt 如果我不想要開頭是英文字母,則可以是這樣: [root@www ~]# grep -n '^[^a-zA-Z]' regular_express.txt ^ 符號,在字符類符號(括號[])之內(nèi)與之外是不同的! 在 [] 內(nèi)代表『反向選擇』,在 [] 之外則代表定位在行首的意義!那如果我想要找出來,行尾結(jié)束為小數(shù)點 (.) 的那一行: [root@www ~]# grep -n '\.$' regular_express.txt 特別注意到,因為小數(shù)點具有其他意義(底下會介紹),所以必須要使用轉(zhuǎn)義字符(\)來加以解除其特殊意義! 找出空白行: [root@www ~]# grep -n '^$' regular_express.txt 因為只有行首跟行尾 (^$),所以,這樣就可以找出空白行啦!任意一個字節(jié) . 與重復字節(jié) * 這兩個符號在正則表達式的意義如下: . (小數(shù)點):代表『一定有一個任意字節(jié)』的意思; * (星號):代表『重復前一個字符, 0 到無窮多次』的意思,為組合形態(tài) 假設我需要找出 g??d 的字串,亦即共有四個字節(jié), 起頭是 g 而結(jié)束是 d ,我可以這樣做: [root@www ~]# grep -n 'g..d' regular_express.txt如果我想要字串開頭與結(jié)尾都是 g,但是兩個 g 之間僅能存在至少一個 o ,亦即是 gog, goog, gooog.... 等等,那該如何? [root@www ~]# grep -n 'goo*g' regular_express.txt如果我想要找出 g 開頭與 g 結(jié)尾的行,當中的字符可有可無 [root@www ~]# grep -n 'g.*g' regular_express.txt限定連續(xù) RE 字符范圍 {} 我們可以利用 . 與 RE 字符及 * 來配置 0 個到無限多個重復字節(jié), 那如果我想要限制一個范圍區(qū)間內(nèi)的重復字節(jié)數(shù)呢? 舉例來說,我想要找出兩個到五個 o 的連續(xù)字串,該如何作?這時候就得要使用到限定范圍的字符 {} 了。 但因為 { 與 } 的符號在 shell 是有特殊意義的,因此, 我們必須要使用字符 \ 來讓他失去特殊意義才行。 至於 {} 的語法是這樣的,假設我要找到兩個 o 的字串,可以是: [root@www ~]# grep -n 'o\{2\}' regular_express.txt假設我們要找出 g 后面接 2 到 5 個 o ,然后再接一個 g 的字串,他會是這樣: [root@www ~]# grep -n 'go\{2,5\}g' regular_express.txt如果我想要的是 2 個 o 以上的 goooo....g 呢?除了可以是 gooo*g ,也可以是: [root@www ~]# grep -n 'go\{2,\}g' regular_express.txt擴展grep(grep -E 或者 egrep)
使用擴展grep的主要好處是增加了額外的正則表達式元字符集。grep 同時滿足多個關(guān)鍵字和滿足任意關(guān)鍵字 ① grep -E "word1|word2|word3" file.txt滿足任意條件(word1、word2和word3之一)將匹配。 ② grep word1 file.txt | grep word2 |grep word3必須同時滿足三個條件(word1、word2和word3)才匹配。1、或操作 grep -E '123|abc' filename // 找出文件(filename)中包含123或者包含abc的行 egrep '123|abc' filename // 用egrep同樣可以實現(xiàn) awk '/123|abc/' filename // awk 的實現(xiàn)方式 2、與操作 grep pattern1 files | grep pattern2 //顯示既匹配 pattern1 又匹配 pattern2 的行。 3、其他操作 grep -i pattern files //不區(qū)分大小寫地搜索。默認情況區(qū)分大小寫, grep -l pattern files //只列出匹配的文件名, grep -L pattern files //列出不匹配的文件名, grep -w pattern files //只匹配整個單詞,而不是字符串的一部分(如匹配‘magic’,而不是‘magical’), grep -C number pattern files //匹配的上下文分別顯示[number]行,打印所有包含NW或EA的行。如果不是使用egrep,而是grep,將不會有結(jié)果查出。 # egrep 'NW|EA' testfile 對于標準grep,如果在擴展元字符前面加\,grep會自動啟用擴展選項-E。 #grep 'NW\|EA' testfile搜索所有包含0個或1個小數(shù)點字符的行。 # egrep '2\.?[0-9]' testfile # grep -E '2\.?[0-9]' testfile # grep '2\.\?[0-9]' testfile 搜索一個或者多個連續(xù)的no的行。 # egrep '(no)+' testfile # grep -E '(no)+' testfile # grep '\(no\)\+' testfile #3個命令返回相同結(jié)果,
ngrep 命令
參考:http://man.linuxde.net/ngrep
? ? ? ? ngrep命令是grep(grep是在文本中搜索字符串的工具)命令的網(wǎng)絡版,他力求更多的grep特征,用于搜尋指定的數(shù)據(jù)包。正由于安裝ngrep需用到libpcap庫, 所以支持大量的操作系統(tǒng)和網(wǎng)絡協(xié)議。能識別TCP、UDP和ICMP包,理解 bpf 的過濾機制。
? ? ? ? 分析網(wǎng)絡數(shù)據(jù)包,有Wireshark,它有著上千種設定、過濾器以及配置選項。它還有一個命令行版本Tshark。如果只是針對簡單的任務,Wireshark就太重量級了,所以除非需要更強大的功能,一般情況下就用ngrep來處理了。Ngrep可以讓你像類似grep處理文件的方式來處理網(wǎng)絡封包。
伯克利包過濾(Berkeley Packet Filter,BPF)語言:http://www.cnblogs.com/zhongxinWang/p/4303153.html
ngrep參數(shù)
root@kali:~# man ngrep用法: ngrep <-hNXViwqpevxlDtTRM> <-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>-h 幫助-V 版本信息-q 靜默模式,如果沒有此開關(guān),未匹配的數(shù)據(jù)包都以“#”顯示-e 顯示空數(shù)據(jù)包-i 忽略大小寫-v 反轉(zhuǎn)匹配。 ngrep -v '' port 23 // 顯示除telnet的數(shù)據(jù)包,-v意為反轉(zhuǎn)。-R is don't do privilege revocation logic-x 以16進制格式顯示-X 以16進制格式匹配-w 整字匹配(is word-regex)-p 不使用混雜模式-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 僅進行單行匹配-I pcap_dump 從捕獲的數(shù)據(jù)包文件pcap_dump中讀取數(shù)據(jù)進行匹配-O pcap_dump 將匹配的數(shù)據(jù)保存到pcap格式的文件pcap_dump中-n num 僅捕獲指定數(shù)目的數(shù)據(jù)包,然后退出。-A num 匹配到數(shù)據(jù)包后,Dump指定數(shù)目的數(shù)據(jù)包-s snaplen 設置 bpf caplen(default 65536)-S limitlen 在匹配的包上設置 上限長度-W normal | byline | single | none 設置dump格式。byline是解析包中的換行符 (normal, byline, single, none) 。加個-W byline參數(shù)后,將解析包中的換行符-c cols 強制顯示列的寬度-P char 將不可打印的顯示字符設置為指定的字符-F file 從文件中讀取 bpf filter-N 顯示由IANA定義的子協(xié)議號-d dev ngrep會選擇一個默認的網(wǎng)絡接口進行監(jiān)聽,使用 -d 選項可以指定接口進行監(jiān)聽。 -d any 捕獲所有的包-K num 殺死匹配的 TCP 連接(類似 tcpkill)。數(shù)值參數(shù)控制發(fā)送了多少個RST段。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
相當于:
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.
實例
ngrep -d eth0 -W byline host 192.168.1.9 // 抓本機eth0 與192.168.1.9的通信信息,并且以行來打印出來ngrep -W byline host 192.168.1.8 and port 80 // 抓本機與192.168.1.8的通信端口為80(本機)的信息ngrep -W byline host 192.168.1.8 or host 192.168.1.9 port 80 // 抓本機與192.168.1.8和192.168.1.9的通信,并且本地端口為80ngrep host 192.168.1.8 udp // 抓udp包ngrep -W byline 'GET /' 'tcp and dst port 80' -d eth1 | awk -v RS="#+" -v FS="\n" '{ print length() }' // 統(tǒng)計請求頭長度ngrep -W byline 'GET /' 'tcp and dst port 80' -d eth1 | awk -v RS="#+" -v FS="\n" 'length() > 1000' // 查詢一下大于 1K 的請求頭捕獲字符串.flv,比如要查看在Web Flash 視頻中的.flv文件的下載地址: ngrep -d3 -N -q \.flv 然后打開一個視頻頁面針對Web流量,幾乎總是想要加上-W byline選項,這會保留換行符,而-q選項可以抑制某些非匹配數(shù)據(jù)包而產(chǎn)生的輸出。
抓取所有包含有GET或POST請求數(shù)據(jù)包的例子:ngrep –q –W byline “^(GET|POST) .*”
可以傳入附加的報文過濾選項,比如限制匹配的報文只針對某個特定的主機,IP或端口。這里我們把所有流經(jīng)Google的流量做一個過濾,只針對80端口且報文中包含“search”。例子:ngrep –q –W byline “search” host www.google.com and port 80
總結(jié)
以上是生活随笔為你收集整理的linux 的 grep 命令 和 ngrep 命令的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java8 Stream详解~Strea
- 下一篇: 内网穿透 --- frp