Is there a way to quickly bind to a TCP port/ip address and simply print out all information to STDOUT? I have a simple debugging solution which writes things to 127.0.0.1:4444 and I'd like to be able to simply bind up a port from bash and print everything that comes across. Is there an easy way to do this?
Asked
Active
Viewed 1.2e+01k times
93
-
related http://unix.stackexchange.com/questions/49936/dev-tcp-listen-instead-of-nc-listen – Ciro Santilli OurBigBook.com Sep 02 '16 at 22:20
4 Answers
130
$ nc -k -l 4444 > filename.out
see nc(1)

Nikolai Fetissov
- 82,306
- 11
- 110
- 171
-
4Any way to do it that doesn't require it to run in a loop, ie bind until killed? I'm repeatedly connecting and disconnecting to the socket and `nc` dies if I don't run it like this: `while true; do nc -l 4444; done`. – Naftuli Kay Jan 19 '11 at 18:43
-
On some distros you'll need to change it to 'nc -k -l -p 4444'. – Rostislav Matl Apr 09 '15 at 11:38
-
2If you use ncat instead of nc, you can have multiple concurrent connections while using the exact same syntax. – Sietse van der Molen Sep 14 '15 at 02:56
-
2yes, use `ncat` instead of `nc` (it comes bundled with `nmap` and it's a modern day incarnation of `nc`) – Freedom_Ben Jul 07 '16 at 18:06
62
Just because you asked how to do it in bash
, though netcat
answer is very valid:
$ exec 3<>/dev/tcp/127.0.0.1/4444
$ cat <&3

Diego Torres Milano
- 65,697
- 9
- 111
- 134
-
13But that doesn't work for listening. I don't think its possible to listen using strictly bash – Vijayender Sep 05 '12 at 11:35
-
8This solution indeed requires a listening server. Bash cannot do this by means of `/dev/tcp` as explained in http://unix.stackexchange.com/a/49947/13746 – xebeche Jun 12 '13 at 21:07
-
2
-
18
That is working as you expecting:
nc -k -l 4444 |bash
and then you
echo "ls" >/dev/tcp/127.0.0.1/4444
then you see the listing performed by bash.
[A Brief Security Warning]
Of course if you leave a thing like this running on your computer, you have a wide open gateway for all kinds of attacks because commands can be sent from any user account on any host in your network. This implements no security (authentication, identification) whatsoever and sends all transmitted commands unencrypted over the network, so it can very easily be abused.
-
2If you don't have `/dev/tcp`, you can run: `echo "ls" | nc 127.0.0.1 4444` – fzbd Jul 04 '18 at 20:15
-
1@fzbd: there is no /dev/tcp. this is special bash file, so ``ls`` will not show this – Marcin Fabrykowski Oct 11 '18 at 14:32
-
@MarcinFabrykowski Correct, but if you run these commands on other shells, there is no handler available. It can also fail if your bash isn't compiled with `--enable-net-redirections`. – fzbd Oct 12 '18 at 08:02
9
Adding an answer using ncat
that @Freedom_Ben alluded to:
ncat -k -l 127.0.0.1 4444
and explanation of options from man ncat:
-k, --keep-open Accept multiple connections in listen mode
-l, --listen Bind and listen for incoming connections

Kilokahn
- 2,281
- 1
- 25
- 50
-
`ncat` comes with `nmap` and supports concurrent connections, while the legacy `nc` command does not. – Serge Stroobandt Jan 12 '18 at 17:32