合 测试端口是否连通的几种方式
telnet
命令:
1 | telnet 192.168.8.8 22 |
回车后,若端口是开放的,则会显示相关的协议,例如:SSH、mysql等,或者为空。
wget
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | C:\Users\lhr>wget 192.168.8.8:22 -v --10:05:11-- http://192.168.8.8:22/ => `index.html' Connecting to 192.168.8.8:22... connected! HTTP request sent, awaiting response... Read error (No such file or directory) in headers. Retrying. --10:05:11-- http://192.168.8.8:22/ (try: 2) => `index.html' Connecting to 192.168.8.8:22... connected! HTTP request sent, awaiting response... Read error (No such file or directory) in headers. Retrying. --10:05:11-- http://192.168.8.8:22/ (try: 3) => `index.html' Connecting to 192.168.8.8:22... connected! HTTP request sent, awaiting response... 10:05:11 ERROR -1: Malformed status line. |
curl
curl是用于从服务器传输数据或将数据传输到服务器的工具。它支持以下协议:DICT,FILE,FTP,FTPS,GOPHER,GOPHERS,HTTP,HTTPS,IMAP,IMAPS,LDAP,LDAPS,MQTT,POP3,POP3S,RTMP,RTMPS,RTSP,SCP,SFTP,SMB,SMBS,SMTP,SMTPS,TELNET,TFTP,WS和WSS
。它由 libcurl
提供支持,适用于所有与传输相关的功能
通的情况:
1 2 3 4 5 | ┌──[root@vms100.liruilongs.github.io]-[~] └─$timeout 3 curl -vvv telnet://192.168.26.55:55555 * About to connect() to 192.168.26.55 port 55555 (#0) * Trying 192.168.26.55... * Connected to 192.168.26.55 (192.168.26.55) port 55555 (#0) |
不通的情况:
1 2 3 4 5 6 7 8 9 10 | ┌──[root@vms100.liruilongs.github.io]-[~] └─$timeout 3 curl -vvv telnet://192.168.26.55:443 * About to connect() to 192.168.26.55 port 443 (#0) * Trying 192.168.26.55... * 拒绝连接 * Failed connect to 192.168.26.55:443; 拒绝连接 * Closing connection 0 curl: (7) Failed connect to 192.168.26.55:443; 拒绝连接 ┌──[root@vms100.liruilongs.github.io]-[~] └─$ |
nc或nact
nc
或者 ncat
命令也可以用于测试远程主机是否在指定端口上侦听或建立到该端口的连接。
1 2 3 | ┌──[root@vms100.liruilongs.github.io]-[~] └─$which nc /usr/bin/nc |
例如,要测试远程主机的 55555 端口是否打开,可以使用以下命令:
通的情况
1 2 3 4 5 6 7 | ┌──[root@vms100.liruilongs.github.io]-[~] └─$nc -vz 192.168.26.55 55555 Ncat: Version 7.50 ( https://nmap.org/ncat ) Ncat: Connected to 192.168.26.55:55555. Ncat: 0 bytes sent, 0 bytes received in 0.01 seconds. ┌──[root@vms100.liruilongs.github.io]-[~] └─$ |
不通的情况
1 2 3 4 | ┌──[root@vms100.liruilongs.github.io]-[~] └─$nc -vz 192.168.26.55 443 Ncat: Version 7.50 ( https://nmap.org/ncat ) Ncat: Connection refused. |
或者可以使用 ncat
,ncat 是 nmap 网络工具套件的一部分,是一个更现代的工具。它被设计为 nc 的更丰富的替代品。它支持 SSL/TLS 加密、IPv6、SOCKS 代理等。
1 2 3 4 5 6 7 8 9 10 11 | ┌──[root@vms100.liruilongs.github.io]-[~] └─$ncat -zv 192.168.26.55 55555 Ncat: Version 7.50 ( https://nmap.org/ncat ) Ncat: Connected to 192.168.26.55:55555. Ncat: 0 bytes sent, 0 bytes received in 0.01 seconds. ┌──[root@vms100.liruilongs.github.io]-[~] └─$ncat -zv 192.168.26.55 443 Ncat: Version 7.50 ( https://nmap.org/ncat ) Ncat: Connection refused. ┌──[root@vms100.liruilongs.github.io]-[~] └─$ |
nmap
端口扫描工具,会扫描所有的端口,一般不建议使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | ┌──[root@vms100.liruilongs.github.io]-[~] └─$rpm -ql nmap || yum -y install nmap ┌──[root@vms100.liruilongs.github.io]-[~] └─$nmap -sT 192.168.26.55 443 Starting Nmap 6.40 ( http://nmap.org ) at 2023-04-11 16:44 CST Nmap scan report for 192.168.26.55 (192.168.26.55) Host is up (0.0025s latency). Not shown: 994 closed ports PORT STATE SERVICE 22/tcp open ssh 80/tcp open http 222/tcp open rsh-spx 8080/tcp open http-proxy 50000/tcp open ibm-db2 55555/tcp open unknown MAC Address: 00:0C:29:9F:48:81 (VMware) Nmap done: 2 IP addresses (1 host up) scanned in 1.74 seconds |
python
Linux 环境一般都 python
环境,要使用 Python 检查远程端口是否打开,可以使用 socket 模块
端口通的 Demo