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.