Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Linux by (6.1k points)

How to find out about a port 445 whether it is listening on a server or not in Linux?

I tried the options listed down below:

1. lsof -i :445 (Takes seconds)

2. netstat -an |grep 445 |grep LISTEN (Takes seconds)

3. telnet (it doesn't return)

4. nmap, netcat are not available on the server

But, can we do it faster and efficiently?

1 Answer

0 votes
by (11.7k points)

While researching about the concept of the port on Linux, I got to know that Bash natively supports tcp connections as file descriptors to use:

exec 6<>/dev/tcp/ip.addr.of.server/445

echo -e "GET / HTTP/1.0\n" >&6

cat <&6

Here, I am preferring 6 as the file descriptor because 0,1,2 are stdin, stdout, and stderr. 5 is sometimes used by Bash for child processes, so 3,4,6,7,8, and 9 should be safe.

Now if you want to test for listening on a local server in a script, try these commands:

exec 6<>/dev/tcp/127.0.0.1/445 || echo "No one is listening!"

exec 6>&- # close output connection

exec 6<&- # close input connection

If you want to find if someone is listening, attempt to connect by loopback. If it goes wrong, then the port is closed or access is not there. Then, close the connection.

You can modify this for your own use case for example:  sending an email, exiting the script on failure, or starting the required service.

Related questions

0 votes
1 answer
asked Feb 18, 2021 in Linux by rahulnayar01123 (6.1k points)
0 votes
1 answer
asked Apr 2, 2021 in Linux by sheela_singh (9.5k points)
0 votes
1 answer

Browse Categories

...