关于网络:常用网络命令总结

9次阅读

共计 4728 个字符,预计需要花费 12 分钟才能阅读完成。

原创:打码日记(微信公众号 ID:codelogs),欢送分享,转载请保留出处。

网络连通性检测

当利用呈现网络异样时,首先须要确认的就是网络的连通性是否失常,上面一组命令可疾速检测网络的连通性,如下:
检测 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:开始到服务端数据接管实现的工夫

查看 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/…

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

总结

把握罕用的网络命令,还是十分有必要的,毕竟网络是如此简单,必须要有货色可能窥探一些外部运行信息。

往期内容

还在胡乱设置连贯闲暇工夫?
我都调通了,为啥你不行?
应用 socat 批量操作多台机器

正文完
 0