feat: add netstat.md cheatsheet.

This commit is contained in:
jaywcjlove 2022-10-21 23:49:09 +08:00
parent cd1be27fae
commit 9d3a061bff
3 changed files with 217 additions and 0 deletions

View File

@ -77,6 +77,7 @@ Quick Reference
[find](./docs/find.md)<!--rehype:style=background: rgb(16 185 129/var(\-\-bg\-opacity));-->
[htop](./docs/htop.md)<!--rehype:style=background: rgb(16 185 129/var(\-\-bg\-opacity));-->
[Home Brew](./docs/homebrew.md)<!--rehype:style=background: rgb(252 185 87/var(\-\-bg\-opacity));-->
[Netstat](./docs/netstat.md)<!--rehype:style=background: rgb(16 185 129/var(\-\-bg\-opacity));-->
[Sed](./docs/sed.md)<!--rehype:style=background: rgb(16 185 129/var(\-\-bg\-opacity));-->
[SSH](./docs/ssh.md)<!--rehype:style=background: rgb(99 99 99/var(\-\-bg\-opacity));-->
[Screen](./docs/screen.md)<!--rehype:style=background: rgb(99 99 99/var(\-\-bg\-opacity));-->

209
docs/netstat.md Normal file
View File

@ -0,0 +1,209 @@
Netstat 备忘清单
===
此快速参考备忘单提供了各种使用 netstat 命令的方法
入门
-----
### 入门实例
端口 80 上的所有连接
```shell
$ netstat -anp | grep :80
```
网络统计帮助
```shell
$ netstat -h
```
### 监听
选项 | 说明
:- | :-
`netstat -ltunp` | 所有监听端口
`netstat -ltn` | 监听 TCP 端口
`netstat -lun` | 监听 UDP 端口
`netstat -lx` | 监听 Unix 端口
`netstat -lt` | 仅列出侦听 TCP 端口
`netstat -lu` | 仅列出侦听 UDP 端口
`netstat -l` | 列出所有监听条件
### 连接
<!--rehype:wrap-class=row-span-2-->
选项 | 说明
:- | :-
`netstat -a` | 所有连接
`netstat -at` | 所有 TCP 连接
`netstat -au` | 所有 UDP 连接
`netstat -ant` | 显示没有反向 DNS 查找的 IP 地址
选项 | 说明
:- | :-
`netstat` | 活动连接
`netstat -a` | 所有连接
`netstat -at` | 所有 TCP 连接
`netstat -au` | 所有 UDP 连接
`netstat -ant` | 显示没有反向 DNS 查找的 IP 地址
`netstat -tnl` | 监听 TCP 端口
`netstat -unl` | 监听 UDP 端口
### 网络
选项 | 说明
:- | :-
`netstat -i` | 显示网络接口
`netstat -ie` | 显示网络接口扩展信息
`netstat -n` | 仅显示 IP 地址
`netstat -F` | 尽可能显示 IP 地址的域名
### 路由
选项 | 说明
:- | :-
`netstat -r` | 显示路由表
`netstat -rn` | 显示路由表,不解析主机
### 统计数据
<!--rehype:wrap-class=row-span-3-->
选项 | 说明
:- | :-
`netstat -s` | 显示统计信息
`netstat -st` | 显示 TCP 统计信息
`netstat -su` | 显示 UDP 统计信息
`netstat -ltpe` | 使用进程信息和扩展信息显示 TCP 的侦听连接
`netstat -tp` | 显示带有 PID 编号的服务名称
`sudo netstat -nlpt` | 列出进程名称/PID 和用户 ID
`netstat -nlptue` | 所有带有 PID 和扩展信息的侦听端口
`netstat -M` | 显示伪装的连接
### 显示没有域名的 TCP 连接
```bash
$ netstat --tcp --numeric
```
### 显示活动/已建立的连接
```bash
$ netstat -atnp | grep ESTA
```
### 获取活动连接的连续列表
```bash
$ watch -d -n0 "netstat -atnp | grep ESTA"
```
### 显示到特定端口的所有打开连接
```bash
$ netstat -anp | grep":"
```
插入`端口`号(上图)代替冒号 `:`
### 检查服务是否正在运行
```bash
$ sudo netstat -aple | grep ntp
```
你可以用`http``smtp`代替`ntp`
Netstat 安全命令
---
### 显示具有大量连接的 IP
<!--rehype:wrap-class=col-span-3-->
```bash
$ netstat -tn 2>/dev/null | grep :80 | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -nr | head
```
<!--rehype:className=wrap-text -->
### 连接到端口 80 的 IP 地址
<!--rehype:wrap-class=col-span-3-->
```bash
$ netstat -tn 2>/dev/null | grep ':80 ' | awk '{print $5}' |sed -e 's/::ffff://' | cut -f1 -d: | sort | uniq -c | sort -rn | head
```
<!--rehype:className=wrap-text -->
### 显示端口 80 上的活动连接数
```bash
$ netstat -an |grep :80 |wc -l
```
### 仅显示外部 IP 地址
<!--rehype:wrap-class=col-span-2-->
```bash
$ netstat -antu | grep :80 | grep -v LISTEN | awk '{print $5}'
```
### 显示活动 SYNC_REC
<!--rehype:wrap-class=row-span-2-->
以下命令将输出服务器上正在发生和正在发生的活动 `SYNC_REC` 数量。数量应该很低(小于 `5`)。如果该数字为两位数,则您可能正在遭受 `DoS` 攻击或被邮件轰炸。
```bash
$ netstat -n -p|grep SYN_REC | wc -l
```
#### 列出发送 SYN_REC 连接的唯一 IP 地址
```bash
$ netstat -n -p | grep SYN_REC | awk '{print $5}' | awk -F: '{print $1}'
```
<!--rehype:className=wrap-text -->
与上面的命令一样,该命令也列出了发送 `SYN_REC` 连接状态的节点的所有唯一 `IP` 地址
### 每个远程 IP 的连接数
<!--rehype:wrap-class=col-span-2-->
```bash
$ netstat -antu | awk '{print $5}' | awk -F: '{print $1}' | sort | uniq -c | sort -n
```
<!--rehype:className=wrap-text -->
或者
```bash
$ netstat -antu | awk '$5 ~ /[0-9]:/{split($5, a, ":"); ips[a[1]]++} END {for (ip in ips) print ips[ip], ip | "sort -k1 -nr"}'
```
<!--rehype:className=wrap-text -->
### 检查开放端口ipv4 和 ipv6
```bash
$ netstat -plntu
```
### 检查开放端口ipv4 和 ipv6
```bash
$ netstat -plnt
```
### 每个 IP 的打开连接数
```bash
$ netstat -an | grep 80 | wc -l
```
### 活跃的互联网连接
```bash
$ netstat -pnut -w | column -t -s $'\t'
```

View File

@ -0,0 +1,7 @@
<svg viewBox="0 0 36 36" xmlns="http://www.w3.org/2000/svg" fill="currentColor" height="1em" width="1em">
<path d="M26.58 32h-18a1 1 0 1 0 0 2h18a1 1 0 0 0 0-2Z"/>
<path d="M31.73 15.4h-6.17a18.87 18.87 0 0 1-1.62 2.52 2.33 2.33 0 0 1 .33 1.19 22 22 0 0 0 5 .45 11.88 11.88 0 0 1-.61 1.53h-.56a17.41 17.41 0 0 1-4.32-.56 2.29 2.29 0 0 1-3 .62 18.43 18.43 0 0 1-7 3.5 2.34 2.34 0 0 1-1.57 1.79l-.29.06a11.93 11.93 0 0 1-3.39-2.8h.66a2.33 2.33 0 0 1 4.37-.58A16.94 16.94 0 0 0 19.78 20a2.32 2.32 0 0 1-.18-1.17c-.42-.24-.84-.49-1.25-.76a17.53 17.53 0 0 1-5.35-5.6 2.31 2.31 0 0 1-2.28-.63 27.31 27.31 0 0 0-5 4.74v-.57a12 12 0 0 1 .14-1.73 18.75 18.75 0 0 1 4.2-3.8 2.28 2.28 0 0 1 1.1-2.25c-.12-.43-.24-.86-.33-1.3 0-.14 0-.29-.11-.64a12 12 0 0 1 1.37-.87c.1.59.14.9.21 1.21s.2.85.32 1.27h.25a2.33 2.33 0 0 1 1.13.63 18.59 18.59 0 0 1 6.39-1L23 3A14 14 0 0 0 3.75 16c0 .45 0 .89.07 1.33A14 14 0 0 0 31.76 16c0-.2-.02-.4-.03-.6Z" />
<path d="M14.26 11.64a16 16 0 0 0 4.93 5.23c.34.23.69.43 1 .63a2.28 2.28 0 0 1 2.58-.57 17.29 17.29 0 0 0 1-1.54h-1.6A3.68 3.68 0 0 1 19 9.89l.56-.89a17.08 17.08 0 0 0-4.84.88 2.25 2.25 0 0 1-.47 1.77Z" />
<path d="M26.85 1.14 21.13 11a1.28 1.28 0 0 0 1.1 2h11.45a1.28 1.28 0 0 0 1.1-2l-5.72-9.86a1.28 1.28 0 0 0-2.21 0Z"/>
<path fill="none" d="M0 0h36v36H0z"/>
</svg>

After

Width:  |  Height:  |  Size: 1.3 KiB