Simple Socket Server in Bash?

LinuxBashSocketsTcp

Linux Problem Overview


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?

Linux Solutions


Solution 1 - Linux

$ nc -k -l 4444 > filename.out

see nc(1)

Solution 2 - Linux

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

Solution 3 - Linux

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.

Solution 4 - Linux

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNaftuli KayView Question on Stackoverflow
Solution 1 - LinuxNikolai FetissovView Answer on Stackoverflow
Solution 2 - LinuxDiego Torres MilanoView Answer on Stackoverflow
Solution 3 - LinuxlukView Answer on Stackoverflow
Solution 4 - LinuxKilokahnView Answer on Stackoverflow