Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

With a netcat listener like:

nc -l <port> < ~/.bashrc

I can grab my .bashrc on a new machine (doesn't have nc or LDAP) with:

cat < /dev/tcp/<ip>/<port> > ~/.bashrc

My question is: Is there a way to mimic the capabilities of nc -l <port> in my first line with /dev/tcp instead of nc?

The machines I'm working on are extremely hardened lab/sandbox environment RHEL (no ssh, no nc, no LDAP, no yum, I cant install new software, and they are not connected to the internet)

share|improve this question
short of writing a python script to keep the socket open is there an easy way to accomplish this? – h3rrmiller Oct 4 '12 at 18:59
up vote 12 down vote accepted

As perl will be installed.

perl -MIO::Socket::INET -ne 'BEGIN{$l=IO::Socket::INET->new(
  $l=$l->accept}print $l $_' < ~/.bashrc

would work, unless a local firewall doesn't allow incoming connections to 1234.

If socat is installed:

socat -u - tcp-listen:1234,reuseaddr < ~/.bashrc

If zsh is installed:

zmodload zsh/net/tcp
ztcp -ld3 1234 && # start listening socket on fd 3
  ztcp -ad4 3 && # accept connection on fd 4
  ztcp -c 3 && # close the listening socket that is no longer needed
  cat < ~/.bashrc >&4 && # send the data
  ztcp -c 4 # close the communication socket to tell the other end we're finished
share|improve this answer
Shouldn't the last ztcp -c 4 command read 3? Otherwise great info, great tip. – dezza Jul 17 at 11:28
@dezza, see edit. The socket on fd 3 wasn't being closed indeed (though it would have been when the script terminates). We need to close the socket on fd 4 so the other end gets an EOF. – Stéphane Chazelas Jul 17 at 11:36
Thanks for the update ! – dezza Jul 17 at 11:37

Unfortunately it's impossible to do with just bash. /dev/tcp/<ip>/<port> virtual files are implemented in the way that bash tries to connect to the specified <ip>:<port> using connect(2) function. In order to create listening socket, it would have to call bind(2) function.

You can check this by downloading bash sources and looking at it. It is implemented in lib/sh/netopen.c file in _netopen4 function (or _netopen6, which also supports IPv6). This function is used by wrapper function netopen from the same file, which in turns is directly used in file redir.c (redir_special_open function) to implement this virtual redirection.

You have to find some other application that can create listening socket on your machine.

share|improve this answer
+1 the bash allows to create the client socket but the server socket you can use nc or can be implemented with perl or c, in fact server process will loop accepting connections and spawning processes or creating threads or it can accept only one connection by one – Nahuel Fouilleul Oct 4 '12 at 19:36
@NahuelFouilleul: That is not entirely correct. You can process a lot of clients with just one thread/process using so called "asynchronious" or "event driven" network programming (try googling for select() function and how it can be used in network programming). In many cases it's much better (faster) way of accepting a lot of clients. – Krzysztof Adamski Oct 4 '12 at 19:50
Thanks I didn't though of this solution – Nahuel Fouilleul Oct 4 '12 at 20:12
This is what xinetd is for. It does the listening, and spawns your arbitrary process/script for any incoming connections. With it, anything can become a TCP/IP server. – Evi1M4chine Jan 23 at 23:33

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.