Use socat to connect to the socket, send a command and read one line

I have a process that can be contacted through a UNIX socket (actually an abstract socket , which I think depends on Linux).

I am writing a shell script that should send commands to this process. I decided to try using SOCAT for this purpose. I'm trying to figure out how (1) to connect to an abstract socket, (2) send it one command (line), (3) read the answer and print the answer to stdout.

Until now, I could easily connect simply by saying:

socat ABSTRACT-CONNECT:mysocket STDIN 

This connects the STDIN to the socket, so if I know that manually enter the command, the socket will be read in the line, and then output the resulting response to the console.

Except that I run socat in a script, I need to automate sending a string to STDIN. Ok, so the easiest way to do this seemed to be to simply pass the string to STDIN. So suppose we have a team called " status ", I would say:

 echo status | socat ABSTRACT-CONNECT:mysocket STDIN 

This seems to work (socat doesn't report errors), except that the result is not sent to stdout. Rather, the call simply returns unanswered. So, how can I also read one line from a socket and map the result to stdout?

+5
source share
1 answer

use the address type "STDIO" for the second argument, this is a bi-directional address. it reads input from the console and sends to the opposite end, and also receives data from the opposite end and writes to the console output. good to mention - aka. dash - shortcut for stdio.

+2
source

Source: https://habr.com/ru/post/1206529/


All Articles