How To: Send TCP packet in Reaction
-
Someone had posted earlier a question about how to send TCP packets from Reactor, and then deleted the post, but I thought the question was interesting. It's not something Reactor can do directly at this time (that is, there's no action specifically for this purpose), but I had a hunch how it would be doable through the shell, and it took me a bit before I could get to my desk/lab to try it out.
Pretty straightforward on Linux-based systems, at least:
This uses a Shell Command action to drive
nc
to send the packet. Theecho
command is used to pipe the string to be transmitted tonc
, which makes the connection and delivers the data, waits one second, then disconnects.This test case has some particulars for communication with Yeelight bulbs. You may need to adjust the command line arguments to suit the intended target if it's something else. Specifically:
- The
echo
command here is used without-n
so that a LF is sent after the string; if no line ending is needed for your target, useecho -n
. - The
-C
option is used onnc
to translateecho
's LF to CRLF, which Yeelight requires. Drop this option if your target has no such requirement.
- The
-
I was able to do it pretty simply in Node-Red, so I set it up there and deleted the question. I put an http listener on the NR flow and call that URL from MSR when I want to trigger the TCP connection. I'll give this solution a try, as having it all in one place seems like a good idea.
EDIT:
So I tried and am getting this. I'm working on it, but you'll probably post a solution before I can figure it out. I'm running MSR in Docker on a RPi4.
Rule<SET>" (rule-kvmgurr3:S) step 3 Shell action failed command "echo '?\"id\?' | nc -C -q 1 192.168.0.25 55443" (1) nc: unrecognized option: C BusyBox v1.31.1 () multi-call binary. Usage: nc [OPTIONS] HOST PORT - connect nc [OPTIONS] -l -p PORT [HOST] [PORT] - listen -e PROG Run PROG after connect (must be last) -l Listen mode, for inbound connects -lk With -e, provides persistent server -p PORT Local port -s ADDR Local address -w SEC Timeout for connects and final net reads -i SEC Delay interval for lines sent -n Don't do DNS resolution -u UDP mode -v Verbose -o FILE Hex dump traffic -z Zero-I/O mode (scanning)
Edit 2:
I used my usual brute force method of Googling various terms and mashing the keyboard (aka trial and error by a Linux novice) and finally made this work on my RPi4 Docker install of MSR.The '-C' didn't work, but I was able to add $'\r\n' to the end of the echo command which I believe achieved the same effect.
The '-q 1' didn't work, but I was able to use '-w 1' which again I believe has the same effect.
So I end up with a shell command of:
echo '{"id":1,"method":"set_power","params":["on", "smooth", 250]}'$'\r\n' | nc -w 1 192.168.0.25 55443
P.S. - I read the 'category topic guide' between my first reply and this edit and realize that I'm probably violating at least one of those rules. I got a good laugh from this part of that post: " I wrote Reactor as a tool for you to use to solve your automation/logic problems, not as a tool for me to use to solve your automation/logic problems." Hopefully I'm living up to that part a little bit by figuring this out for myself (with a initial help from Patrick).
-
T toggledbits locked this topic on