可以通过telnet、ssh、curl、nmap、nc测试端口的连通性。
方法一:telnet
telnet为用户提供了在本地计算机上完成远程主机工作的能力,因此可以通过telnet来测试端口的连通性。
telnet只能用于测试TCP端口,而nc即可用于测试TCP端口也可用来测试UDP端口。
用法:telnet ip port
[root@zq /]# telnet 10.21.137.42 1520
Trying 10.21.137.42...
Connected to 10.21.137.42. #==>出现Connected表示连通了,说
Escape character is '^]'. #==>按“ctrl+]”退出此地。
hello world #发送的消息,一行是一条消息,直接按“Enter”键
^]
telnet> quit #==>按“quit”退出连接
Connection closed.
方法二:ssh
SSH 是目前较可靠,专为远程登录会话和其他网络服务提供安全性的协议,在linux上可以通过ssh命令来测试端口的连通性。
用法:
ssh -v -p port username@ip
说明:
-v 调试模式(会打印日志)
-p 指定端口
username:远程主机的登录用户
ip:远程主机
如果远程主机开通了相应的端口,会有如下图所示的建立成功的提示
[root@zq]# ssh -v 10.21.137.42 -p 1520
OpenSSH_6.6.1, OpenSSL 1.0.1e-fips 11 Feb 2013
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 56: Applying options for *
debug1: Connecting to 10.21.137.42 [10.21.137.42] port 1520.
debug1: Connection established. # 连接成功
debug1: permanently_set_uid: 0/0
debug1: identity file /root/.ssh/id_rsa type -1
debug1: identity file /root/.ssh/id_rsa-cert type -1
debug1: identity file /root/.ssh/id_dsa type -1
debug1: identity file /root/.ssh/id_dsa-cert type -1
debug1: identity file /root/.ssh/id_ecdsa type -1
debug1: identity file /root/.ssh/id_ecdsa-cert type -1
debug1: identity file /root/.ssh/id_ed25519 type -1
debug1: identity file /root/.ssh/id_ed25519-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_6.6.1
方法三:curl
curl是利用URL语法在命令行方式下工作的开源文件传输工具。也可以用来测试端口的连通性。
用法:curl ip:port
说明:
ip:是测试主机的ip地址
port:是端口,比如80
如果远程主机开通了相应的端口,都会输出信息,如果没有开通相应的端口,则没有任何提示,需要CTRL+C断开
方法四:nmap
nmap用于在远程机器上探针侧网络,执行安全扫描、网络审计、搜索开放端口,它会扫描远程在线主机的操作系统、开放的端口
zq@ubuntu:~$ nmap smtp.mxhichina.com # 查找开放的端口、服务、MAC地址
Starting Nmap 7.91 ( https://nmap.org ) at 2021-07-06 06:33 PDT
Nmap scan report for smtp.mxhichina.com (42.120.226.4)
Host is up (0.031s latency).
Other addresses for smtp.mxhichina.com (not scanned): 42.120.219.29
Not shown: 997 filtered ports
PORT STATE SERVICE
25/tcp open smtp # 开放的端口
80/tcp open http
465/tcp open smtps
Nmap done: 1 IP address (1 host up) scanned in 35.59 seconds
zq@ubuntu:~$ nmap smtp.mxhichina.com -p 465 # 查找特定端口是否开放
Starting Nmap 7.91 ( https://nmap.org ) at 2021-07-06 06:34 PDT
Nmap scan report for smtp.mxhichina.com (42.120.219.29)
Host is up (0.021s latency).
Other addresses for smtp.mxhichina.com (not scanned): 42.120.226.4
PORT STATE SERVICE
465/tcp open smtps
Nmap done: 1 IP address (1 host up) scanned in 2.26 seconds
方法五:nc
实现任意TCP/UDP端口的侦听、机器间的机器之间网络测速
zq@ubuntu:~$ nc -vz -w 2 smtp.mxhichina.com 25 # -v可视化,-z扫描时不发送数据,-w超时几秒,后面跟数字
Connection to smtp.mxhichina.com 25 port [tcp/smtp] succeeded!
zq@ubuntu:~$ nc -v -w 2 smtp.mxhichina.com 25
Connection to smtp.mxhichina.com 25 port [tcp/smtp] succeeded!
220 smtp.aliyun-inc.com MX AliMail Server
zq@ubuntu:~$ nc -w 2 smtp.mxhichina.com 25
220 smtp.aliyun-inc.com MX AliMail Server
参考链接:
nmap命令详解 - 马昌伟 - 博客园 nmap命令详解
nc命令用法举例 - nmap - 博客园 nc命令用法举例