Network for Developer: Connection testing with ping and netcat

tanut aran
CODEMONDAY
Published in
3 min readJul 10, 2020

It’s always mysterious and sometimes painful for us developer to develop or deploy our app over network.

In this short tutorial, we will find the way to test our app, server or database connection.

Credit: submarinecablemap.com

Let’s start with the classic ping.

Ping the Machine !

Ping is on the Internet Layer

You can ping your router, server, dns etc.

// ping the google DNS server
$ ping 0.0.0.0
PING 0.0.0.0 (127.0.0.1) 56(84) bytes of data.
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.032 ms
...
// ping server
$ ping www.bitbucket.org
// ping our router
// see the ip of gateway with ifconfig/ipconfig command
$ ping 192.168.0.1

But what about ping a simple python server we just wrote:

$ mkdir simple-http-server
$ cd simple-http-server
$ echo hello > hello.html
$ python3 -m http.server 9999
$ ping localhost:9999
... hang up ...

That’s not working because our simple server is not written to handle ‘ping packet’. No handle means no reply back so it hang up until timeout.

Ping the AWS RDS database machine also not working:

$ ping <AWS_RDS_ENDPOINT>
... hang up ...

This is also because the server doesn’t support the ping protocol i.e. ICMP (Internet Control Message Protocol).

So when the ping is not working the causes is:

  1. Machine dead
  2. No internet connection
  3. Wrong routing to that machine
  4. Machine alive but not support ping

Netcat the Application !

Netcat is on the Transport layer

You might wonder how to ping with a port. It’s not supported. Port is not support down to ping’s internet layer. See the diagram:

TCP allows you to have port.

and netcat or nc is a way to ping TCP port i.e. things we usually write and setup is in the application layer and connected with port specified.

Note: ncat is modern implementation of netcat

Example: Testing our simple web server

$ nc -vz localhost 9999
Connection to localhost 9999 port [tcp/*] succeeded!

Example: Testing whether git server open SSH port

$ nc -vz github.com 22
Connection to github.com 22 port [tcp/ssh] succeeded!

Example: Testing AWS RDS Postgres Database

$ nc -vz <AWS_RDS_ENDPOINT> 5432
Connection to <AWS_RDS_ENDPOINT> 5432 port [tcp/postgresql] succeeded!

Example: Testing Docker My SQL

$ docker run -p 3306:3306 \
--env MYSQL_ROOT_PASSWORD=123456 \
-d mysql:8.0
$ nc -vz localhost 3306
Connection to localhost 3306 port [tcp/mysql] succeeded!

and many more.

Hope this help. Cheers!

Web Application | IoT

www.codemonday.com

--

--