Skip to content
Advertisement

Output a linux command to a url/port or scocket instead of writing it to a file

I have a command which out outputs certain data which i store in a ext file using a ‘>>’ command.Now Instead of doing that I want to have a socket or a port on any server which will catch the output of the command.Basically i want to output all my script data to a socket or url which ever is possible.

Any help in this direction is most welcomed.

Advertisement

Answer

You can use socat to listening on a port 12345 and echo any data sent to it like this:

socat -u TCP-LISTEN:12345,keepalive,reuseaddr,fork STDOUT

If you want to capture it to a file as well (file.log), you can use the same command with tee:

socat -u TCP-LISTEN:12345,keepalive,reuseaddr,fork STDOUT | tee file.log

You can run your program to output to bash’s TCP virtual device:

./prog > /dev/tcp/localhost/12345

If you don’t want to use bash magic then you can also use socat to send the data:

./prog | socat - TCP-CONNECT:localhost:12345

The above example assume you are running your program and “logger” on the same system but you can replace “localhost” with the hostname or address of the system you wish to send to (where the socat is listening).

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement