从一个bash脚本如何快速查找端口445是否在服务器上打开/监听。
我已经试过几个办法了,但我想快点: 1. lsof -i:445(需要数秒) 2. netstat -an |grep 445 |grep LISTEN(需要数秒) 3.Telnet(不返回) 4. 服务器上没有Nmap、netcat
知道一种不先枚举然后再grep的方法将会很好。
从一个bash脚本如何快速查找端口445是否在服务器上打开/监听。
我已经试过几个办法了,但我想快点: 1. lsof -i:445(需要数秒) 2. netstat -an |grep 445 |grep LISTEN(需要数秒) 3.Telnet(不返回) 4. 服务器上没有Nmap、netcat
知道一种不先枚举然后再grep的方法将会很好。
当前回答
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.
其他回答
如果你使用的是iptables,试试:
iptables -nL
or
iptables -nL | grep 445
您也可以使用netcat命令
[location of netcat]/netcat -zv [ip] [port]
or
nc -zv [ip] [port]
-z -设置nc简单地扫描监听守护进程,而不实际发送任何数据给它们。 -v -启用详细模式。
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.
这里有一个非常简短的“快速回答”:如何测试远程TCP端口是否从Shell脚本打开?
nc -z <host> <port>; echo $?
我使用127.0.0.1作为“远程”地址。
如果端口是开放的,返回“0”,如果端口是关闭的,返回“1”
e.g.
nc -z 127.0.0.1 80; echo $?
-z指定nc只扫描监听守护进程, 没有发送任何数据给他们。使用此选项是错误的 conjunc - 带有-l选项的Tion。
你可以这样使用netstat来获得更快的结果:
在Linux上:
netstat -lnt | awk '$6 == "LISTEN" && $4 ~ /\.445$/'
在Mac:
netstat -anp tcp | awk '$6 == "LISTEN" && $4 ~ /\.445$/'
这将输出在端口上侦听的进程列表(本例中为445),如果端口空闲,则不输出任何内容。