Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 876
Siler Avatar asked Nov 10 '14 01:11

Siler


People also ask

What does socat command do?

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.

What's socat?

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.

Can you connect to socat with netcat?

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.

What is Ubuntu socat?

Socat is a command line based utility that establishes two bidirectional byte streams and transfers data between them.


1 Answers

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 .

like image 86
把友情留在无盐 Avatar answered Oct 27 '22 12:10

把友情留在无盐