繼linux命令之後,我又給你們整理了網絡命令歸納,快給我來收藏

{"type":"doc","content":[{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"前言","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"上次發了","attrs":{}},{"type":"link","attrs":{"href":"https://xie.infoq.cn/article/bc1ac76b89662add12392392e","title":""},"content":[{"type":"text","text":"linux命令總結","attrs":{}}]},{"type":"text","text":"之後,很多朋友說想看網絡命令歸納總結,今天他來了,別廢話,給我收藏起來。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"推薦一下自己的linuxC/C++交流羣:973961276!整理了一些個人覺得比較好的學習書籍、視頻資料以及大廠面經視頻共享在羣文件裏面,有需要的小夥伴可以自行添加哦!~","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"網絡連通性檢測","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"當應用出現網絡異常時,首先需要確認的就是網絡的連通性是否正常,下面一組命令可快速檢測網絡的連通性,如下:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"檢測DNS","attrs":{}}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"dig www.baidu.combash\nnslookup www.baidu.combash\nhost www.baidu.com","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"檢測主機是否可達","attrs":{}}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"ping www.baidu.com","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"檢測port是否可達","attrs":{}}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"#檢查tcp端口\ntelnet www.baidu.com 80\n#檢查udp端口\nnc -uvz ip port","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"檢測SSL","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"SSL認證也經常導致程序無法連接,主要出現在SSL握手過程中。","attrs":{}}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"openssl s_client -connect www.baidu.com:443 -prexit","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"一鍵檢測","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"多數情況下,可以使用curl一鍵檢測所有過程,如果有問題,再使用上面的命令逐個排查。","attrs":{}}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"curl -v http://www.baidu.com:80/","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"時間消耗分佈","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"使用curl可檢測出http協議接口各階段花費的時間。","attrs":{}}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"$ curl -o /dev/null -s -w \" time_namelookup:%{time_namelookup}s\\n time_connect:%{time_connect}s\\n time_starttransfer:%{time_starttransfer}s\\n time_total:%{time_total}s\\n speed_download:%{speed_download}\\n http_code:%{http_code}\" \"http://www.baidu.com\"\n time_namelookup:0.016542s\n time_connect:0.038686s\n time_starttransfer:0.063550s\n time_total:0.063593s\n speed_download:37793.000\n http_code:200","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"time_namelookup:開始到DNS查詢完成的時間","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"time_connect:開始到TCP三次握手完成的時間","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"time_starttransfer:開始到收到服務端發來首字節數據的時間","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"time_total:開始到服務端數據接收完成的時間","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"零基礎和大三大四的朋友看這裏>>","attrs":{}},{"type":"link","attrs":{"href":"https://ke.qq.com/course/444655?flowToken=1029781","title":null},"content":[{"type":"text","text":"c/c++ 企業級項目實戰","attrs":{}}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"已經工作了想繼續自我提升跳槽漲薪的工程師看這裏>>","attrs":{}},{"type":"link","attrs":{"href":"http://xn--https-vw1h119eb0elyzjoa//ke.qq.com/course/417774?flowToken=1026211","title":null},"content":[{"type":"text","text":"c/c++ linux服務器高級","attrs":{}}]}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"檢查socket連接","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"由於網絡通信都需要靠socket,所以檢查一下socket連接以及它的分佈情況也是非常有必要的。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"檢查端口是否監聽","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"服務端程序一定會監聽至少一個端口,檢查監聽socket是否存在,也是判斷服務進程是否還存在的一種方法。","attrs":{}}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"netstat -nltp|grep 8080\nlsof -nP -i -sTCP:LISTEN|grep 8080","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"查看socket狀態分佈","attrs":{}}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"$ ss -s\n$ netstat -nat | awk '/tcp/{print $6}'|sort|uniq -c\n 9 CLOSE_WAIT\n 102 ESTABLISHED\n 55 LISTEN\n 70 TIME_WAIT","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"需格外關注TIME_WAIT與CLOSE_WAIT這兩種狀態的數量,如果TIME_WAIT過多,可考慮優化內核網絡參數或使用連接池,如果CLOSE_WAIT過多,就需要檢查程序代碼中哪裏出現了連接泄露,導致未關閉連接了。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"誰連我最多","attrs":{}}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"netstat -ant | awk '/tcp/{rl=split($5,r,\":\");printf \"%16s\\t%s\\n\",$4,r[rl-1]}' | sort | uniq -c | sort -nrk1 | head -n10","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"我連誰最多","attrs":{}}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"netstat -ant | awk '/tcp/{ll=split($4,l,\":\");printf \"%11s\\t%s\\n\",l[ll-1],$5}' | sort | uniq -c | sort -nrk1 | head -n10","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"網絡使用率檢測","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"查看各連接網速","attrs":{}}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"iftop -B -nNP\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"查看各進程網速","attrs":{}}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"nethogs","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"查看網卡網速","attrs":{}}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"sar -n DEV 1\nifstat ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"查看網卡是否丟包","attrs":{}}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"# ifconfig命令,觀察overrun/error/drop這幾項\nifconfig\n# 同樣,觀察類似overflow、error、drop這些項\nethtool -S eth0","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"TCP層丟包與重傳","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"有時,網卡層未出現丟包,但網絡中間鏈路有可能出現丟包,這會導致tcp層重傳,另外,如果tcp層的內核參數設置不合理,也可能導致丟包,比如backlog設置過小,服務器端網絡io處理不過來。","attrs":{}}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"$ sar -n TCP,ETCP 1\n$ sudo watch -d -n1 'netstat -s|grep -iE \"listen|pruned|collapsed|reset|retransmit\"'\n 2879 connection resets received\n 378542 segments retransmitted\n 3357875 resets sent\n 52 resets received for embryonic SYN_RECV sockets\n 5 times the listen queue of a socket overflowed\n 5 SYNs to LISTEN sockets dropped\n TCPLostRetransmit: 235599\n 6337 fast retransmits\n 7877 retransmits in slow start\n 10385 connections reset due to unexpected data\n 1183 connections reset due to early user close","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"網絡抓包","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"純文本抓包","attrs":{}}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"# ngrep比較適合抓包類似http這種的純文本協議\nsudo ngrep -W byline port 3306\n# 在無法使用抓包命令的情況下,也可使用nc、socat之類的網絡工具,做一個端口轉發,同時將轉發流量打印出來\n# 另外在抓包https時,也可以使用socat將https流量代理爲http流量,再進行抓包\nsocat -v TCP4-LISTEN:9999,bind=0.0.0.0,reuseaddr TCP4:remoteIp:9999","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"通用抓包工具","attrs":{}}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"# tcpdump抓包給wireshark分析\nsudo tcpdump tcp -i eth1 -s 0 -c 10000 and port 9999 -w ./target.cap\n# 抓rst包,用於網絡經常出現connection reset異常的情況\nsudo tcpdump -ni any -s0 tcp and 'tcp[13] & 4 != 0 ' -vvv\n# 抓fin包,用於網絡經常斷連的情況\nsudo tcpdump -ni any -s0 tcp and 'tcp[13] & 1 != 0 ' -vvv","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"mysql抓包","attrs":{}}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"$ 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\nRunning as user \"root\" and group \"root\". This could be dangerous.\nCapturing on 'ens33'\n4 1605412440.114466205 0.000000000 10.224.72.135 3306 59016 10.221.38.217 0 88 89 0.001027726\n6 1605412440.160709874 0.046243669 10.221.38.217 59016 3306 10.224.72.135 0 185 186 0.000020998\n8 1605412440.160929986 0.000220112 10.224.72.135 3306 59016 10.221.38.217 0 48 137 0.000211802\n9 1605412440.213810997 0.052881011 10.221.38.217 59016 3306 10.224.72.135 0 24 210 0.052881011 0.052881011\n11 1605412440.214178087 0.000367090 10.224.72.135 3306 59016 10.221.38.217 0 22 159 0.000341184\n12 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\n14 1605412440.258812895 0.000421532 10.224.72.135 3306 59016 10.221.38.217 0 83 242 0.000395748\n15 1605412440.303693157 0.044880262 10.221.38.217 59016 3306 10.224.72.135 0 13 260 0.044880262 0.044880262 select 1\n16 1605412440.303955060 0.000261903 10.224.72.135 3306 59016 10.221.38.217 0 49 291 0.000261903 0.000261903\n17 1605412440.351387241 0.047432181 10.221.38.217 59016 3306 10.224.72.135 0 5 265 0.047432181 0.047432181","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"grpc抓包","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"對於grpc抓包,可以先使用tcpdump抓下來,然後到wireshark中查看,也可使用我從github找到的這個項目https://github.com/rmedvedev/grpcdump","attrs":{}}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"sudo grpcdump -i eth0 -p 9999 -proto-path ~/protos -proto-files order/v1/log_service.proto","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"傳輸文件","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"使用scp","attrs":{}}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"#上傳文件到遠程機器\nscp test.txt root@remoteIp:/home/\n#從遠程機器下載文件\nscp root@remoteIp:/home/test.txt .","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"使用ncat","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"ncat其實就是常說的nc,但由於netcat也叫nc且用法稍有不同(ubuntu上的nc就是netcat),避免混淆,這裏直接使用ncat","attrs":{}}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"# 接收文件端\nncat -l 9999 > test.txt\n# 發送文件端\nncat remoteIp 9999 < test.txt","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"使用python http server","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"python的http server經常用於分享本機文件給其它人,非常方便。","attrs":{}}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"python -m SimpleHTTPServer 8000\nwget http://remoteIp:8000/test.txt","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"使用使用python ftp server","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"使用python可以快速搭建一個ftp server,這樣就即可以上傳,又可以下載了。","attrs":{}}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"sudo pip3 install pyftpdlib\npython3 -m pyftpdlib -p 2121 -w\n#上傳到ftp\ncurl ftp://remoteIp:2121/files/ -T file.txt\n#從ftp下載\ncurl -O ftp://remoteIp:2121/files/file.txt","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"總結","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"掌握常用的網絡命令,還是非常有必要的,畢竟網絡是如此複雜,必須要有東西能夠窺探一些內部運行信息。","attrs":{}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/eb/eb920314e43a133cb56e5636cf849f28.jpeg","alt":null,"title":"","style":[{"key":"width","value":"100%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章