Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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

2 Answers 2

up vote 6 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 && ztcp -ad4 3 && cat < ~/.bashrc >&4 && ztcp -c 4
share|improve this answer

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

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.