Sending TCP packets to a device

doest it work friom ssh/putty , thats the real test

I tried these settings but i get “network error:connection refused”

image

putty is in port 22 , also enable SSH service first

Have you tried a return at the end? x0d

Did you get this working @Alex_Yeoman? I was looking at a NoHassleAV 8x8 matrix (https://www.amazon.co.uk/HDCP2-2-Selector-Crestron-Control4-Automation/dp/B01GKFQNG8) but ended up importing a J-Tech digital unit (all appear to be the same rebadged units - https://www.jtechdigital.com/j-tech-digital-8x8-hdmi-matrix-switcher-4k-60hz-ultra-hd-8-displays-hdmi-2-0-supports-hdcp-2-2-1-4-edid-dts-dolby-hd.html) from the US, so I’m about to start configuring this.

@Alex_Yeoman if you see connection refused, it’s possible you already have something using that port ?

If you set something up to make a call, unless you set a time out it might stay locked open to that connection. Make sure you have nothing else running and restart your matrix and see what happens

If you can, share a link to the device’s documentation and the ASCII or HEX commands you need to use .

I tried the commands from the config code directly on the command line and didn’t work, but then I realised you have curly quotes. Changed to straight quotes and it switches (and I see a response).

Nope:

echo -e “\x40\x54\x30\x32\x30\x33\x23” | nc 192.168.1.168 5000

Yep (in this example switches output 3 to input 4):

echo -e "\x40\x54\x30\x32\x30\x33\x23" | nc 192.168.1.168 5000
@ 09 01 #30x32x30x33x23 ”
x21”

Edit - this works for me:

switch:
  - platform: command_line
    switches:
      hdmi_matrix:
        command_on: 'echo -e "\x40\x54\x30\x32\x30\x33\x23" | nc 192.168.1.168 5000'
        command_off: 'echo -e "\x40\x54\x30\x32\x30\x33\x23" | nc 192.168.1.168 5000'
        friendly_name: HDMI Matrix

Is there any setup for getting this to work in a hass.io install? Anything that needs to be enabled?

- platform: command_line
  switches:
    projector:
      command_on: 'echo -e "PWR ON\r" | nc 192.168.1.33 4999'
      command_off: 'echo -e "PWR OFF\r" | nc 192.168.1.33 4999'
      friendly_name: Projector

This does not work for me, but writing echo -e “PWR ON\r” | nc 192.168.1.33 4999 directly in the terminal at the ubuntu machine where HA/hass is installed works fine.

no problem for me, but my quoting is different in my setup, also running hassio on hassos

sc_living:
  friendly_name: Living
  command_on: echo -e "\xED\x43\x31\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xAF\xAF\x43\x07\x01" | nc 192.168.0.10 1001
  command_off: echo -e "\xED\x43\x31\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xAF\xAF\x43\x07\x00" | nc 192.168.0.10 1001

Weird, I tried bot with and without the outer quotes, didn’t make a difference.

Jumping in for some help here,

my Sharp information display seems to REQUIRE a username and password, ive tried setting them to blank but it still asks for longin.

Ive tried writing a script to login like so:

#!/bin/bash
echo "admin" | nc 172.16.0.53 10008
echo "admin" | nc 172.16.0.53 10008
echo "MUTE   1" | nc 172.16.0.53 10008

but it doesnt work for me.

Similarly, using the TCP sensor I get:

If I manually open a connection using Putty and login I can then send commands fine…

That’s 3 different TCP connections that you’re opening to 172.16.0.53, each of which sends a single line and closes the connection.

Maybe something like

(echo "admin"; echo "admin"; echo "MUTE 1") | nc 172.16.0.53 10008

which sends 3 lines into a single connection…

There are other tools like expect that allow you to script interactive sessions programmatically if you need to wait for prompts to appear, etc.

lmartin@ubuntu:~$ (echo "admin"; echo "admin"; echo "MUTE 1") | nc 172.16.0.53 10008
Login:
Password:

It was a good attempt but no dice :frowning:

is there a way I can put delays into the command? giving it time to prompt for login / pass before sending it ?

sorry this is all new to me

I dunno, maybe try:

(echo "admin"; sleep 1; echo "admin"; sleep 1; echo "MUTE 1") | nc 172.16.0.53 10008

At some point, you probably need some other tool or small program to emulate an interactive session, rather than blindly shouting down a connection…

or just launch a .py file , easier to send messages then

I had problems to send the netcat commands from haasio as well…

I ended up to make a ssh loop and it worked. Perhaps this helps someone…

ssh -o StrictHostKeyChecking=no -i /config/ssh/id_rsa root@localhost ‘echo 38ff0000aa83 | xxd -r -p | nc 192.168.10.50 8189’

Thats my command.

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.