从一个bash脚本如何快速查找端口445是否在服务器上打开/监听。

我已经试过几个办法了,但我想快点: 1. lsof -i:445(需要数秒) 2. netstat -an |grep 445 |grep LISTEN(需要数秒) 3.Telnet(不返回) 4. 服务器上没有Nmap、netcat

知道一种不先枚举然后再grep的方法将会很好。


当前回答

nc -l 8000

其中8000是端口号。如果端口是空闲的,它将启动一个可以轻松关闭的服务器。如果不是,它将抛出一个错误:

nc: Address already in use

其他回答

你可以使用netcat。

nc ip port < /dev/null

连接到服务器,然后直接重新关闭连接。如果netcat不能连接,它将返回一个非零退出码。退出码存储在变量$?举个例子,

nc ip port < /dev/null; echo $?

当且仅当netcat成功连接到端口时返回0。

nc -l 8000

其中8000是端口号。如果端口是空闲的,它将启动一个可以轻松关闭的服务器。如果不是,它将抛出一个错误:

nc: Address already in use

Nmap是正确的工具。 简单地使用nmap example.com -p 80

您可以从本地或远程服务器使用它。 它还可以帮助您识别防火墙是否正在阻止访问。

如果你使用的是iptables,试试:

iptables -nL

or

iptables -nL | grep 445

tcp是一个开销非常低的好工具。它还有一个timeout参数,以使它更快:

[root@centos_f831dfb3 ~]# tcping 10.86.151.175 22 -t 1
10.86.151.175 port 22 open.
[root@centos_f831dfb3 ~]# tcping 10.86.150.194 22 -t 1
10.86.150.194 port 22 user timeout.
[root@centos_f831dfb3 ~]# tcping 1.1.1.1 22 -t 1
1.1.1.1 port 22 closed.