Monday, July 20, 2020

Bash script to check if a port is accessible from Internet.

My initial solution was to run a basic server at the chosen port and execute a lambda function (or Google's Cloud Function -- basically an external API call) that checks if the port is accessible from outside and returns true/false.

But now that I started actually working on it, I am thinking can I skip the external API altogether by using the IP of the same machine??

This is the flow (let me assume I want to check if port 222 was accessible and not blocked by any security group/ISP):

  1. Start a basic server nc -l 222
  2. Get the external IP IP=$(curl --silent icanhazip.com)
  3. Try connecting to the port nc -N $IP 222 < /dev/null
  4. Check if that was successful echo $?

So basically in step 3 I did not use an API call to a lambda function that checks port from an external server. I am using the same server but using the external IP.

My question is, will this work in all cases?? Is it good enough? I mean I still feel writing an API will be foolproof but I am curious if I use the IP will the call go through the internet and come back (and doesn't change to localhost at DNS resolution)??



No comments:

Post a Comment