What you will build
Step 1: 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:
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
EOFStep 2: 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:
#!/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"Files
walkthrough.sh: every step of the reference solution, in order.
How it is checked
Each step in the guided lab has a check script that runs on the lab machine after you finish. It passes only when the files and output it expects are there, so the lab cannot be completed by reading alone.