11

继 linux 命令之后,我又给你们整理了网络命令归纳,快给我来收藏

 3 years ago
source link: https://xie.infoq.cn/article/0806ac01e33ac67bac5e68251
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

前言

上次发了 linux命令总结 之后,很多朋友说想看网络命令归纳总结,今天他来了,别废话,给我收藏起来。

推荐一下自己的linuxC/C++交流群:973961276!整理了一些个人觉得比较好的学习书籍、视频资料以及大厂面经视频共享在群文件里面,有需要的小伙伴可以自行添加哦!~

网络连通性检测

当应用出现网络异常时,首先需要确认的就是网络的连通性是否正常,下面一组命令可快速检测网络的连通性,如下:

检测DNS

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

检测主机是否可达

ping www.baidu.com

检测port是否可达

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

检测SSL

SSL认证也经常导致程序无法连接,主要出现在SSL握手过程中。

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

一键检测

多数情况下,可以使用curl一键检测所有过程,如果有问题,再使用上面的命令逐个排查。

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

时间消耗分布

使用curl可检测出http协议接口各阶段花费的时间。

$ 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"
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:开始到服务端数据接收完成的时间

零基础和大三大四的朋友看这里>> c/c++ 企业级项目实战

已经工作了想继续自我提升跳槽涨薪的工程师看这里>> c/c++ linux服务器高级

检查socket连接

由于网络通信都需要靠socket,所以检查一下socket连接以及它的分布情况也是非常有必要的。

检查端口是否监听

服务端程序一定会监听至少一个端口,检查监听socket是否存在,也是判断服务进程是否还存在的一种方法。

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 "%16s\t%s\n",$4,r[rl-1]}' | sort | uniq -c | sort -nrk1 | head -n10

我连谁最多

netstat -ant | awk '/tcp/{ll=split($4,l,":");printf "%11s\t%s\n",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/grpcdump

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

总结

掌握常用的网络命令,还是非常有必要的,毕竟网络是如此复杂,必须要有东西能够窥探一些内部运行信息。

ZvQZbiF.jpg!mobile


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK