--- title: 检查端口是否能通的几种方法 abbrlink: ecf46dbb categories: - 基础运维 tags: - Bash - Command - 测试 - 网络 date: 2021-12-20 20:26:07 --- 服务器上运行了一个服务,想测试一下端口通不通,可以通过以下一些方法来测试。 ## telnet 常见的tcp端口测试都是用的telnet,用法也很简单 ```bash telnet 10.0.0.7 22 ``` 成功会显示: ```console Trying 10.0.0.7... Connected to 10.0.0.7. Escape character is '^]'. SSH-2.0-OpenSSH_7.4 ``` 失败会显示: ```console Trying 10.0.0.7... telnet: Unable to connect to remote host: Connection refused ``` ## ssh 使用 ```bash ssh root@localhost -p 8000 ``` 失败会显示 ```text ssh: connect to host localhost port 8001: Connection refused ``` ## curl curl ip:port 失败会显示 ```text curl: (7) Failed to connect to localhost port 8001: Connection refused ``` ## netcat ### 检查TCP端口 下面的命令会检查远程主机上是否打开了端口 80、22 和 9000: ```bash nc -zv 192.168.2.1 80 22 9000 ``` 也可以指定端口扫描的范围(速度非常快): ```bash nc -zv 192.168.2.1 20-9000 ``` ### 检查UDP端口 ```bash nc -vuz 10.16.83.10 161 ``` 通的话, 会显示: ![](https://static.zahui.fan/images/202309121124086.png) 不通的话, 会显示: ![image.png](https://static.zahui.fan/images/202309121126143.png)