Help us measure site usage?

With your permission, we use self-hosted analytics to understand page usage. It is optional and does not affect the Service. Learn more.

BashbeginnercliAI generated

Port Watcher

Build a Bash script that checks whether the services on your machine are up by testing their network ports: for example, is the database listening on 5432, is the web server answering on 8080? You list the services in a simple config file, and the script tries to connect to each port with a timeout, optionally makes an HTTP request with curl to check a web service really answers, and prints a clear table of what's up and what's down, with awk. It can also run the same checks on another of your servers over SSH. Only check machines you own or manage. It's a good first shell scripting project: you'll learn Bash variables and loops, reading a config file, TCP connections from the shell, curl, awk for formatting, and exit codes.

Estimate
~7h
Steps
6
Completed by
0
Proposed by
codeseed.app

curl · GNU Awk · OpenSSH

Project roadmap

  1. 01

    Define the config and output

    ~1h

    Write README.md: the config file has one service per line (a name, a host, a port, and optionally an HTTP path to check), lines starting with # are comments, and the output is a table with the name, host:port, status (UP, DOWN or HTTP ERROR), and response time. The script exits with 0 when everything is up and 1 otherwise. Done when: README.md shows an example config and output.

  2. 02

    Read and validate the config

    ~1h

    Write portwatch.sh with set -euo pipefail. Read the config line by line, skipping comments and empty lines, and check each line: a name, a host, and a port that's a number from 1 to 65535. Report bad lines with their line number and skip them. Done when: a config with a bad port reports that line and still checks the rest.

  3. 03

    Check the ports

    ~1.5h

    For each service, try to connect with timeout 2 bash -c "</dev/tcp/HOST/PORT" (or nc -z -w 2 when available), and measure how long it took with date +%s%N. When an HTTP path is given and the port is open, call it with curl --silent --max-time 3 --output /dev/null --write-out "%{http_code}" and mark anything outside 200 to 399 as HTTP ERROR. Done when: checks on your own machine correctly show open and closed ports.

  4. 04

    Print the report

    ~1h

    Collect the results as tab-separated lines and pipe them into awk to print an aligned table with a header, coloring UP green and the rest red when the output is a terminal (check with [ -t 1 ]), and a summary line such as 5 of 6 services up. Done when: the table is neatly aligned, and the summary is correct.

  5. 05

    Check a remote server over SSH

    ~1h

    Add --remote user@host, which runs the same checks from one of your other servers with ssh -o BatchMode=yes -o ConnectTimeout=5, sending the config and script through standard input so nothing needs to be installed there. Report clearly when the SSH connection itself fails. Done when: checks run from a remote server you manage, and a failed SSH connection gives a clear message.

  6. 06

    Test it

    ~1.5h

    Start a test service with python3 -m http.server on a free port, and write tests with bats-core: an open port, a closed port, an HTTP check with 200 and 404, bad config lines, and the exit codes. Run shellcheck on the script. Done when: the tests pass, and shellcheck reports no warnings.

Ready to build this?

Get a GitHub repo and start building. Your AI reviewer checks each step as you go.

~7h · 6 steps

Tech stack

curlGNU AwkOpenSSH