繼linux命令以後,我又給大家整理了網絡命令概括,快給爺來收藏

前言

上次發了linux命令總結以後,不少朋友說想看網絡命令概括總結,今天他來了,別廢話,給我收藏起來。
歡迎各位進羣973961276一塊兒聊聊技術吹吹牛,每週都會有幾回抽獎送專業書籍的活動,獎品雖不甚值錢,但也算個彩頭不是python

網絡連通性檢測

當應用出現網絡異常時,首先須要確認的就是網絡的連通性是否正常,下面一組命令可快速檢測網絡的連通性,以下:
檢測DNSmysql

dig www.baidu.combash
nslookup www.baidu.combash
host www.baidu.com

檢測主機是否可達linux

ping www.baidu.com

檢測port是否可達c++

#檢查tcp端口
telnet www.baidu.com 80
#檢查udp端口
nc -uvz ip port

檢測SSL
SSL認證也常常致使程序沒法鏈接,主要出如今SSL握手過程當中。git

openssl s_client -connect www.baidu.com:443 -prexit

一鍵檢測
多數狀況下,可使用curl一鍵檢測全部過程,若是有問題,再使用上面的命令逐個排查。github

curl -v http://www.baidu.com:80/

時間消耗分佈
使用curl可檢測出http協議接口各階段花費的時間。sql

$ curl -o /dev/null -s -w " time_namelookup:%{time_namelookup}sn time_connect:%{time_connect}sn time_starttransfer:%{time_starttransfer}sn time_total:%{time_total}sn speed_download:%{speed_download}n http_code:%{http_code}" "http://www.baidu.com"
 time_namelookup:0.016542s
 time_connect:0.038686s
 time_starttransfer:0.063550s
 time_total:0.063593s
 speed_download:37793.000
 http_code:200

time_namelookup:開始到DNS查詢完成的時間
time_connect:開始到TCP三次握手完成的時間
time_starttransfer:開始到收到服務端發來首字節數據的時間
time_total:開始到服務端數據接收完成的時間ubuntu

零基礎和大三大四的朋友看這裏>>c/c++ 企業級項目實戰
已經工做了想繼續自我提高跳槽漲薪的工程師看這裏>>c/c++ linux服務器高級segmentfault

檢查socket鏈接

因爲網絡通訊都須要靠socket,因此檢查一下socket鏈接以及它的分佈狀況也是很是有必要的。
檢查端口是否監聽
服務端程序必定會監聽至少一個端口,檢查監聽socket是否存在,也是判斷服務進程是否還存在的一種方法。bash

netstat -nltp|grep 8080
lsof  -nP -i -sTCP:LISTEN|grep 8080

查看socket狀態分佈

$ ss -s
$ netstat -nat | awk '/tcp/{print $6}'|sort|uniq -c
      9 CLOSE_WAIT
    102 ESTABLISHED
     55 LISTEN
     70 TIME_WAIT

需格外關注TIME_WAIT與CLOSE_WAIT這兩種狀態的數量,若是TIME_WAIT過多,可考慮優化內核網絡參數或使用鏈接池,若是CLOSE_WAIT過多,就須要檢查程序代碼中哪裏出現了鏈接泄露,致使未關閉鏈接了。
誰連我最多

netstat -ant | awk '/tcp/{rl=split($5,r,":");printf "%16st%sn",$4,r[rl-1]}' | sort | uniq -c | sort -nrk1 | head -n10

我連誰最多

netstat -ant | awk '/tcp/{ll=split($4,l,":");printf "%11st%sn",l[ll-1],$5}' | sort | uniq -c | sort -nrk1 | head -n10

網絡使用率檢測

查看各鏈接網速

iftop -B -nNP

查看各進程網速

nethogs

查看網卡網速

sar -n DEV 1
ifstat

查看網卡是否丟包

# ifconfig命令,觀察overrun/error/drop這幾項
ifconfig
# 一樣,觀察相似overflow、error、drop這些項
ethtool -S eth0

TCP層丟包與重傳
有時,網卡層未出現丟包,但網絡中間鏈路有可能出現丟包,這會致使tcp層重傳,另外,若是tcp層的內核參數設置不合理,也可能致使丟包,好比backlog設置太小,服務器端網絡io處理不過來。

$ sar -n TCP,ETCP 1
$ sudo watch -d -n1  'netstat -s|grep -iE "listen|pruned|collapsed|reset|retransmit"'
    2879 connection resets received
    378542 segments retransmitted
    3357875 resets sent
    52 resets received for embryonic SYN_RECV sockets
    5 times the listen queue of a socket overflowed
    5 SYNs to LISTEN sockets dropped
    TCPLostRetransmit: 235599
    6337 fast retransmits
    7877 retransmits in slow start
    10385 connections reset due to unexpected data
    1183 connections reset due to early user close

網絡抓包

純文本抓包

# ngrep比較適合抓包相似http這種的純文本協議
sudo ngrep -W byline port 3306
# 在沒法使用抓包命令的狀況下,也可以使用nc、socat之類的網絡工具,作一個端口轉發,同時將轉發流量打印出來
# 另外在抓包https時,也可使用socat將https流量代理爲http流量,再進行抓包
socat -v TCP4-LISTEN:9999,bind=0.0.0.0,reuseaddr TCP4:remoteIp:9999

通用抓包工具

# tcpdump抓包給wireshark分析
sudo tcpdump tcp -i eth1 -s 0 -c 10000 and port 9999 -w ./target.cap
# 抓rst包,用於網絡常常出現connection reset異常的狀況
sudo tcpdump -ni any -s0 tcp and 'tcp[13] & 4 != 0 ' -vvv
# 抓fin包,用於網絡常常斷連的狀況
sudo tcpdump -ni any -s0 tcp and 'tcp[13] & 1 != 0 ' -vvv

mysql抓包

$ sudo tshark -i eth0 -n -f 'tcp port 3306' -Y 'mysql' -T fields  -e frame.number -e frame.time_epoch -e frame.time_delta_displayed  -e ip.src -e tcp.srcport -e tcp.dstport -e ip.dst -e tcp.stream -e tcp.len -e tcp.nxtseq -e tcp.time_delta -e tcp.analysis.ack_rtt -e mysql.query
Running as user "root" and group "root". This could be dangerous.
Capturing on 'ens33'
4       1605412440.114466205    0.000000000     10.224.72.135   3306    59016   10.221.38.217   0       88      89      0.001027726
6       1605412440.160709874    0.046243669     10.221.38.217   59016   3306    10.224.72.135   0       185     186     0.000020998
8       1605412440.160929986    0.000220112     10.224.72.135   3306    59016   10.221.38.217   0       48      137     0.000211802
9       1605412440.213810997    0.052881011     10.221.38.217   59016   3306    10.224.72.135   0       24      210     0.052881011     0.052881011
11      1605412440.214178087    0.000367090     10.224.72.135   3306    59016   10.221.38.217   0       22      159     0.000341184
12      1605412440.258391363    0.044213276     10.221.38.217   59016   3306    10.224.72.135   0       37      247     0.044213276     0.044213276     select @@version_comment limit 1
14      1605412440.258812895    0.000421532     10.224.72.135   3306    59016   10.221.38.217   0       83      242     0.000395748
15      1605412440.303693157    0.044880262     10.221.38.217   59016   3306    10.224.72.135   0       13      260     0.044880262     0.044880262     select 1
16      1605412440.303955060    0.000261903     10.224.72.135   3306    59016   10.221.38.217   0       49      291     0.000261903     0.000261903
17      1605412440.351387241    0.047432181     10.221.38.217   59016   3306    10.224.72.135   0       5       265     0.047432181     0.047432181

grpc抓包
對於grpc抓包,能夠先使用tcpdump抓下來,而後到wireshark中查看,也可以使用我從github找到的這個項目https://github.com/rmedvedev/...

sudo grpcdump -i eth0 -p 9999 -proto-path ~/protos -proto-files order/v1/log_service.proto

傳輸文件

使用scp

#上傳文件到遠程機器
scp test.txt root@remoteIp:/home/
#從遠程機器下載文件
scp root@remoteIp:/home/test.txt .

使用ncat
ncat其實就是常說的nc,但因爲netcat也叫nc且用法稍有不一樣(ubuntu上的nc就是netcat),避免混淆,這裏直接使用ncat

# 接收文件端
ncat -l 9999 > test.txt
# 發送文件端
ncat remoteIp 9999 < test.txt

使用python http server
python的http server常常用於分享本機文件給其它人,很是方便。

python -m SimpleHTTPServer 8000
wget http://remoteIp:8000/test.txt

使用使用python ftp server
使用python能夠快速搭建一個ftp server,這樣就便可以上傳,又能夠下載了。

sudo pip3 install pyftpdlib
python3 -m pyftpdlib -p 2121 -w
#上傳到ftp
curl ftp://remoteIp:2121/files/ -T file.txt
#從ftp下載
curl -O ftp://remoteIp:2121/files/file.txt

總結

掌握經常使用的網絡命令,仍是很是有必要的,畢竟網絡是如此複雜,必需要有東西可以窺探一些內部運行信息。

相關文章
相關標籤/搜索