>_ IzzyLab

Hands-on task

This task is step 5 of the course “Networking and HTTP from the terminal”

bash /dev/tcp: check a port

medium~10 min

Write a script /root/portcheck.sh: it takes a port number and prints open if that TCP port on localhost is open, and closed otherwise. Check it with bash itself, without nc/telnet/curl/ss. Then use it to check port 8000 and write the result to /root/status.txt.

What you'll practise:
  • bash
  • /dev/tcp
  • port check
  • exit codes
  • subshell

Topic breakdown

bash has a built-in pseudo-device /dev/tcp/HOST/PORT: trying to open it for writing/reading initiates a TCP connection without external utilities like nc. A successful redirect (exit code 0) means the port is open, a failure means it's closed. The attempt is wrapped in a subshell with stderr silenced, and by the exit code, via if/else, you choose which word to write.

What's checked

  1. the state of port 8000 is recorded correctly
  2. the script detects an open port
  3. the script detects a closed port
  4. the check uses bash itself

What it looks like