1. 程式人生 > >七周二次課

七周二次課

抓包 tcpdump tshark wireshark

10.6 監控io性能

iotop命令是一個用來監視磁盤I/O使用狀況的top類工具。iotop具有與top相似的UI,其中包括PID、用戶、I/O、進程等相關信息。Linux下的IO統計工具如iostat,nmon等大多數是只能統計到per設備的讀寫情況,如果你想知道每個進程是如何使用IO的就比較麻煩,使用iotop命令可以很方便的查看。
iotop使用Python語言編寫而成,要求Python2.5(及以上版本)和Linux kernel2.6.20(及以上版本)。iotop提供有源代碼及rpm包,可從其官方主頁下載。
安裝
Ubuntu
apt-get install iotop
CentOS
yum install iotop

編譯安裝
wget http://guichaz.free.fr/iotop/files/iotop-0.4.4.tar.gz
tar zxf iotop-0.4.4.tar.gz
python setup.py build
python setup.py install
語法
iotop(選項)
選項
-o:只顯示有io操作的進程
-b:批量顯示,無交互,主要用作記錄到文件。
-n NUM:顯示NUM次,主要用於非交互式模式。
-d SEC:間隔SEC秒顯示一次。
-p PID:監控的進程pid。
-u USER:監控的進程用戶。
iotop常用快捷鍵:

  1. 左右箭頭:改變排序方式,默認是按IO排序。
  2. r:改變排序順序。
  3. o:只顯示有IO輸出的進程。
  4. p:進程/線程的顯示方式的切換。
  5. a:顯示累積使用量。
  6. q:退出。
    實例
    直接執行iotop就可以看到效果了:
    Total DISK read:       0.00 B/s | Total DISK write:       0.00 B/s
    TID  PRIO  USER     DISK READ  DISK WRITE  SWAPIN     IO>    command
    1 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % init [3]
    2 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [kthreadd]
    3 rt/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [migration/0]
    4 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [ksoftirqd/0]
    5 rt/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [watchdog/0]
    6 rt/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [migration/1]
    7 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [ksoftirqd/1]
    8 rt/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [watchdog/1]
    9 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [events/0]
    10 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [events/1]
    11 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [khelper]
    2572 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [bluetooth]

    10.7 free命令

    free命令可以顯示當前系統未使用的和已使用的內存數目,還可以顯示被內核使用的內存緩沖區。
    語法
    free(選項)
    選項
    -b:以Byte為單位顯示內存使用情況;
    -k:以KB為單位顯示內存使用情況;
    -m:以MB為單位顯示內存使用情況;
    -o:不顯示緩沖區調節列;
    -s<間隔秒數>:持續觀察內存使用狀況;
    -t:顯示內存總和列;
    -V:顯示版本信息。
    實例

    free -m
             total       used       free     shared    buffers     cached
    Mem:          2016       1973         42          0        163       1497
    -/+ buffers/cache:        312       1703
    Swap:         4094          0       4094

    第一部分Mem行解釋:
    total:內存總數;
    used:已經使用的內存數;
    free:空閑的內存數;
    shared:當前已經廢棄不用;
    buffers Buffer:磁盤中的數據調入到內存給cpu處理,需要緩沖;
    cached Page:CPU處理後的數據從內存寫入到磁盤,需要緩存
    關系:total = used + free
    第二部分(-/+ buffers/cache)解釋:
    (-buffers/cache) used內存數:第一部分Mem行中的 used – buffers – cached
    (+buffers/cache) free內存數: 第一部分Mem行中的 free + buffers + cached
    可見-buffers/cache反映的是被程序實實在在吃掉的內存,而+buffers/cache反映的是可以挪用的內存總數。
    第三部分是指交換分區。

    10.8 ps命令

    ps命令用於報告當前系統的進程狀態。可以搭配kill指令隨時中斷、刪除不必要的程序。ps命令是最基本同時也是非常強大的進程查看命令,使用該命令可以確定有哪些進程正在運行和運行的狀態、進程是否結束、進程有沒有僵死、哪些進程占用了過多的資源等等,總之大部分信息都是可以通過執行該命令得到的。
    ps(選項)
    選項
    -a:顯示所有終端機下執行的程序,除了階段作業領導者之外。
    a:顯示現行終端機下的所有程序,包括其他用戶的程序。
    -A:顯示所有程序。
    -c:顯示CLS和PRI欄位。
    c:列出程序時,顯示每個程序真正的指令名稱,而不包含路徑,選項或常駐服務的標示。
    -C<指令名稱>:指定執行指令的名稱,並列出該指令的程序的狀況。
    -d:顯示所有程序,但不包括階段作業領導者的程序。
    -e:此選項的效果和指定"A"選項相同。
    e:列出程序時,顯示每個程序所使用的環境變量。
    -f:顯示UID,PPIP,C與STIME欄位。
    f:用ASCII字符顯示樹狀結構,表達程序間的相互關系。
    -g<群組名稱>:此選項的效果和指定"-G"選項相同,當亦能使用階段作業領導者的名稱來指定。
    g:顯示現行終端機下的所有程序,包括群組領導者的程序。
    -G<群組識別碼>:列出屬於該群組的程序的狀況,也可使用群組名稱來指定。
    h:不顯示標題列。
    -H:顯示樹狀結構,表示程序間的相互關系。
    -j或j:采用工作控制的格式顯示程序狀況。
    -l或l:采用詳細的格式來顯示程序狀況。
    L:列出欄位的相關信息。
    -m或m:顯示所有的執行緒。
    n:以數字來表示USER和WCHAN欄位。
    -N:顯示所有的程序,除了執行ps指令終端機下的程序之外。
    -p<程序識別碼>:指定程序識別碼,並列出該程序的狀況。
    p<程序識別碼>:此選項的效果和指定"-p"選項相同,只在列表格式方面稍有差異。
    r:只列出現行終端機正在執行中的程序。
    -s<階段作業>:指定階段作業的程序識別碼,並列出隸屬該階段作業的程序的狀況。
    s:采用程序信號的格式顯示程序狀況。
    S:列出程序時,包括已中斷的子程序資料。
    -t<終端機編號>:指定終端機編號,並列出屬於該終端機的程序的狀況。
    t<終端機編號>:此選項的效果和指定"-t"選項相同,只在列表格式方面稍有差異。
    -T:顯示現行終端機下的所有程序。
    -u<用戶識別碼>:此選項的效果和指定"-U"選項相同。
    u:以用戶為主的格式來顯示程序狀況。
    -U<用戶識別碼>:列出屬於該用戶的程序的狀況,也可使用用戶名稱來指定。
    U<用戶名稱>:列出屬於該用戶的程序的狀況。
    v:采用虛擬內存的格式顯示程序狀況。
    -V或V:顯示版本信息。
    -w或w:采用寬闊的格式來顯示程序狀況。
    x:顯示所有程序,不以終端機來區分。
    X:采用舊式的Linux i386登陸格式顯示程序狀況。
    -y:配合選項"-l"使用時,不顯示F(flag)欄位,並以RSS欄位取代ADDR欄位 。
    -<程序識別碼>:此選項的效果和指定"p"選項相同。
    --cols<每列字符數>:設置每列的最大字符數。
    --columns<每列字符數>:此選項的效果和指定"--cols"選項相同。
    --cumulative:此選項的效果和指定"S"選項相同。
    --deselect:此選項的效果和指定"-N"選項相同。
    --forest:此選項的效果和指定"f"選項相同。
    --headers:重復顯示標題列。
    --help:在線幫助。
    --info:顯示排錯信息。
    --lines<顯示列數>:設置顯示畫面的列數。
    --no-headers:此選項的效果和指定"h"選項相同,只在列表格式方面稍有差異。
    --group<群組名稱>:此選項的效果和指定"-G"選項相同。
    --Group<群組識別碼>:此選項的效果和指定"-G"選項相同。
    --pid<程序識別碼>:此選項的效果和指定"-p"選項相同。
    --rows<顯示列數>:此選項的效果和指定"--lines"選項相同。
    --sid<階段作業>:此選項的效果和指定"-s"選項相同。
    --tty<終端機編號>:此選項的效果和指定"-t"選項相同。
    --user<用戶名稱>:此選項的效果和指定"-U"選項相同。
    --User<用戶識別碼>:此選項的效果和指定"-U"選項相同。
    --version:此選項的效果和指定"-V"選項相同。
    --widty<每列字符數>:此選項的效果和指定"-cols"選項相同。
    ? ps 查看系統進程
    ? 用法:ps aux、ps -elf
    ? STAT部分說明
    ? D 不能中斷的進程
    ? R run狀態的進程
    ? S sleep狀態的進程
    ? T 暫停的進程
    ? Z 僵屍進程
    ? < 高優先級進程
    ? N 低優先級進程
    ? L 內存中被鎖了內存分頁
    ? s 主進程
    ? l 多線程進程
    ? + 前臺進程

#ps -A 顯示進程信息
PID TTY     TIME CMD
  1 ?    00:00:02 init
  2 ?    00:00:00 kthreadd
  3 ?    00:00:00 migration/0
  4 ?    00:00:00 ksoftirqd/0
  5 ?    00:00:00 watchdog/0
  6 ?    00:00:00 events/0
  7 ?    00:00:00 cpuset
  8 ?    00:00:00 khelper
  9 ?    00:00:00 netns
  10 ?    00:00:00 async/mgr
  11 ?    00:00:00 pm
  12 ?    00:00:00 sync_supers
  13 ?    00:00:00 bdi-default
  14 ?    00:00:00 kintegrityd/0
  15 ?    00:00:02 kblockd/0
  16 ?    00:00:00 kacpid
  17 ?    00:00:00 kacpi_notify
  18 ?    00:00:00 kacpi_hotplug
  19 ?    00:00:27 ata/0

……省略部分結果

30749 pts/0  00:00:15 gedit
30886 ?    00:01:10 qtcreator.bin
30894 ?    00:00:00 qtcreator.bin
31160 ?    00:00:00 dhclient
31211 ?    00:00:00 aptd
31302 ?    00:00:00 sshd
31374 pts/2  00:00:00 bash
31396 pts/2  00:00:00 ps

顯示指定用戶信息

#ps -u root //顯示root進程用戶信息
 PID TTY     TIME CMD
  1 ?    00:00:02 init
  2 ?    00:00:00 kthreadd
  3 ?    00:00:00 migration/0
  4 ?    00:00:00 ksoftirqd/0
  5 ?    00:00:00 watchdog/0
  6 ?    00:00:00 events/0
  7 ?    00:00:00 cpuset
  8 ?    00:00:00 khelper
  9 ?    00:00:00 netns
  10 ?    00:00:00 async/mgr
  11 ?    00:00:00 pm
  12 ?    00:00:00 sync_supers
  13 ?    00:00:00 bdi-default
  14 ?    00:00:00 kintegrityd/0
  15 ?    00:00:02 kblockd/0
  16 ?    00:00:00 kacpid
……省略部分結果
30487 ?    00:00:06 gnome-terminal
30488 ?    00:00:00 gnome-pty-helpe
30489 pts/0  00:00:00 bash
30670 ?    00:00:00 debconf-communi
30749 pts/0  00:00:15 gedit
30886 ?    00:01:10 qtcreator.bin
30894 ?    00:00:00 qtcreator.bin
31160 ?    00:00:00 dhclient
31211 ?    00:00:00 aptd
31302 ?    00:00:00 sshd
31374 pts/2  00:00:00 bash
31397 pts/2  00:00:00 ps

顯示所有進程信息,連同命令行

#ps -ef //顯示所有命令,連帶命令行
UID    PID PPID C STIME TTY     TIME CMD
root     1   0 0 10:22 ?    00:00:02 /sbin/init
root     2   0 0 10:22 ?    00:00:00 [kthreadd]
root     3   2 0 10:22 ?    00:00:00 [migration/0]
root     4   2 0 10:22 ?    00:00:00 [ksoftirqd/0]
root     5   2 0 10:22 ?    00:00:00 [watchdog/0]
root     6   2 0 10:22 ?    /usr/lib/NetworkManager
……省略部分結果
root   31302 2095 0 17:42 ?    00:00:00 sshd: root@pts/2
root   31374 31302 0 17:42 pts/2  00:00:00 -bash
root   31400   1 0 17:46 ?    00:00:00 /usr/bin/python /usr/sbin/aptd
root   31407 31374 0 17:48 pts/2  00:00:00 ps -ef

10.9 查看網絡狀態

netstat

用於顯示網絡狀態。

利用netstat指令可讓你得知整個Linux系統的網絡情況。

語法
netstat [-acCeFghilMnNoprstuvVwx][-A<網絡類型>][--ip]
參數說明:

-a或--all 顯示所有連線中的Socket。
-A<網絡類型>或--<網絡類型> 列出該網絡類型連線中的相關地址。
-c或--continuous 持續列出網絡狀態。
-C或--cache 顯示路由器配置的快取信息。
-e或--extend 顯示網絡其他相關信息。
-F或--fib 顯示FIB。
-g或--groups 顯示多重廣播功能群組組員名單。
-h或--help 在線幫助。
-i或--interfaces 顯示網絡界面信息表單。
-l或--listening 顯示監控中的服務器的Socket。
-M或--masquerade 顯示偽裝的網絡連線。
-n或--numeric 直接使用IP地址,而不通過域名服務器。
-N或--netlink或--symbolic 顯示網絡硬件外圍設備的符號連接名稱。
-o或--timers 顯示計時器。
-p或--programs 顯示正在使用Socket的程序識別碼和程序名稱。
-r或--route 顯示Routing Table。
-s或--statistice 顯示網絡工作信息統計表。
-t或--tcp 顯示TCP傳輸協議的連線狀況。
-u或--udp 顯示UDP傳輸協議的連線狀況。
-v或--verbose 顯示指令執行過程。
-V或--version 顯示版本信息。
-w或--raw 顯示RAW傳輸協議的連線狀況。
-x或--unix 此參數的效果和指定"-A unix"參數相同。
--ip或--inet 此參數的效果和指定"-A inet"參數相同。
實例
顯示詳細的網絡狀況

# netstat -a
顯示當前戶籍UDP連接狀況

# netstat -nu

顯示UDP端口號的使用情況

# netstat -apu
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address        Foreign Address       State    PID/Program name 
udp    0   0 *:32768           *:*                   -         
udp    0   0 *:nfs            *:*                   -         
udp    0   0 *:641            *:*                   3006/rpc.statd  
udp    0   0 192.168.0.3:netbios-ns   *:*                   3537/nmbd     
udp    0   0 *:netbios-ns        *:*                   3537/nmbd     
udp    0   0 192.168.0.3:netbios-dgm   *:*                   3537/nmbd     
udp    0   0 *:netbios-dgm        *:*                   3537/nmbd     
udp    0   0 *:tftp           *:*                   3346/xinetd    
udp    0   0 *:999            *:*                   3366/rpc.rquotad 
udp    0   0 *:sunrpc          *:*                   2986/portmap   
udp    0   0 *:ipp            *:*                   6938/cupsd    
udp    0   0 *:1022           *:*                   3392/rpc.mountd  
udp    0   0 *:638            *:*                   3006/rpc.statd

顯示網卡列表

# netstat -i
Kernel Interface table
Iface    MTU Met  RX-OK RX-ERR RX-DRP RX-OVR  TX-OK TX-ERR TX-DRP TX-OVR Flg
eth0    1500  0  181864   0   0   0  141278   0   0   0 BMRU
lo    16436  0   3362   0   0   0   3362   0   0   0 LRU

顯示組播組的關系

# netstat -g
IPv6/IPv4 Group Memberships
Interface    RefCnt Group
--------------- ------ ---------------------
lo       1   ALL-SYSTEMS.MCAST.NET
eth0      1   ALL-SYSTEMS.MCAST.NET
lo       1   ff02::1
eth0      1   ff02::1:ff0a:b0c
eth0      1   ff02::1

顯示網絡統計信息

# netstat -s
Ip:
  184695 total packets received
  0 forwarded
  0 incoming packets discarded
  184687 incoming packets delivered
  143917 requests sent out
  32 outgoing packets dropped
  30 dropped because of missing route
Icmp:
  676 ICMP messages received
  5 input ICMP message failed.
  ICMP input histogram:
    destination unreachable: 44
    echo requests: 287
    echo replies: 345
  304 ICMP messages sent
  0 ICMP messages failed
  ICMP output histogram:
    destination unreachable: 17
    echo replies: 287
Tcp:
  473 active connections openings
  28 passive connection openings
  4 failed connection attempts
  11 connection resets received
  1 connections established
  178253 segments received
  137936 segments send out
  29 segments retransmited
  0 bad segments received.
  336 resets sent
Udp:
  5714 packets received
  8 packets to unknown port received.
  0 packet receive errors
  5419 packets sent
TcpExt:
  1 resets received for embryonic SYN_RECV sockets
  ArpFilter: 0
  12 TCP sockets finished time wait in fast timer
  572 delayed acks sent
  3 delayed acks further delayed because of locked socket
  13766 packets directly queued to recvmsg prequeue.
  1101482 packets directly received from backlog
  19599861 packets directly received from prequeue
  46860 packets header predicted
  14541 packets header predicted and directly queued to user
  TCPPureAcks: 12259
  TCPHPAcks: 9119
  TCPRenoRecovery: 0
  TCPSackRecovery: 0
  TCPSACKReneging: 0
  TCPFACKReorder: 0
  TCPSACKReorder: 0
  TCPRenoReorder: 0
  TCPTSReorder: 0
  TCPFullUndo: 0
  TCPPartialUndo: 0
  TCPDSACKUndo: 0
  TCPLossUndo: 0
  TCPLoss: 0
  TCPLostRetransmit: 0
  TCPRenoFailures: 0
  TCPSackFailures: 0
  TCPLossFailures: 0
  TCPFastRetrans: 0
  TCPForwardRetrans: 0
  TCPSlowStartRetrans: 0
  TCPTimeouts: 29
  TCPRenoRecoveryFail: 0
  TCPSackRecoveryFail: 0
  TCPSchedulerFailed: 0
  TCPRcvCollapsed: 0
  TCPDSACKOldSent: 0
  TCPDSACKOfoSent: 0
  TCPDSACKRecv: 0
  TCPDSACKOfoRecv: 0
  TCPAbortOnSyn: 0
  TCPAbortOnData: 1
  TCPAbortOnClose: 0
  TCPAbortOnMemory: 0
  TCPAbortOnTimeout: 3
  TCPAbortOnLinger: 0
  TCPAbortFailed: 3
  TCPMemoryPressures: 0

顯示監聽的套接口

# netstat -l
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address        Foreign Address       State  
tcp    0   0 *:32769           *:*             LISTEN  
tcp    0   0 *:nfs            *:*             LISTEN  
tcp    0   0 *:644            *:*             LISTEN  
tcp    0   0 *:1002           *:*             LISTEN  
tcp    0   0 *:netbios-ssn        *:*             LISTEN  
tcp    0   0 *:sunrpc          *:*             LISTEN  
tcp    0   0 vm-dev:ipp         *:*             LISTEN  
tcp    0   0 *:telnet          *:*             LISTEN  
tcp    0   0 *:601            *:*             LISTEN  
tcp    0   0 *:microsoft-ds       *:*             LISTEN  
tcp    0   0 *:http           *:*             LISTEN  
tcp    0   0 *:ssh            *:*             LISTEN  
tcp    0   0 *:https           *:*             LISTEN  
udp    0   0 *:32768           *:*                  
udp    0   0 *:nfs            *:*                  
udp    0   0 *:641            *:*                  
udp    0   0 192.168.0.3:netbios-ns   *:*                  
udp    0   0 *:netbios-ns        *:*                  
udp    0   0 192.168.0.3:netbios-dgm   *:*                  
udp    0   0 *:netbios-dgm        *:*                  
udp    0   0 *:tftp           *:*                  
udp    0   0 *:999            *:*                  
udp    0   0 *:sunrpc          *:*                  
udp    0   0 *:ipp            *:*                  
udp    0   0 *:1022           *:*                  
udp    0   0 *:638            *:*                  
Active UNIX domain sockets (only servers)
Proto RefCnt Flags    Type    State     I-Node Path
unix 2   [ ACC ]   STREAM   LISTENING   10621 @/tmp/fam-root-
unix 2   [ ACC ]   STREAM   LISTENING   7096  /var/run/acpid.socket
unix 2   [ ACC ]   STREAM   LISTENING   9792  /tmp/.gdm_socket
unix 2   [ ACC ]   STREAM   LISTENING   9927  /tmp/.X11-unix/X0
unix 2   [ ACC ]   STREAM   LISTENING   10489 /tmp/ssh-lbUnUf4552/agent.4552
unix 2   [ ACC ]   STREAM   LISTENING   10558 /tmp/ksocket-root/kdeinit__0
unix 2   [ ACC ]   STREAM   LISTENING   10560 /tmp/ksocket-root/kdeinit-:0
unix 2   [ ACC ]   STREAM   LISTENING   10570 /tmp/.ICE-unix/dcop4664-1270815442
unix 2   [ ACC ]   STREAM   LISTENING   10843 /tmp/.ICE-unix/4735
unix 2   [ ACC ]   STREAM   LISTENING   10591 /tmp/ksocket-root/klauncherah3arc.slave-socket
unix 2   [ ACC ]   STREAM   LISTENING   7763  /var/run/iiim/.iiimp-unix/9010
unix 2   [ ACC ]   STREAM   LISTENING   11047 /tmp/orbit-root/linc-1291-0-1e92c8082411
unix 2   [ ACC ]   STREAM   LISTENING   11053 /tmp/orbit-root/linc-128e-0-dc070659cbb3
unix 2   [ ACC ]   STREAM   LISTENING   8020  /var/run/dbus/system_bus_socket
unix 2   [ ACC ]   STREAM   LISTENING   58927 /tmp/mcop-root/vm-dev-2c28-4beba75f
unix 2   [ ACC ]   STREAM   LISTENING   7860  /tmp/.font-unix/fs7100
unix 2   [ ACC ]   STREAM   LISTENING   7658  /dev/gpmctl
unix 2   [ ACC ]   STREAM   LISTENING   10498 @/tmp/dbus-s2MLJGO5Ci

分享一個小技巧:

netstat -an | awk ‘/^tcp/ {++sta[$NF]} END {for(key in sta) print key,"\t",sta[key]}‘

[root@aiker02 ~]#  netstat -an | awk ‘/^tcp/ {++sta[$NF]} END {for(key in sta) print key,"\t",sta[key]}‘
LISTEN   10
ESTABLISHED      11
TIME_WAIT        6

ss

ss命令用來顯示處於活動狀態的套接字信息。ss命令可以用來獲取socket統計信息,它可以顯示和netstat類似的內容。但ss的優勢在於它能夠顯示更多更詳細的有關TCP和連接狀態的信息,而且比netstat更快速更高效。

當服務器的socket連接數量變得非常大時,無論是使用netstat命令還是直接cat /proc/net/tcp,執行速度都會很慢。可能你不會有切身的感受,但請相信我,當服務器維持的連接達到上萬個的時候,使用netstat等於浪費 生命,而用ss才是節省時間。

天下武功唯快不破。ss快的秘訣在於,它利用到了TCP協議棧中tcp_diag。tcp_diag是一個用於分析統計的模塊,可以獲得Linux 內核中第一手的信息,這就確保了ss的快捷高效。當然,如果你的系統中沒有tcp_diag,ss也可以正常運行,只是效率會變得稍慢。
語法
ss(選項)
選項
-h:顯示幫助信息;
-V:顯示指令版本信息;
-n:不解析服務名稱,以數字方式顯示;
-a:顯示所有的套接字;
-l:顯示處於監聽狀態的套接字;
-o:顯示計時器信息;
-m:顯示套接字的內存使用情況;
-p:顯示使用套接字的進程信息;
-i:顯示內部的TCP信息;
-4:只顯示ipv4的套接字;
-6:只顯示ipv6的套接字;
-t:只顯示tcp套接字;
-u:只顯示udp套接字;
-d:只顯示DCCP套接字;
-w:僅顯示RAW套接字;
-x:僅顯示UNIX域套接字。
實例
顯示ICP連接

[root@localhost ~]# ss -t -a
State       Recv-Q Send-Q                            Local Address:Port                                Peer Address:Port   
LISTEN      0      0                                             *:3306                                           *:*       
LISTEN      0      0                                             *:http                                           *:*       
LISTEN      0      0                                             *:ssh                                            *:*       
LISTEN      0      0                                     127.0.0.1:smtp                                           *:*       
ESTAB       0      0                                112.124.15.130:42071                              42.156.166.25:http    
ESTAB       0      0                                112.124.15.130:ssh                              121.229.196.235:33398 

顯示 Sockets 摘要

[root@localhost ~]# ss -s
Total: 172 (kernel 189)
TCP:   10 (estab 2, closed 4, orphaned 0, synrecv 0, timewait 0/0), ports 5

Transport Total     ip        IPv6
*         189       -         -        
RAW       0         0         0        
UDP       5         5         0        
TCP       6         6         0        
INET      11        11        0        
FRAG      0         0         0   

列出當前的established, closed, orphaned and waiting TCP sockets

列出所有打開的網絡連接端口

root@localhost ~]# ss -l
Recv-Q Send-Q                                 Local Address:Port                                     Peer Address:Port   
0      0                                                  *:3306                                                *:*       
0      0                                                  *:http                                                *:*       
0      0                                                  *:ssh                                                 *:*       
0      0                                          127.0.0.1:smtp                                                *:* 

查看進程使用的socket

[root@localhost ~]# ss -pl
Recv-Q Send-Q                                          Local Address:Port                                              Peer Address:Port   
0      0                                                           *:3306                                                         *:*        users:(("mysqld",1718,10))
0      0                                                           *:http                                                         *:*        users:(("nginx",13312,5),("nginx",13333,5))
0      0                                                           *:ssh                                                          *:*        users:(("sshd",1379,3))
0      0                                                   127.0.0.1:smtp                                                         *:*        us

找出打開套接字/端口應用程序

[root@localhost ~]# ss -pl | grep 3306
0      0                            *:3306                          *:*        users:(("mysqld",1718,10))

顯示所有UDP Sockets

[root@localhost ~]# ss -u -a
State       Recv-Q Send-Q                                     Local Address:Port                                         Peer Address:Port   
UNCONN      0      0                                                      *:syslog                                                  *:*       
UNCONN      0      0                                         112.124.15.130:ntp                                                     *:*       
UNCONN      0      0                                            10.160.7.81:ntp                                                     *:*       
UNCONN      0      0                                              127.0.0.1:ntp                                                     *:*       
UNCONN      0      0                                                      *:ntp                                                     *:*

10.10 linux下抓包

Linux tcpdump命令用於傾倒網絡傳輸數據。

執行tcpdump指令可列出經過指定網絡界面的數據包文件頭,在Linux操作系統中,你必須是系統管理員

tcpdump [-adeflnNOpqStvx][-c&lt;數據包數目&gt;][-dd][-ddd][-F&lt;表達文件&gt;][-i&lt;網絡界面&gt;][-r&lt;數據包文件&gt;][-s&lt;數據包大小&gt;][-tt][-T&lt;數據包類型&gt;][-vv][-w&lt;數據包文件&gt;][輸出數據欄位]
參數說明:

-a 嘗試將網絡和廣播地址轉換成名稱。
-c<數據包數目> 收到指定的數據包數目後,就停止進行傾倒操作。
-d 把編譯過的數據包編碼轉換成可閱讀的格式,並傾倒到標準輸出。
-dd 把編譯過的數據包編碼轉換成C語言的格式,並傾倒到標準輸出。
-ddd 把編譯過的數據包編碼轉換成十進制數字的格式,並傾倒到標準輸出。
-e 在每列傾倒資料上顯示連接層級的文件頭。
-f 用數字顯示網際網絡地址。
-F<表達文件> 指定內含表達方式的文件。
-i<網絡界面> 使用指定的網絡截面送出數據包。
-l 使用標準輸出列的緩沖區。
-n 不把主機的網絡地址轉換成名字。
-N 不列出域名。
-O 不將數據包編碼最佳化。
-p 不讓網絡界面進入混雜模式。
-q 快速輸出,僅列出少數的傳輸協議信息。
-r<數據包文件> 從指定的文件讀取數據包數據。
-s<數據包大小> 設置每個數據包的大小。
-S 用絕對而非相對數值列出TCP關聯數。
-t 在每列傾倒資料上不顯示時間戳記。
-tt 在每列傾倒資料上顯示未經格式化的時間戳記。
-T<數據包類型> 強制將表達方式所指定的數據包轉譯成設置的數據包類型。
-v 詳細顯示指令執行過程。
-vv 更詳細顯示指令執行過程。
-x 用十六進制字碼列出數據包資料。
-w<數據包文件> 把數據包數據寫入指定的文件。

顯示TCP包信息

[root@aiker02 ~]# tcpdump
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
21:26:23.225041 IP aiker02.ssh > 113.87.160.89.59150: Flags [P.], seq 3156686562:31
56686802, ack 4219132626, win 296, length 240
21:26:23.225375 IP aiker02.59474 > 100.100.2.136.domain: 29645+ PTR? 89.160.87.113.
in-addr.arpa. (44)
21:26:23.235956 IP 113.87.160.89.59150 > aiker02.ssh: Flags [.], ack 240, win 257, 
length 0

用法:tcpdump -nn

 [root@aiker02 ~]# tcpdump -nn | more    ##
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
21:21:44.256458 IP 172.18.111.154.22 > 113.87.160.89.59150: Flags [P.], seq 3156677
602:3156677842, ack 4219130514, win 296, length 240
21:21:44.267239 IP 113.87.160.89.59150 > 172.18.111.154.22: Flags [.], ack 240, win
 253, length 0
21:21:44.428156 IP 172.18.111.154.58734 > 106.11.68.13.80: Flags [P.], seq 21705513
28:2170551946, ack 1131120783, win 65296, length 618
21:21:44.458544 IP 106.11.68.13.80 > 172.18.111.154.58734: Flags [.], ack 618, win 
65535, length 0

顯示指定數量包

[root@aiker02 ~]# tcpdump -c 10
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
21:28:24.763895 IP aiker02.58734 > 106.11.68.13.http: Flags [P.], seq 2170580156:2170581658, ack 1131122559, win 65296, length 1502
21:28:24.764157 IP aiker02.ssh > 113.87.160.89.59150: Flags [P.], seq 3156691858:3156692098, ack 4219133970, win 296, length 240
21:28:24.764500 IP aiker02.50738 > 100.100.2.136.domain: 45228+ PTR? 13.68.11.106.in-addr.arpa. (43)
21:28:24.764908 IP 100.100.2.136.domain > aiker02.50738: 45228 NXDomain 0/1/0 (114)
21:28:24.765720 IP aiker02.50159 > 100.100.2.138.domain: 32905+ PTR? 154.111.18.172.in-addr.arpa. (45)
21:28:24.765891 IP 100.100.2.138.domain > aiker02.50159: 32905 NXDomain* 0/1/0 (99)
21:28:24.766001 IP aiker02.43812 > 100.100.2.136.domain: 33870+ PTR? 89.160.87.113.in-addr.arpa. (44)
21:28:24.766051 IP aiker02.ssh > 113.87.160.89.59150: Flags [P.], seq 240:464, ack 1, win 296, length 224
21:28:24.766145 IP 100.100.2.136.domain > aiker02.43812: 33870 NXDomain 0/1/0 (133)
21:28:24.766308 IP aiker02.51700 > 100.100.2.138.domain: 39631+ PTR? 136.2.100.100.in-addr.arpa. (44)
10 packets captured
14 packets received by filter
0 packets dropped by kernel

精簡顯示

[root@aiker02 ~]# tcpdump -c 10 -q
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
21:29:06.198749 IP aiker02.ssh > 113.87.160.89.59150: tcp 240
21:29:06.199045 IP aiker02.47044 > 100.100.2.136.domain: UDP, length 44
21:29:06.199153 IP 100.100.2.136.domain > aiker02.47044: UDP, length 133
21:29:06.199867 IP aiker02.58807 > 100.100.2.138.domain: UDP, length 45
21:29:06.200022 IP 100.100.2.138.domain > aiker02.58807: UDP, length 99
21:29:06.200107 IP aiker02.38179 > 100.100.2.136.domain: UDP, length 44
21:29:06.200153 IP aiker02.ssh > 113.87.160.89.59150: tcp 160
21:29:06.200250 IP 100.100.2.136.domain > aiker02.38179: UDP, length 99
21:29:06.200388 IP aiker02.46348 > 100.100.2.138.domain: UDP, length 44
21:29:06.200420 IP aiker02.ssh > 113.87.160.89.59150: tcp 240
10 packets captured
11 packets received by filter
0 packets dropped by kernel

轉換克閱讀格式

# tcpdump -d   
(000) ret   #96

轉換成十進制格式


# tcpdump -ddd
1
6 0 0 96

tcpdump -nn -i eth0抓取指定網卡的包

21:31:42.083901 IP 113.87.160.89.59150 > 172.18.111.154.22: Flags [.], ack 285520, win 258, length 0
21:31:42.083915 IP 172.18.111.154.22 > 113.87.160.89.59150: Flags [P.], seq 286160:287104, ack 289, win 296, length 944
21:31:42.085025 IP 72.224.57.91.45940 > 172.18.111.154.22: Flags [P.], seq 1:42, ack 1, win 229, options [nop,nop,TS val 31494495 ecr 1323297742], length 41
21:31:42.085037 IP 172.18.111.154.22 > 72.224.57.91.45940: Flags [R], seq 3953515596, win 0, length 0

tcpdump -nn port 80抓取http包

`[root@aiker02 ~]# tcpdump -nn port 80
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes

21:33:29.456129 IP 172.18.111.154.58734 > 106.11.68.13.80: Flags [P.], seq 2170600702:2170602088, ack 1131122629, win 65296, length 1386
21:33:29.486657 IP 106.11.68.13.80 > 172.18.111.154.58734: Flags [.], ack 1340, win 65535, length 0
21:33:29.486661 IP 106.11.68.13.80 > 172.18.111.154.58734: Flags [.], ack 1386, win 65535, length 0
21:33:48.986686 IP 106.11.68.13.80 > 172.18.111.154.58734: Flags [P.], seq 1:11, ack 1386, win 65535, length 10
21:33:49.026286 IP 172.18.111.154.58734 > 106.11.68.13.80: Flags [.], ack 11, win 65296, length 0
21:33:49.084523 IP 172.18.111.154.58734 > 106.11.68.13.80: Flags [P.], seq 1386:1392, ack 11, win 65296, length 6
21:33:49.114984 IP 106.11.68.13.80 > 172.18.111.154.58734: Flags [.], ack 1392, win 65535, length 0
21:33:59.999613 IP 172.18.111.154.58734 > 106.11.68.13.80: Flags [P.], seq 1392:1866, ack 11, win 65296, length 474
21:34:00.030013 IP 106.11.68.13.80 > 172.18.111.154.58734: Flags [.], ack 1866, win 65535, length 0
21:34:00.030048 IP 172.18.111.154.58734 > 106.11.68.13.80: Flags [P.], seq 1866:2340, ack 11, win 65296, length 474`

tcpdump -nn not port 22 and host 172.16.20.221抓取非ssh,且主機為172.16.20.221的包

21:40:55.280469 IP 172.16.20.221 &gt; 172.18.111.154: ICMP echo request, id 46006, seq 1, length 64<br/>21:40:55.280492 IP 172.18.111.154 &gt; 172.16.20.221: ICMP echo reply, id 46006, seq 1, length 64<br/>21:40:56.282339 IP 172.16.20.221 &gt; 172.18.111.154: ICMP echo request, id 46006, seq 2, length 64<br/>21:40:56.282374 IP 172.18.111.154 &gt; 172.16.20.221: ICMP echo reply, id 46006, seq 2, length 64<br/>21:40:57.284003 IP 172.16.20.221 &gt; 172.18.111.154: ICMP echo request, id 46006, seq 3, length 64

tcpdump -nn -c 100 -w 1.cap抓取100個包保存到本地

[root@aiker02 ~]# tcpdump -nn -c 100 -w 1.cap
tcpdump: listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
100 packets captured
100 packets received by filter
0 packets dropped by kernel
[root@aiker02 ~]# file 1.cap  ##查看文件類型
1.cap: tcpdump capture file (little-endian) - version 2.4 (Ethernet, capture length 65535)
[root@aiker02 ~]# tcpdump -r 1.cap | more     ##查看包,不能直接眼紅cat
reading from file 1.cap, link-type EN10MB (Ethernet)
21:46:10.868588 IP aiker02.ssh > 113.87.160.89.59150: Flags [P.], seq 3157001826:3157002002, ack 4219143394, win
 307, length 176
21:46:10.879236 IP 113.87.160.89.59150 > aiker02.ssh: Flags [.], ack 176, win 256, length 0
21:46:15.627657 IP reverse.gdsz.cncnet.net.60108 > aiker02.ssh: Flags [P.], seq 2470955638:2470955706, ack 30283
95133, win 342, options [nop,nop,TS val 2985083467 ecr 1324156523], length 68
21:46:15.628119 IP reverse.gdsz.cncnet.net.60108 > aiker02.ssh: Flags [F.], seq 68, ack 1, win 342, options [nop
,nop,TS val 2985083468 ecr 1324156523], length 0
21:46:15.630427 IP aiker02.ssh > reverse.gdsz.cncnet.net.60108: Flags [F.], seq 1, ack 69, win 317, options [nop
,nop,TS val 1324171541 ecr 2985083467], length 0
21:46:15.630546 IP aiker02.ssh > 183.238.237.156.61224: Flags [P.], seq 3176389391:3176389427, ack 694266937, wi
n 303, options [nop,nop,TS val 1324171541 ecr 2465049784], length 36
21:46:15.639297 IP reverse.gdsz.cncnet.net.61844 > aiker02.ssh: Flags [S], seq 2289524520, win 29200, options [m
ss 1460,sackOK,TS val 2985083479 ecr 0,nop,wscale 7], length 0
21:46:15.639333 IP aiker02.ssh > reverse.gdsz.cncnet.net.61844: Flags [S.], seq 4061188495, ack 2289524521, win 
28960, options [mss 1460,sackOK,TS val 1324171550 ecr 2985083479,nop,wscale 7], length 0
21:46:15.639380 IP reverse.gdsz.cncnet.net.60108 > aiker02.ssh: Flags [.], ack 2, win 342, options [nop,nop,TS v
al 2985083479 ecr 1324171541], length 0
21:46:15.646671 IP reverse.gdsz.cncnet.net.61844 > aiker02.ssh: Flags [.], ack 1, win 229, options [nop,nop,TS v
al 2985083486 ecr 1324171550], length 0
yum install -y wireshark  ##安裝wireshark,默認系統不帶

tshark -n -t a -R http.request -T fields -e "frame.time" -e "ip.src" -e "http.host" -e "http.request.method" -e "http.request.uri"

[root@lnmp ~]#  tshark -n -t a -R http.request -T fields -e "frame.time" -e "ip.src" -e "http.host" -e "http.request.method" -e "http.request.uri" 
tshark: -R without -2 is deprecated. For single-pass filtering use -Y.
Running as user "root" and group "root". This could be dangerous.
Capturing on ‘eth0‘
"Apr 26, 2018 21:55:52.189474891 CST"   192.168.1.11     itks.oceanwing.com      GET     /
"Apr 26, 2018 21:55:55.206433578 CST"   192.168.1.11     itks.oceanwing.com      GET     /s/aa930d4c36adaba78d934013ca763201-CDN/zh_CN/7401/e170a3915839da2821803843481d4419ff03b453/0df730a42ee8d76c63c1eb518c3c2174/_/download/contextbatch/css/_super/batch.css
"Apr 26, 2018 21:55:55.206750875 CST"   192.168.1.11     itks.oceanwing.com      GET     /s/318e1b0631e552960ac3b9bcb853eaf8-CDN/zh_CN/7401/e170a3915839da2821803843481d4419ff03b453/59c602be4454e616e744008b87ddc849/_/download/contextbatch/css/atl.confluence.plugins.pagetree-desktop,main,viewcontent,page,atl.general,atl.comments,-_super/batch.css?analytics-enabled=true&anonymous-access-enabled=true&confluence.table.resizable=true&confluence.view.edit.transition=true&healthcheck-resources=true&highlightactions=true&hostenabled=true&is-server-instance=true&isSystemAdministrator=true&nps-acknowledged=true
"Apr 26, 2018 21:55:55.207339852 CST"   192.168.1.11     itks.oceanwing.com      GET     /s/ace86e1e15546e9ca01f8ffc7760e2b0-CDN/zh_CN/7401/e170a3915839da2821803843481d4419ff03b453/3.0.15/_/download/batch/confluence.extra.livesearch:livesearch-macro-web-resources/confluence.extra.livesearch:livesearch-macro-web-resources.css
"Apr 26, 2018 21:55:55.207486804 CST"   192.168.1.11     itks.oceanwing.com      GET     /s/7de5f41f5bef3bfca9827328bdbdc7f3-CDN/zh_CN/7401/e170a3915839da2821803843481d4419ff03b453/3.10.0/_/download/batch/confluence.macros.advanced:listlabels-resources/confluence.macros.advanced:listlabels-resources.css
"Apr 26, 2018 21:55:55.234588588 CST"   192.168.1.11     itks.oceanwing.com      GET     /s/zh_CN/7401/e170a3915839da2821803843481d4419ff03b453/8/_/styles/colors.css

擴展tcp三次握手四次揮手

一、Linux服務器上11種網絡連接狀態:
技術分享圖片
圖:TCP的狀態機

通常情況下:一個正常的TCP連接,都會有三個階段:1、TCP三次握手;2、數據傳送;3、TCP四次揮手

註:以下說明最好能結合”圖:TCP的狀態機”來理解。

SYN: (同步序列編號,Synchronize Sequence Numbers)該標誌僅在三次握手建立TCP連接時有效。表示一個新的TCP連接請求。

ACK: (確認編號,Acknowledgement Number)是對TCP請求的確認標誌,同時提示對端系統已經成功接收所有數據。

FIN: (結束標誌,FINish)用來結束一個TCP回話.但對應端口仍處於開放狀態,準備接收後續數據。

1)、LISTEN:首先服務端需要打開一個socket進行監聽,狀態為LISTEN. / The socket is listening for incoming connections. 偵聽來自遠方TCP端口的連接請求 /
2)、SYN_SENT:客戶端通過應用程序調用connect進行active open.於是客戶端tcp發送一個SYN以請求建立一個連接.之後狀態置為SYN_SENT. /The socket is actively attempting to establish a connection. 在發送連接請求後等待匹配的連接請求 /

3)、SYN_RECV:服務端應發出ACK確認客戶端的SYN,同時自己向客戶端發送一個SYN. 之後狀態置為SYN_RECV / A connection request has been received from the network. 在收到和發送一個連接請求後等待對連接請求的確認 /

4)、ESTABLISHED: 代表一個打開的連接,雙方可以進行或已經在數據交互了。/ The socket has an established connection. 代表一個打開的連接,數據可以傳送給用戶 /

5)、FIN_WAIT1:主動關閉(active close)端應用程序調用close,於是其TCP發出FIN請求主動關閉連接,之後進入FIN_WAIT1狀態./ The socket is closed, and the connection is shutting down. 等待遠程TCP的連接中斷請求,或先前的連接中斷請求的確認 /

6)、CLOSE_WAIT:被動關閉(passive close)端TCP接到FIN後,就發出ACK以回應FIN請求(它的接收也作為文件結束符傳遞給上層應用程序),並進入CLOSE_WAIT. / The remote end has shut down, waiting for the socket to close. 等待從本地用戶發來的連接中斷請求 /

7)、FIN_WAIT2:主動關閉端接到ACK後,就進入了FIN-WAIT-2 ./ Connection is closed, and the socket is waiting for a shutdown from the remote end. 從遠程TCP等待連接中斷請求 /

8)、LAST_ACK:被動關閉端一段時間後,接收到文件結束符的應用程序將調用CLOSE關閉連接。這導致它的TCP也發送一個 FIN,等待對方的ACK.就進入了LAST-ACK . / The remote end has shut down, and the socket is closed. Waiting for acknowledgement. 等待原來發向遠程TCP的連接中斷請求的確認 /

9)、TIME_WAIT:在主動關閉端接收到FIN後,TCP就發送ACK包,並進入TIME-WAIT狀態。/ The socket is waiting after close to handle packets still in the network.等待足夠的時間以確保遠程TCP接收到連接中斷請求的確認 /

10)、CLOSING: 比較少見./ Both sockets are shut down but we still don’t have all our data sent. 等待遠程TCP對連接中斷的確認 /

11)、CLOSED: 被動關閉端在接受到ACK包後,就進入了closed的狀態。連接結束./ The socket is not being used. 沒有任何連接狀態 /
TIME_WAIT狀態的形成只發生在主動關閉連接的一方。
主動關閉方在接收到被動關閉方的FIN請求後,發送成功給對方一個ACK後,將自己的狀態由FIN_WAIT2修改為TIME_WAIT,而必須再等2倍 的MSL(Maximum Segment Lifetime,MSL是一個數據報在internetwork中能存在的時間)時間之後雙方才能把狀態 都改為CLOSED以關閉連接。目前RHEL裏保持TIME_WAIT狀態的時間為60秒。

當然上述很多TCP狀態在系統裏都有對應的解釋或設置,可見man tcp

二、關於長連接和短連接:
通俗點講:短連接就是一次TCP請求得到結果後,連接馬上結束.而長連接並不馬上斷開,而一直保持著,直到長連接TIMEOUT(具體程序都有相關參數說明).長連接可以避免不斷的進行TCP三次握手和四次揮手.
長連接(keepalive)是需要靠雙方不斷的發送探測包來維持的,keepalive期間服務端和客戶端的TCP連接狀態是ESTABLISHED.目前http 1.1版本裏默認都是keepalive(1.0版本默認是不keepalive的),ie6/7/8和firefox都默認用的是http 1.1版本了(如何查看當前瀏覽器用的是哪個版本,這裏不再贅述)。Apache,java

一個應用至於到底是該使用短連接還是長連接,應該視具體情況而定。一般的應用應該使用長連接。

1、Linux的相關keepalive參數

a、 tcp_keepalive_time – INTEGER
How often TCP sends out keepalive messages when keepalive is enabled.
Default: 2hours.
b、 tcp_keepalive_probes – INTEGER
How many keepalive probes TCP sends out, until it decides that the
connection is broken. Default value: 9.
c、 tcp_keepalive_intvl – INTEGER
How frequently the probes are send out. Multiplied by
tcp_keepalive_probes it is time to kill not responding connection,
after probes started. Default value: 75sec i.e. connection
will be aborted after ~11 minutes of retries.

2、F5負載均衡上的相關參數說明

a、Keep Alive Interval
Specifies, when enabled, how frequently the system sends data over an idle TCP connection, to determine whether the connection is still valid.
Specify: Specifies the interval at which the system sends data over an idle connection, to determine whether the connection is still valid. The default is 1800 milliseconds.
b、Time Wait
Specifies the length of time that a TCP connection remains in the TIME-WAIT state before entering the CLOSED state.
Specify: Specifies the number of milliseconds that a TCP connection can remain in the TIME-WAIT state. The default is 2000.

c、Idle Timeout
Specifies the length of time that a connection is idle (has no traffic) before the connection is eligible for deletion.
Specify: Specifies a number of seconds that the TCP connection can remain idle before the system deletes it. The default is 300 seconds.

3、Apache的相關參數說明
以下是Apache/2.0.61版本的默認參數和說明

a、KeepAlive:
default On.Whether or not to allow persistent connections (more than
one request per connection). Set to “Off” to deactivate.
b、MaxKeepAliveRequests:
default 100.The maximum number of requests to allow
during a persistent connection. Set to 0 to allow an unlimited amount.
We recommend you leave this number high, for maximum performance.
c、KeepAliveTimeout:
default 15. Number of seconds to wait for the next request from the
same client on the same connection.

tshark幾個用法:

  1. 以下的用法可以顯示訪問http請求的域名以及uri
    tshark -n -t a -R http.request -T fields -e "frame.time" -e "ip.src" -e "http.host" -e "http.request.method" -e "http.request.uri"
    [root@lnmp ~]# tshark -n -t a -R http.request -T fields -e "frame.time" -e "ip.src" -e "http.host" -e "http.request.method" -e "http.request.uri"
    tshark: -R without -2 is deprecated. For single-pass filtering use -Y.
    Running as user "root" and group "root". This could be dangerous.
    Capturing on ‘eth0‘
    "Apr 26, 2018 22:02:07.766836435 CST"   192.168.1.1     itks.oceanwing.com      GET     /rest/quickreload/latest/2818059?since=1524751419942&_=1524751755298
    "Apr 26, 2018 22:02:11.103628087 CST"   192.168.1.1     itks.oceanwing.com      GET     /pages/viewpage.action?pageId=2818201&src=contextnavpagetreemode
    "Apr 26, 2018 22:02:11.124342006 CST"   172.16.20.247   jira.oceanwing.com      GET     /rest/remote-link-aggregation/1/aggregation?globalId=appId%3D08f53665-0768-33e9-8e32-2d8bd069921d%26pageId%3D2818201&globalId=appId%3D7c6a47f5-391d-3b1e-903b-f4868e9d8f3d%26pageId%3D2818201
    "Apr 26, 2018 22:02:11.124588387 CST"   172.16.22.246   jira.oceanwing.com:80   GET     /rest/remote-link-aggregation/1/aggregation?globalId=appId%3D08f53665-0768-33e9-8e32-2d8bd069921d%26pageId%3D2818201&globalId=appId%3D7c6a47f5-391d-3b1e-903b-f4868e9d8f3d%26pageId%3D2818201
    "Apr 26, 2018 22:02:11.900276596 CST"   192.168.1.1     itks.oceanwing.com      POST    /rest/webResources/1.0/resources
    "Apr 26, 2018 22:02:12.048704592 CST"   192.168.1.1     itks.oceanwing.com      GET     /plugins/pagetree/naturalchildren.action?decorator=none&excerpt=false&sort=position&reverse=false&disableLinks=false&expandCurrent=true&hasRoot=true&pageId=2818059&treeId=0&startDepth=0&mobile=false&ances
  2. 以下可以抓取mysql的查詢
    tshark -n -i eth1 -R ‘mysql.query‘ -T fields -e "ip.src" -e "mysql.query"
    另外一種方法:
    tshark -i eth1 port 3307 -d tcp.port==3307,mysql -z "proto,colinfo,mysql.query,mysql.query"

  3. 以下可以抓取指定類型的MySQL查詢
    tshark -n -i eth1 -R ‘mysql matches "SELECT|INSERT|DELETE|UPDATE"‘ -T fields -e "ip.src" -e "mysql.query"

  4. 統計http的狀態
    tshark -n -q -z http,stat, -z http,tree
    
    [root@lnmp ~]# tshark -n -q -z http,stat, -z http,tree
    Running as user "root" and group "root". This could be dangerous.
    Capturing on ‘eth0‘
    ^C413 packets dropped
    3328 packets captured

===================================================================
HTTP/Packet Counter value rate percent

Total HTTP Packets 459 0.038620
HTTP Request Packets 108 0.009087 23.53%
GET 80 0.006731 74.07%
POST 28 0.002356 25.93%
HTTP Response Packets 173 0.014556 37.69%
???: broken 0 0.000000 0.00%
1xx: Informational 0 0.000000 0.00%
2xx: Success 163 0.013715 94.22%
200 OK 163 0.013715 100.00%
3xx: Redirection 10 0.000841 5.78%
304 Not Modified 10 0.000841 100.00%
4xx: Client Error 0 0.000000 0.00%
5xx: Server Error 0 0.000000 0.00%
Other HTTP Packets 178 0.014977 38.78%

===================================================================

===================================================================
HTTP Statistics

  • HTTP Status Codes in reply packets
    HTTP 200 OK
    HTTP 304 Not Modified
  • List of HTTP Request methods
    GET 80
    POST 28

    
    這個命令,直到你ctrl + c 才會顯示出結果
  1. tshark 增加時間標簽
    tshark -t ad
    [root@lnmp ~]# tshark  -t  ad
    Running as user "root" and group "root". This could be dangerous.
    Capturing on ‘eth0‘
    1 2018-04-26 22:05:18.275490126 172.16.20.230 -> 255.255.255.255 UDP 334 Source port: 57926  Destination port: 7989
    2 2018-04-26 22:05:18.801628914 172.16.22.246 -> 172.16.58.8  SSH 262 Encrypted response packet len=208
    3 2018-04-26 22:05:18.812573457 9c:06:1b:fe:3b:29 -> Broadcast    ARP 60 Who has 172.16.20.223?  Tell 172.16.23.254

    tshark -t a

    [root@lnmp ~]# tshark  -t  a
    Running as user "root" and group "root". This could be dangerous.
    Capturing on ‘eth0‘
    1 22:06:32.816149710 9c:06:1b:fe:3b:29 -> Broadcast    ARP 60 Who has 172.16.20.231?  Tell 172.16.23.254
    2 22:06:32.816241400 9c:06:1b:fe:3b:29 -> Broadcast    ARP 60 Who has 172.16.20.238?  Tell 172.16.23.254
    3 22:06:32.816367198 9c:06:1b:fe:3b:29 -> Broadcast    ARP 60 Who has 172.16.20.239?  Tell 172.16.23.254

參考
https://ask.wireshark.org/questions/16964/analyzing-http-protocol-using-tshark

七周二次課