First bad news
Unfortunately, there seems to be no hope of doing this in pure Bash.
Even making
exec 3<> /dev/tcp/<ip>/<port>
does not work because these special files are implemented on top connect()instead bind(). This is obvious if we look at the source.
In Bash 4.2, for example, a function _netopen4()(or _netopen6()for IPv6) is read as follows ( lib/sh/netopen.c):
s = socket(AF_INET, (typ == 't') ? SOCK_STREAM : SOCK_DGRAM, 0);
if (s < 0)
{
sys_error ("socket");
return (-1);
}
if (connect (s, (struct sockaddr *)&sin, sizeof (sin)) < 0)
{
e = errno;
sys_error("connect");
close(s);
errno = e;
return (-1);
}
But
, nc. .
nc -l <port>
localhost:<port>.