/dev/tcp listen instead of nc listen
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.
If Perl is installed (as it will be on a RHEL machine):
perl -MIO::Socket::INET -ne 'BEGIN{$l=IO::Socket::INET->new(
LocalPort=>1234,Proto=>"tcp",Listen=>5,ReuseAddr=>1);
$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