How to check for open port in Linux on multiple servers together ?
Suppose you have a list of servers, and you want to check for a particular port on which server its open.
So if you telnet each server on that port manually so that will take a lot of time.
In this post, we will look how to check for open ports in Linux through a script to get this job done quickly.
I. Check on one serve for port open, say port number 53 for DNS.
### telnet server-name port-number # telnet ngelinux5 53 [root@ngelinux001 ~]# nc -zv dns1-serv 53 Ncat: Version 7.50 ( https://nmap.org/ncat ) Ncat: Connection timed out.
II. Create a file with list of servers.
[root@ngelinux001 ~]# cat /tmp/tempfile2 dns2-serv f5dns1 dns3 dns1-serv {-output-truncated-}
III. Now we check if port 53 is open.
If open, we redirect server-name to a new file tempfile3 so in last we have a list of servers.
[root@ngelinux001 ~]# for i in `cat /tmp/tempfile2`; do nc -zv $i; if [ $? -eq 0 ]; then echo $i >> /tmp/tempfile3; fi; done Ncat: Version 7.50 ( https://nmap.org/ncat ) Ncat: Connection timed out. Ncat: Version 7.50 ( https://nmap.org/ncat ) Ncat: Connection timed out. Ncat: Version 7.50 ( https://nmap.org/ncat ) Ncat: Connection timed out. Ncat: Version 7.50 ( https://nmap.org/ncat ) Ncat: Connection timed out. Ncat: Version 7.50 ( https://nmap.org/ncat ) Ncat: Connection timed out. Ncat: Version 7.50 ( https://nmap.org/ncat ) Ncat: Connection timed out. Ncat: Version 7.50 ( https://nmap.org/ncat ) Ncat: Connection timed out. Ncat: Version 7.50 ( https://nmap.org/ncat ) Ncat: Connection timed out. Ncat: Version 7.50 ( https://nmap.org/ncat ) Ncat: Connection timed out. Ncat: Version 7.50 ( https://nmap.org/ncat ) Ncat: Connection timed out. Ncat: Version 7.50 ( https://nmap.org/ncat ) Ncat: Connection timed out. Ncat: Version 7.50 ( https://nmap.org/ncat ) Ncat: Connection timed out. Ncat: Version 7.50 ( https://nmap.org/ncat ) Ncat: Connection timed out. Ncat: Version 7.50 ( https://nmap.org/ncat ) Ncat: Connection timed out. Ncat: Version 7.50 ( https://nmap.org/ncat ) Ncat: Connection timed out. Ncat: Version 7.50 ( https://nmap.org/ncat ) Ncat: Connection timed out. Ncat: Version 7.50 ( https://nmap.org/ncat ) Ncat: Connection timed out. Ncat: Version 7.50 ( https://nmap.org/ncat ) Ncat: Connection timed out. Ncat: Version 7.50 ( https://nmap.org/ncat ) Ncat: Connection timed out. Ncat: Version 7.50 ( https://nmap.org/ncat ) Ncat: Connection timed out. Ncat: Version 7.50 ( https://nmap.org/ncat ) Ncat: Connection timed out. Ncat: Version 7.50 ( https://nmap.org/ncat ) Ncat: Connection timed out. Ncat: Version 7.50 ( https://nmap.org/ncat ) Ncat: Connection timed out. Ncat: Version 7.50 ( https://nmap.org/ncat ) Ncat: Connection timed out. Ncat: Version 7.50 ( https://nmap.org/ncat ) Ncat: Connection timed out. Ncat: Version 7.50 ( https://nmap.org/ncat ) Ncat: Connection timed out. Ncat: Version 7.50 ( https://nmap.org/ncat ) Ncat: Connection timed out. Ncat: Version 7.50 ( https://nmap.org/ncat )
IV. To get server list where the port is found opened.
# cat /tmp/tempfile3