Bash Socket Programming

Standard

From: http://hacktux.com/bash/socket


You can connect to a socket using Bash by using exec and redirecting to and from the pseudo-path /dev/tcp/<hostname>/<port> or /dev/udp/<hostname>/<port>. For instance, to connect to your localhost SSH port using TCP:

Then, use cat and echo to read or write to the socket. Here is an example read:

Notice that there is no such file as /dev/tcp or /dev/udp. Bash interprets the pseudo-path.

As another example, maybe you want to download a webpage:

Finally, let’s say you wanted to connect to an IRC server. Here is an example:

Sources

tldp.org: Advanced Bash-Scripting Guide – Chapter 29
thesmithfam.org: Bash socket programming with /dev/tcp


565 total views, no views today