A repository for the most elegant and useful UNIX commands. Great commands can be shared, discussed and voted on to provide a comprehensive resource for working from the command-line
Sourced through Scoop.it from: www.commandlinefu.com
Read and write to TCP or UDP sockets with common bash tools
Ever needed to test firewalls but didn’t have netcat, telnet or FTP?
Enter /dev/tcp, your new best friend. /dev/tcp/(hostname)/(port) is a virtual device that bash can use to talk to TCP ports
First, exec sets up a redirect for /dev/tcp/$server/$host to file descriptor 5.
Then, as per some excellent feedback from @flatcap, we launch a redirect from file descriptor 5 to STDOUT (implied by
Finally, when the second cat dies (the connection is closed), we clean up the file descriptor with ‘exec 5>&-‘.
This one-liner opens a connection on a port to a server and lets you read and write to it from the terminal.
replace time.nist.gov with 127.0.0.1
replace 13 with 22
You should see a response from your SSH server:
Typing TEST should net you a Protocol Mismatch:
It can be used for FTP, HTTP, NTP, or netcat listening on a port (makes for a simple chat client!)
Replace /tcp/ with /udp/ to use UDP instead.
Comments (6) | Add to favourites | Report as malicious | Submit alternative | Report as a duplicate