看一个Demo,使用 Linux 中的 /dev/tcp 虚拟文件系统连接到远程主机的 55555 端口。用于测试远程主机是否在该端口上侦听或建立到该端口的连接。 /dev/proto/host/port/ 对应测试数据 /dev/tcp/$host/$port
这里为了好看,我们做一些简单修饰
通的情况
1 2 3
┌──[root@vms100.liruilongs.github.io]-[~] └─$(timeout 1 bash -c '</dev/tcp/192.168.26.55/55555' && echo PORT OPEN || echo PORT CLOSED) 2>/dev/null PORT OPEN
不通的情况
1 2 3
┌──[root@vms100.liruilongs.github.io]-[~] └─$(timeout 1 bash -c '</dev/tcp/192.168.26.55/443' && echo PORT OPEN || echo PORT CLOSED) 2>/dev/null PORT CLOSED
[root@master ~]# </dev/tcp/172.30.127.23/3306 bash: connect: No route to host bash: /dev/tcp/172.30.127.23/3306: No route to host [root@master ~]# </dev/tcp/192.168.26.55/55555 bash: connect: Network is unreachable bash: /dev/tcp/192.168.26.55/55555: Network is unreachable [root@master ~]#
Network is unreachable :当源设备无法找到到达目标网络的路由时,会出现此错误。这意味着源设备的路由表中没有目标设备所在的网络的条目。这可能是因为网络已经关闭,或者路由表中存在配置错误。
No route to host :当源设备可以找到到达目标网络的路由,但无法找到该网络上特定主机的路由时,会出现此错误。这意味着源设备的路由表中有目标设备所在的网络的条目,但没有目标设备的特定IP地址的条目。这可能是因为目标设备已关闭,或者路由表中存在配置错误。
┌──[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]-[~] └─$
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
1 2 3 4 5 6 7 8 9
┌──[root@vms100.liruilongs.github.io]-[~] └─$python2 Python 2.7.5 (default, Aug 4 2017, 00:39:18) [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux2 Type "help", "copyright", "credits" or "license"for more information. >>> import socket >>> sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) >>> sock.connect(('192.168.26.55', 55555)) >>>
端口不通的 Demo
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
┌──[root@vms100.liruilongs.github.io]-[~] └─$python2 Python 2.7.5 (default, Aug 4 2017, 00:39:18) [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux2 Type "help", "copyright", "credits" or "license"for more information. >>> import socket >>> sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) >>> sock.connect(('192.168.26.55', 443)) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/lib64/python2.7/socket.py", line 224, in meth return getattr(self._sock,name)(*args) socket.error: [Errno 111] Connection refused >>> ┌──[root@vms100.liruilongs.github.io]-[~] └─$