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

10k views Asked by At

I have a process which can be communicated with via a UNIX socket (actually an abstract socket which I think is Linux-specific).

I'm writing a shell script which needs to send commands to this process. I decided to try using SOCAT for this purpose. I'm trying to figure out how to (1) connect to the abstract socket, (2) send it a single command (a string), (3) read the response and print the response to stdout.

So far, I can connect easily by simply saying:

socat ABSTRACT-CONNECT:mysocket STDIN

This connects STDIN to the socket, so if I know manually type a command, the socket will read in the string, and then output the resulting response to the console.

Except when I run socat in a script, I need to automate sending a string to STDIN. Okay, so the most straightforward way to do this seemed to be to just pipe a string to STDIN. So suppose we have a command called "status", I would say:

echo status | socat ABSTRACT-CONNECT:mysocket STDIN

This seems to work (socat reports no errors), except the result is not sent to stdout. Rather, the call simply returns with no response. So how can I also read a single line from the socket and display the result on stdout?

1

There are 1 answers

2
把友情留在无盐 On BEST ANSWER

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