Sending TCP packets to a device

I came to this thread while trying to send hex commands over TCP to a board containing an ESP, a microcontroller and relays. I fought several hours with it, debugging, so I thought what follows might save some time for others: For some reason, echo -e “\xA0” pipe netcat… was actually sending the ASCII string. Changing to simple quotes solved the problem miraculously. echo -e ‘\xA0’ pipe netcat…

Not that miraculous, you need to do some reading on quoting in a shell. Most of which I have forgotten as I don’t do much shell scripting now. However I do recall there is a distinct different between single and double quotes.

IIRC this covers it http://www.tldp.org/LDP/abs/html/quoting.html

i finally found a working solution for this problem on HASSIO (or the new HA)
to send raw ip packet data to any device in your network all you need to do is the following:
what you need to do is download a static binary for netcat from this location:
https://github.com/therealsaumil/static-arm-bins/blob/f46fded7181694862fec4d3a39ce41059e494a01/nc-arm-static
then copy that file to /config/ and rename it to “nc”
then you can call netcat from the HA container.

example:
in my case i created a shell_command in my config file which calls two *.sh scripts
one script for turning the fireplace on and one for turning it off.
the on script sends a byte sequence to an ip address and a port with netcat.
you just have to specify the path to the nc file in the *.sh script.

example: echo -e “\x02\x33\x30\x33\x30\x33\x30\x33\x30\x38\x30\x31\x41\x03” | /config/nc 192.168.1.XXX 2000
/bin/bash

all you have to do then is make a boolean switch and make setting it to “on” call the fireplace_on.sh script and setting it to “off” make it call the fireplace_off.sh script.

2 Likes

I’m really struggling with my integration of a Sonance 6 zone whole home audio amp. I currently control it with a phone app called Irule and a global cache ip2sl device that takes TCP/IP commands and sends serial commands to the Sonance. I can use Putty to connect to the ip2sl and send commands that are recognized. I’ve tried the suggestion shown here with various tweaks but no luck.

I’d like to have a button card with 4 buttons for each zone: power on, off, vol+, and vol-. I have the commands and with a putty telnet connection I can send :Z21 as the zone 2 on command without issue. The itach comes with testing software and I can successfully send the command through that with various formats depending on the settings in that software of raw, mixed or hex. :Z21\x0D hex 3a 5a 32 31 0d or \x3a\x5a\x32\x31\x0d. With telnet only :Z21 works, carriage return is likely provided by the pressing enter.

I’ve tried a button with code to test:

type: ‘custom:button-card’
name: zone2_on
tap_action:
action: call-service
service: shell_command.zone2_on

and this as the command:

shell_command:
zone2_on: echo -e “\x3a\x5a\x32\x31\x0d” | nc 192.168.1.114 4999

I’ve tinkered with what is in the quotes, trying all combinations of things I’m able to send via Putty telnet session and the testing software but am not getting a result.

Any help would be much appreciated.

Maybe it’s because of the line endings - echo normally adds a LF (linefeed) character to the end of a string.
Depending on what your device expects, you can try

echo -ne "..."

(where the -n supresses any newline characters) and add a "\n" (<LF>) or "\r\n"(<CR><LF>) manually - depending on what your device is looking for (this should be in the protocol specifications).

For debugging, you could point the IP address to another system, run nc in server mode there (-l <port> option) and pipe that into a tool like e.g. hexdump just to see if your command a) works at all and b) is sending the exact string you’re expecting.

Sebastian

1 Like

https://community.home-assistant.io/t/possible-to-have-a-button-send-custom-rs232-commands/259522/4?u=docsparks binbash and proper quoting was my save all, best of luck.

Thank you very much for sharing this! I’ve been stuck with trying to send netcat commands from ssh on the new Blue box. Thanks for sharing your fix!

Hi to all!
I was wondering how to make a TCP Call to my SR201 Web realy bougth on Bang Good, and found this article very helpful.

Thanks to all because I managed to create a switch reading around this article in minutes.

Now I would like to ask one question:

  • I use HOMEKIT to monitor devices with my phone, the SWITCH appears n the devices shown in homekit, but when I press the switch (it works), but it remain on, even if the switch turns off by itself after 1 second.

the code is this

switch:     
- platform: command_line
  scan_interval: 10
  switches:
    apri_cancello:
      command_on: echo -n "12*" | nc 192.168.20.14 6722
      command_state: echo -n 00 | nc -q 1 192.168.20.14 6722
      value_template: "{{ value[0:1] == '1' }}"
      friendly_name: "Apri Cancello"
      icon_template: mdi:gate

NOTE: I do not use command_state_off because the switch turns on and off by itself (like a push button or a switch with inching mode on). The icon on homeassistant behave well, it turns on and off after 1 second, but in homekit the switch keep to stay pressed (on) i need to push it again.

I’m trying to send commands to my alarm but it’s not working through the home assistant.

If I send through a VM with ubuntu the alarm gets the command, but using shell command by home assistant doesn’t work.

ubuntu:

echo -e "\x08\xe9\x21\x31\x32\x33\x34\x44\x21\x5e" | nc 192.168.0.15 9009

HA:

amt_ativar: echo -e "\x08\xe9\x21\x31\x32\x33\x34\x41\x21\x5b" | nc 192.168.0.15 9009

Any idea?

Hard to say without logs.

There’s nothing in the logs. :thinking:

Full logs
2022-09-03 11:14:34.435 WARNING (SyncWorker_0) [homeassistant.loader] We found a custom integration hacs which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you experience issues with Home Assistant
2022-09-03 11:14:34.438 WARNING (SyncWorker_0) [homeassistant.loader] We found a custom integration rpi_gpio which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you experience issues with Home Assistant
2022-09-03 11:14:34.439 WARNING (SyncWorker_0) [homeassistant.loader] We found a custom integration dht which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you experience issues with Home Assistant
2022-09-03 11:14:34.441 WARNING (SyncWorker_0) [homeassistant.loader] We found a custom integration localtuya which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you experience issues with Home Assistant
2022-09-03 11:14:34.443 WARNING (SyncWorker_0) [homeassistant.loader] We found a custom integration next_holiday which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you experience issues with Home Assistant
2022-09-03 11:14:34.445 WARNING (SyncWorker_0) [homeassistant.loader] We found a custom integration duckdns_ipv4_ipv6 which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you experience issues with Home Assistant
2022-09-03 11:14:34.447 WARNING (SyncWorker_0) [homeassistant.loader] We found a custom integration arpscan_tracker which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you experience issues with Home Assistant
2022-09-03 11:14:45.921 WARNING (MainThread) [homeassistant.components.number] custom_components.localtuya.number::LocaltuyaNumber is overriding deprecated methods on an instance of NumberEntity, this is not valid and will be unsupported from Home Assistant 2022.10. Please report it to the custom integration author.
22-09-03 11:49:57 WARNING (MainThread) [supervisor.addons.options] Option 'cnames' does not exist in the schema for Mosquitto broker (core_mosquitto)
22-09-03 11:51:04 INFO (MainThread) [supervisor.homeassistant.api] Updated Home Assistant API token
22-09-03 11:54:59 WARNING (MainThread) [supervisor.addons.options] Option 'cnames' does not exist in the schema for Mosquitto broker (core_mosquitto)
22-09-03 12:00:01 WARNING (MainThread) [supervisor.addons.options] Option 'cnames' does not exist in the schema for Mosquitto broker (core_mosquitto)
22-09-03 12:05:03 WARNING (MainThread) [supervisor.addons.options] Option 'cnames' does not exist in the schema for Mosquitto broker (core_mosquitto)
22-09-03 12:10:05 WARNING (MainThread) [supervisor.addons.options] Option 'cnames' does not exist in the schema for Mosquitto broker (core_mosquitto)
22-09-03 12:15:07 WARNING (MainThread) [supervisor.addons.options] Option 'cnames' does not exist in the schema for Mosquitto broker (core_mosquitto)
22-09-03 12:19:59 INFO (MainThread) [supervisor.resolution.check] Starting system checks with state CoreState.RUNNING
22-09-03 12:19:59 INFO (MainThread) [supervisor.resolution.checks.base] Run check for IssueType.NETWORK_CONNECTION_PROBLEM/ContextType.SYSTEM
22-09-03 12:19:59 INFO (MainThread) [supervisor.resolution.checks.base] Run check for IssueType.DNS_SERVER_IPV6_ERROR/ContextType.DNS_SERVER
22-09-03 12:19:59 INFO (MainThread) [supervisor.resolution.checks.base] Run check for IssueType.PWNED/ContextType.ADDON
22-09-03 12:19:59 INFO (MainThread) [supervisor.resolution.checks.base] Run check for IssueType.FREE_SPACE/ContextType.SYSTEM
22-09-03 12:19:59 INFO (MainThread) [supervisor.resolution.checks.base] Run check for IssueType.SECURITY/ContextType.CORE
22-09-03 12:19:59 INFO (MainThread) [supervisor.resolution.checks.base] Run check for IssueType.DNS_SERVER_FAILED/ContextType.DNS_SERVER
22-09-03 12:19:59 INFO (MainThread) [supervisor.resolution.checks.base] Run check for IssueType.TRUST/ContextType.SUPERVISOR
22-09-03 12:19:59 INFO (MainThread) [supervisor.resolution.check] System checks complete
22-09-03 12:19:59 INFO (MainThread) [supervisor.resolution.evaluate] Starting system evaluation with state CoreState.RUNNING
22-09-03 12:20:00 INFO (MainThread) [supervisor.resolution.evaluate] System evaluation complete
22-09-03 12:20:00 INFO (MainThread) [supervisor.resolution.fixup] Starting system autofix at state CoreState.RUNNING
22-09-03 12:20:00 INFO (MainThread) [supervisor.resolution.fixup] System autofix complete
22-09-03 12:20:09 WARNING (MainThread) [supervisor.addons.options] Option 'cnames' does not exist in the schema for Mosquitto broker (core_mosquitto)
22-09-03 12:21:00 INFO (MainThread) [supervisor.updater] Fetching update data from https://version.home-assistant.io/stable.json
22-09-03 12:21:05 INFO (MainThread) [supervisor.homeassistant.api] Updated Home Assistant API token
22-09-03 12:25:11 WARNING (MainThread) [supervisor.addons.options] Option 'cnames' does not exist in the schema for Mosquitto broker (core_mosquitto)
22-09-03 12:30:13 WARNING (MainThread) [supervisor.addons.options] Option 'cnames' does not exist in the schema for Mosquitto broker (core_mosquitto)
22-09-03 12:34:21 INFO (MainThread) [supervisor.host.info] Updating local host information
22-09-03 12:34:22 INFO (MainThread) [supervisor.host.services] Updating service information
22-09-03 12:34:22 INFO (MainThread) [supervisor.host.network] Updating local network information
22-09-03 12:34:23 INFO (MainThread) [supervisor.host.sound] Updating PulseAudio information
22-09-03 12:34:23 INFO (MainThread) [supervisor.host.manager] Host information reload completed
22-09-03 12:35:15 WARNING (MainThread) [supervisor.addons.options] Option 'cnames' does not exist in the schema for Mosquitto broker (core_mosquitto)
22-09-03 12:40:17 WARNING (MainThread) [supervisor.addons.options] Option 'cnames' does not exist in the schema for Mosquitto broker (core_mosquitto)
22-09-03 12:45:19 WARNING (MainThread) [supervisor.addons.options] Option 'cnames' does not exist in the schema for Mosquitto broker (core_mosquitto)
22-09-03 12:50:21 WARNING (MainThread) [supervisor.addons.options] Option 'cnames' does not exist in the schema for Mosquitto broker (core_mosquitto)
22-09-03 12:51:05 INFO (MainThread) [supervisor.homeassistant.api] Updated Home Assistant API token
22-09-03 12:55:23 WARNING (MainThread) [supervisor.addons.options] Option 'cnames' does not exist in the schema for Mosquitto broker (core_mosquitto)
22-09-03 13:00:25 WARNING (MainThread) [supervisor.addons.options] Option 'cnames' does not exist in the schema for Mosquitto broker (core_mosquitto)
22-09-03 13:05:27 WARNING (MainThread) [supervisor.addons.options] Option 'cnames' does not exist in the schema for Mosquitto broker (core_mosquitto)
22-09-03 13:10:29 WARNING (MainThread) [supervisor.addons.options] Option 'cnames' does not exist in the schema for Mosquitto broker (core_mosquitto)
22-09-03 13:15:31 WARNING (MainThread) [supervisor.addons.options] Option 'cnames' does not exist in the schema for Mosquitto broker (core_mosquitto)
22-09-03 13:20:00 INFO (MainThread) [supervisor.resolution.check] Starting system checks with state CoreState.RUNNING
22-09-03 13:20:00 INFO (MainThread) [supervisor.resolution.checks.base] Run check for IssueType.NETWORK_CONNECTION_PROBLEM/ContextType.SYSTEM
22-09-03 13:20:00 INFO (MainThread) [supervisor.resolution.checks.base] Run check for IssueType.DNS_SERVER_IPV6_ERROR/ContextType.DNS_SERVER
22-09-03 13:20:00 INFO (MainThread) [supervisor.resolution.checks.base] Run check for IssueType.PWNED/ContextType.ADDON
22-09-03 13:20:00 INFO (MainThread) [supervisor.resolution.checks.base] Run check for IssueType.FREE_SPACE/ContextType.SYSTEM
22-09-03 13:20:00 INFO (MainThread) [supervisor.resolution.checks.base] Run check for IssueType.SECURITY/ContextType.CORE
22-09-03 13:20:00 INFO (MainThread) [supervisor.resolution.checks.base] Run check for IssueType.DNS_SERVER_FAILED/ContextType.DNS_SERVER
22-09-03 13:20:00 INFO (MainThread) [supervisor.resolution.checks.base] Run check for IssueType.TRUST/ContextType.SUPERVISOR
22-09-03 13:20:00 INFO (MainThread) [supervisor.resolution.check] System checks complete
22-09-03 13:20:00 INFO (MainThread) [supervisor.resolution.evaluate] Starting system evaluation with state CoreState.RUNNING
22-09-03 13:20:00 INFO (MainThread) [supervisor.resolution.evaluate] System evaluation complete
22-09-03 13:20:00 INFO (MainThread) [supervisor.resolution.fixup] Starting system autofix at state CoreState.RUNNING
22-09-03 13:20:00 INFO (MainThread) [supervisor.resolution.fixup] System autofix complete
22-09-03 13:20:33 WARNING (MainThread) [supervisor.addons.options] Option 'cnames' does not exist in the schema for Mosquitto broker (core_mosquitto)
22-09-03 13:21:06 INFO (MainThread) [supervisor.homeassistant.api] Updated Home Assistant API token
22-09-03 13:25:35 WARNING (MainThread) [supervisor.addons.options] Option 'cnames' does not exist in the schema for Mosquitto broker (core_mosquitto)
22-09-03 13:30:37 WARNING (MainThread) [supervisor.addons.options] Option 'cnames' does not exist in the schema for Mosquitto broker (core_mosquitto)
22-09-03 13:31:45 INFO (MainThread) [supervisor.api.middleware.security] /network/info access from core_ssh
22-09-03 13:31:45 INFO (MainThread) [supervisor.api.middleware.security] /network/info access from core_ssh
22-09-03 13:31:45 INFO (MainThread) [supervisor.api.middleware.security] /host/info access from core_ssh
22-09-03 13:31:45 INFO (MainThread) [supervisor.api.middleware.security] /core/info access from core_ssh
22-09-03 13:33:10 INFO (MainThread) [supervisor.api.proxy] Home Assistant WebSocket API request initialize
22-09-03 13:33:11 INFO (MainThread) [supervisor.api.proxy] WebSocket access from a0d7b954_vscode
22-09-03 13:33:11 INFO (MainThread) [supervisor.api.proxy] Home Assistant WebSocket API request running
22-09-03 13:35:39 WARNING (MainThread) [supervisor.addons.options] Option 'cnames' does not exist in the schema for Mosquitto broker (core_mosquitto)
22-09-03 13:36:24 INFO (MainThread) [supervisor.api.proxy] Home Assistant WebSocket API connection is closed
22-09-03 13:40:28 INFO (MainThread) [supervisor.api.proxy] Home Assistant WebSocket API request initialize
22-09-03 13:40:29 INFO (MainThread) [supervisor.api.proxy] WebSocket access from a0d7b954_vscode
22-09-03 13:40:29 INFO (MainThread) [supervisor.api.proxy] Home Assistant WebSocket API request running
22-09-03 13:40:41 WARNING (MainThread) [supervisor.addons.options] Option 'cnames' does not exist in the schema for Mosquitto broker (core_mosquitto)
22-09-03 13:45:43 WARNING (MainThread) [supervisor.addons.options] Option 'cnames' does not exist in the schema for Mosquitto broker (core_mosquitto)
22-09-03 13:50:45 WARNING (MainThread) [supervisor.addons.options] Option 'cnames' does not exist in the schema for Mosquitto broker (core_mosquitto)
22-09-03 13:51:06 INFO (MainThread) [supervisor.homeassistant.api] Updated Home Assistant API token
22-09-03 13:55:47 WARNING (MainThread) [supervisor.addons.options] Option 'cnames' does not exist in the schema for Mosquitto broker (core_mosquitto)

If I use -v in Terminal & SSH it returns OPEN and the command is not executed

echo -e "\x0a\xe9\x21\x31\x32\x33\x34\x35\x36\x41\x21\x5a" | nc -v -w 2 192.168.0.15 9009

Can you give some more context to this?

You mean about the code?

\x08\ Number of bytes to be sent
xe9\ Command (as they call it in the documentation)
x21\ Frame limiter (as they call it in the documentation)
x31\x32\x33\x34\ password
x44\ Deactivation code
x21\Frame limiter (as they call it in the documentation)
x5e Checksun

I was trying to use the TCP sensor to communicate with the alarm, but I couldn’t.
I don’t know if it was in the cache, but I deleted it from the yaml file and it kept showing up in the UI.
Only after several restarts it disappeared.
I thought it was blocking the port, but apparently it’s not.

This was the code I used for the sensor

- platform: tcp
  name: TESTING_TCP
  host: 192.168.0.15
  port: 9009
  payload: "\x08\xe9\x21\x31\x32\x33\x34\x5b\x21\x41"

I completely turned off the alarm and raspberry, still nothing.

If I open wireshark I can see the communication when I use ubuntu.
But nothing about HA

I managed to solve the problem with the shell command with @Luuk_Neele tip.

Any way to use the tcp sensor and get the response?

- platform: tcp
  name: AMT 4010 TCP
  host: 192.168.0.15
  port: 9009
  payload: "\x0a\xe9\x21\x36\x38\x36\x31\x37\x33\x5b\x21\x4a"
2022-09-06 15:48:15.277 WARNING (SyncWorker_0) [homeassistant.components.tcp.common] Timeout (10 second(s)) waiting for a response after sending '\né!686173[!J' to 192.168.0.15 on port 9009

Response

37 E9 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 41 52 01 00 00 11 0F 33 06 09 16 00 00 00 00 00 0F 00 01 00 00 02 00 00 00 00 00 00 00 00 0B

Hello,

I have the same problem. Can you tell me how you solve this issue?
I send the below command_line and doesnt do anything, WIth udp which i have worked before the commands works immidiately and if the remote device was out of the network i had error at HA. Now with tcp nothing, I will appreciate if you can help me with this issue. I have managed to send with third party softwares the command from my pc to the remote device

switch:
  - platform: command_line
    switches:
       audac:
        command_on: 'echo  "#|M001|F001|SV01|40|U|\r\n" | nc 192.168.168.202 5001'
        command_off: 'echo "#|M001|F001|SV01|60|U|\r\n" | nc 192.168.168.202 5001'

I ended up using Node Red.
There are TCP and UDP nodes

Thanks. The udp connections works perfectly. Do we know why the tcp commands doesn’t work at all?

1 Like

Find this:

This is a sensor, Not for sending ASCII tcp commands. Thank you very mush for your time anyway!

Try this Send TCP/UDP Packets on Linux using /dev/tcp & /dev/udp Pseudo Devices | Platform Engineer