Dev.to Security 🔐 Cybersecurity 👁 0 📖 2 min read

A port scanner in bash, and what it teaches you about recon

You land on a container or a jump host and nmap is not installed. Package installs are blocked. What is there? Bash, almost always. That is enough to find out what is listening. The one line that matters Bash

You land on a container or a jump host and nmap is not installed. Package installs are blocked. What is there? Bash, almost always. That is enough to find out what is listening.

The one line that matters

Bash treats paths of the form /dev/tcp/HOST/PORT as network sockets. Redirecting into one opens a TCP connection:

(echo > /dev/tcp/10.0.0.5/22) 2>/dev/null && echo "22 open"

If the connection succeeds, the subshell exits 0 and the && fires. If the port is closed, the redirect fails and nothing prints. There is no tool involved, only the shell.

From one line to a scanner

A scanner is that check inside two loops:

for ip in 10.0.0.{1..20}; do
  for port in 22 80 443 445 3389; do
    timeout 1 bash -c "echo > /dev/tcp/$ip/$port" 2>/dev/null \
      && echo "$ip:$port open"
  done
done

The timeout 1 matters. A filtered port drops packets instead of refusing them, and without a timeout each one holds the loop until the kernel gives up, which can take a minute or more per probe.

A ping sweep needs less:

for ip in 10.0.0.{1..254}; do ping -c1 -W1 $ip >/dev/null && echo $ip; done

Where it falls short

It is serial, so a /24 across several ports is slow. You can background each probe with & and wait, at the cost of messy output. It reports reachability and nothing about the service behind it. ICMP is often blocked, so the sweep can miss hosts that are alive. And everything it sends is visible to anyone watching: scanning is only for systems you are authorised to test.

What it is for

Nobody should replace nmap with this. The value is that the loop is yours. Once you can write it, you can change it: read targets from a file, write hits to a log, diff today's output against last week's, hand open ports to the next command. That is the habit that separates running tools from building them. A scanner you wrote also fails in ways you understand, which is useful the first time a firewall does something odd and the output stops making sense. You know exactly which line produced each result, so you can debug it instead of guessing what a black box meant.

Practise and go further

A real terminal helps more than reading. The free VaultSync recon CTF on Codelivly drops you into a shell to enumerate step by step. If the commands themselves still feel unfamiliar, the free Linux commands cheat sheet lists 60+ of them with syntax and examples.

When you want to go beyond one-liners, the Shell Scripting for Pentesters book covers building Bash and Python tools, automating recon and scanning, and scripting red team workflows. The shop's Black Friday sale, up to 70% off selected books and bundles, ends 29 Nov 2026.

📰 Read the original article on Dev.to Security

Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.