$_ CLI In Depth
Learning progress 0%
build networking sockets ssh

Build: A Port Health Checker

3 min read Module 16: Networking Tools Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:07

The specification

The script is named portcheck.sh and takes one or more port numbers as arguments. For each port, it tries to connect to 127.0.0.1 on that port and waits at most one second. It prints a line with the port number and the word open or closed. It exits with status 0 when every port is open and with status 1 when any port is closed. A missing argument prints a usage message and exits with status 2.

Design decisions

The connection test uses the /dev/tcp path, which needs no extra program and works in bash. The timeout command limits the wait, because a connect to a filtered address can hang for a long time, and a probe that hangs is worse than one that fails. The result is recorded per port, so one script run reports every port instead of stopping at the first closed one.

The reference solution

The solution below follows the specification. Read it against the spec, one rule at a time.

BASH18 lines
#!/usr/bin/env bash
# Usage: portcheck.sh PORT...
# Prints "PORT open" or "PORT closed" for each port on 127.0.0.1.
set -u
if [ "$#" -eq 0 ]; then
  echo "usage: portcheck.sh PORT..." >&2
  exit 2
fi
status=0
for port in "$@"; do
  if timeout 1 bash -c "exec 3<>/dev/tcp/127.0.0.1/$port" 2>/dev/null; then
    echo "$port open"
  else
    echo "$port closed"
    status=1
  fi
done
exit "$status"

The usage check comes first, so the loop never runs with no ports. The loop tests every argument and records a failure in status instead of exiting, which is what makes the script report all ports. The timeout command wraps a bash process that makes the connection, and its status is 0 only when the connection succeeds.

Why the test runs in a child bash

The timeout command runs its argument as a program, so the connection is made in a child bash. That child exits when the redirection finishes, which closes the descriptor. If the connection hangs, timeout kills the child and reports a failure, which the if statement treats as closed.

Testing the checker

Test the checker against a port that is open and one that is closed. To make a port open for a test, start a short-lived listener under timeout, as the raw TCP unit showed. The lab does exactly this, so the result does not depend on any service that happens to run on the machine.

Extensions to try

Add an option that reads ports from a file, one per line, or print the result as a single line for a log. Each change is small and reuses one of the patterns from this module. Test each one against an open port and a closed port.

Terminal lab · 2 steps · real Linux in your browser

Build: a port health checker

Write a checker for one port first, then extend it to many ports with a usage message and the right exit statuses.

01
Check one port
Create portcheck.sh in /workspace. It takes one port number as its argument. It tries to connect to 127.0.0.1 on that port within one second, prints the port and open when the connection works and the port and closed when it does not, and exits 0 for open and 1 for closed: ``bash cat > portcheck.sh <<'EOF' #!/usr/bin/env bash port=$1 if timeout 1 bash -c "exec 3<>/dev/tcp/127.0.0.1/$port" 2>/dev/null; then echo "$port open" exit 0 fi echo "$port closed" exit 1 EOF ``
Show hint for step 1
The timeout command wraps a child bash that opens the /dev/tcp connection. The if statement reads the result.
02
Check many ports with usage
Replace portcheck.sh with a version that accepts any number of ports. For each port, print a line with the port and open or closed, and keep going after a closed port. Exit 1 if any port is closed, and exit 2 with a usage message on standard error when no arguments are given. Use a loop over the arguments and a status variable: ``bash #!/usr/bin/env bash # Usage: portcheck.sh PORT... set -u if [ "$#" -eq 0 ]; then echo "usage: portcheck.sh PORT..." >&2 exit 2 fi status=0 for port in "$@"; do if timeout 1 bash -c "exec 3<>/dev/tcp/127.0.0.1/$port" 2>/dev/null; then echo "$port open" else echo "$port closed" status=1 fi done exit "$status" ``
Show hint for step 2
Set the status to 1 inside the loop when a port is closed, and exit with that variable at the end, so every port is still reported.
Check yourself Question 1 of 4
Concept Check Exit status for a closed port recall

What exit status does the port checker return when at least one port is closed?

Recap & Key Takeaways

The checker tests each port with /dev/tcp inside a timeout, so a hanging connect cannot stall the probe.
Recording a failure per port, instead of exiting at the first closed port, reports every result in one run.
A usage check that exits with status 2 separates a bad invocation from a closed port, which exits with status 1.
Test the probe with a listener that you start for the test, so the result does not depend on other services.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue