Bash TCP port scan
TL;DR
With this short trick, you can check for open TCP ports on a target system, only using bash built-in features.
On Unix-like systems, there are pseudo-devices who are located at /dev
. These are interfaces without actual hardware connection.
Let’s execute a command on /dev/tcp/<host>/<port>
pseudo-device file, to let Bash open a TCP connection to the associated socket.
In this example, we try to connect at target IP 10.0.0.21 to port 22/TCP. The command will print out “open”, if we get any replay from the target.
|
|
There is also an /dev/udp
pseudo-device, but because UDP uses “stateless” connection, we won’t get any reply from the UDP port. So we can’t check for open UDP ports.
With this one-liner, you can quickly check multiple targets for open ports.
|
|
References
- Advanced Bash-Scripting Guide: Chapter 29. /dev and /proc, tldp.org.
- TCP Port Scanner in Bash, catonmat.net.
- 3.6 Redirections, gnu.org.
- Writing a pseudo-device driver on Linux, lyngvaer.no.