在 Linux 中使用 netcat 命令扫描端口

无论您想在备用端口上使用 SSH 还是将 Web 应用程序部署到特定端口,第一步始终是检查该端口是否正在使用。

netcat 实用程序是排除网络故障的首选工具之一,也可用于扫描端口。

为了 example,如果我想检查端口号 22 在我的本地 VM 上是否打开,我将使用 netcat 命令,如下所示:

nc -zvn 192.168.1.6 22

如您所见,端口号 22 已打开供连接使用。 那很简单。

但是等等,您可以使用 netcat 命令做更多的事情。

使用 netcat 命令扫描端口

您需要先安装 netcat 命令,因为它没有预装在许多发行版中。

对于基于 Debian/Ubuntu 的发行版:

sudo apt install netcat

为了 Fedora 和瑞尔:

sudo dnf install nc 

现在,让我们开始扫描多个端口。

使用netcat命令扫描多个端口

要使用 netcat 一次扫描多个端口,您需要遵循给定的命令语法:

nc -zvn <target> port1 port2 port3

这里,

  • -z 用于指示 netcat 在不建立连接的情况下扫描端口。
  • -v 产生更详细的输出。
  • -n 停止netcat进行域名解析。

使用netcat命令扫描特定范围内的端口

确实,您可以使用前面的方法扫描多个端口,但是如果您想扫描超过 50 个或 100 个端口怎么办? 您可以定义范围。

为了 example,如果我想扫描 1 到 100 之间的端口,这将是我的命令:

nc -vz -w3 google.com 1-100
[email protected]:~$ nc -vz -w3 google.com 1-100
nc: connect to google.com (142.250.183.110) port 1 (tcp) timed out: Operation now in progress
nc: connect to google.com (2404:6800:4009:823::200e) port 1 (tcp) failed: Network is unreachable
nc: connect to google.com (142.250.183.110) port 2 (tcp) timed out: Operation now in progress
nc: connect to google.com (2404:6800:4009:823::200e) port 2 (tcp) failed: Network is unreachable
nc: connect to google.com (142.250.183.110) port 3 (tcp) timed out: Operation now in progress
nc: connect to google.com (2404:6800:4009:823::200e) port 3 (tcp) failed: Network is unreachable
nc: connect to google.com (142.250.183.110) port 4 (tcp) timed out: Operation now in progress
nc: connect to google.com (2404:6800:4009:823::200e) port 4 (tcp) failed: Network is unreachable
nc: connect to google.com (142.250.183.110) port 5 (tcp) timed out: Operation now in progress
nc: connect to google.com (2404:6800:4009:823::200e) port 5 (tcp) failed: Network is unreachable
nc: connect to google.com (142.250.183.110) port 6 (tcp) timed out: Operation now in progress
nc: connect to google.com (2404:6800:4009:823::200e) port 6 (tcp) failed: Network is unreachable
nc: connect to google.com (142.250.183.110) port 7 (tcp) timed out: Operation now in progress
nc: connect to google.com (2404:6800:4009:823::200e) port 7 (tcp) failed: Network is unreachable
nc: connect to google.com (142.250.183.110) port 8 (tcp) timed out: Operation now in progress
nc: connect to google.com (2404:6800:4009:823::200e) port 8 (tcp) failed: Network is unreachable
nc: connect to google.com (142.250.183.110) port 9 (tcp) timed out: Operation now in progress
nc: connect to google.com (2404:6800:4009:823::200e) port 9 (tcp) failed: Network is unreachable
nc: connect to google.com (142.250.183.110) port 10 (tcp) timed out: Operation now in progress
nc: connect to google.com (2404:6800:4009:823::200e) port 10 (tcp) failed: Network is unreachabl

当然是google,你不能指望让他们给你开放端口。 但是你可以将它用于你的服务器并且可能会找到开放端口

不可用端口列表似乎很长,对吗? 在这种情况下,您可以使用 grep 命令仅获取打开的端口:

netcat -w1 -znv 192.168.1.6 1-100 2>&1 | grep succeeded
使用 grep 命令在 linux 中查找 fetch 打开的端口

这里,

  • -w1 将强制 netcat 命令为每个端口等待 1 秒。
  • 2&1 重定向标准错误。

总结

这是使用 netcat 命令扫描开放端口的快速指南。 既然你找到了打开的,也许你想知道如何 close 那些端口。

我希望本指南可以解决您之前的任何疑问,如果没有,请在评论中告诉我。

Linux基础培训