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?
Socat allows for bidirectional data transfers from one location to another. The socat utility is a relay for bidirectional data transfers between two independent data channels.
Socat is a flexible, multi-purpose relay tool. Its purpose is to establish a relationship between two data sources, where each data source can be a file, a Unix socket, UDP, TCP, or standard input.
Netcat and Socat allows you to pass simple messages between computers interactively over the network. The below setup will allow both client and server to send data to the other party. It can act like a simple ad-hoc chat program. Socat can talk to Netcat and Netcat can talk to Socat.
Socat is a command line based utility that establishes two bidirectional byte streams and transfers data between them.
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 .
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With