我期待测试主机上的特定端口是否打开.我在用:
nc -z host 22 nc -z host 80 nc -z host 443 nc -z host 8080
这是有效的,但使用像单线路一样很好:
nc -z host 22 80 443 8080
这不起作用.
如果可能的话,我想避免端口范围nc -z host 22-8080,如果可能的话,因为我想检查的端口#s有很大的差距.此外,我不想扫描每个端口,并被视为扫描开放端口.
如果没有编写bash循环,我可以选择测试端口是否打开?我有几十个主机,每个主机都有一些端口可供检查.
解决方法
我使用下面的-w 1将超时限制为1秒.我也使用-v作为评论中提到的原因.我用-n来拒绝反向DNS查找的延迟……
[mpenning@tsunami ~]$for i in $(echo "172.16.1.1,172.16.1.5"|tr "," "\n"); do echo -e "22\n80\n443\n8080" | xargs -i nc -w 1 -zvn $i {}; done (UNKNOWN) [172.16.1.1] 22 (ssh) open (UNKNOWN) [172.16.1.1] 80 (www) : Connection timed out (UNKNOWN) [172.16.1.1] 443 (https) open (UNKNOWN) [172.16.1.1] 8080 (http-alt) : Connection timed out (UNKNOWN) [172.16.1.5] 22 (ssh) open (UNKNOWN) [172.16.1.5] 80 (www) open (UNKNOWN) [172.16.1.5] 443 (https) open (UNKNOWN) [172.16.1.5] 8080 (http-alt) : Connection refused [mpenning@tsunami ~]$