Controlling a remote Linux PC via Xinetd

For months I have been trying to send commands to my Fedora Linux PC from my RPi4 HA controller with limited success. I was wanting voice announcements to come through this PC which is connected to a whole house audio system, mostly for motion detection at first, but there are many other potential uses for this as well.

The other day I just happened to stumbled on a totally unrelated subject that tied right into what I needed to do. Someone was needing to check the status of a database and was suggested to use Xinetd service with a script. I thought this could be quite useful for my project, so I made it happen. It was quite easy to do and is extremely reliable so far.

Linux PC Side

Requirments: A working Linux install. Xinetd and telnet installed.

  • First, I created the entries in /etc/xinetd.d/ which includes drivewaymotion, eveninglightson/off and hallwaymotion. These entries look like this:
service drivewaymotion 
{
    disable         = no
    port            = 1234
    socket_type     = stream
    protocol        = tcp
    wait            = no
    user            = root
    server          = /home/username/bin/drivewaymotion-sfx.sh
    instances       = 1
    type            = unlisted
    log_type        = FILE /home/username/homeautomation/drivewaymotion.log
    log_on_success  += PID HOST DURATION
    log_on_failure  = HOST
}

For each, you will need to change the service name, port, server (script) and the logging destination (optional).

service hallwaymotion 
{
    disable         = no
    port            = 1237
    socket_type     = stream
    protocol        = tcp
    wait            = no
    user            = root
    server          = /home/username/bin/hallwaymotion-sfx.sh
    instances       = 1
    type            = unlisted
    log_type        = FILE /home/username/homeautomation/hallwaymotion.log
    log_on_success  += PID HOST DURATION
    log_on_failure  = HOST
}

Since my Linux PC uses Pipewire for audio, you have to make sure these run as the user your pipewire is running under. If you use root, it will not work, unless that is how you login which would be bad.

Here is what the driveway trigger script looks like:

#!/bin/sh

# This goes in /home/username/bin/drivewaymotion-sfx.sh

su -c "aplay /home/username/Documents/HomeAutomationSfx/doorbell2.wav" username
su -c "aplay /home/username/Documents/HomeAutomationSfx/driveway.wav" username

Note that I am using su -c to ensure it runs aplay as the correct user. The username is at the end of the command. Also, the PATHs to your .wav files can be anywhere you like. This is just where I chose to put mine.

Your scripts MUST BE EXECUTABLE! Just do a chmod 755 on them and you should be fine. Also the top of the script contains #!/bin/sh. This also MUST be present or it will not work!

Next, you will need to create your audio samples for playback. I use the Piper project for this.

Creating the samples is as easy as typing it in. It creates the .wav file for you.

echo "There is motion detected in the hallway.." | ./piper --model voices/en_US-amy-medium.onnx --output_file hallwaymotion.wav

Piper creates a very natural sounding voice. Of course you could just record your own voice as well but this is much easier imo.

You may put your scripts and samples wherever you like. Just be sure to use the correct paths. Also, to test this, simply open a local shell on your Linux PC and type #telnet localhost port. You should now hear your sample play through the speakers…instantly!

Now, onto the Home Assistant work:

Home Assistant Side
You need to add shell commands to your configuration.yaml file:

shell_command:
  script_evening_lights_on: curl telnet://192.168.254.15:1234
  script_driveway_motion: curl telnet://192.168.254.15:1235
  script_eveninglights_off: curl telnet://192.168.254.15:1236
  script_hallway_motion: curl telnet://192.168.254.15:1237

Since HAOS does not include telnet, you have to use curl with the telnet://url command.
Reload your HA and the scripts should show up for use in your automations.

Conclusion:
You may have to open these ports on your Linux firewall. Easy to do. Just search for that if you need it. You can also just disable it for testing as long as it’s behind a secure router. ie. systemctl stop firewalld

Your remote Linux PC could be of course, another Raspberry Pi, or practically anything running Linux for that matter.

Hope somebody finds this as useful as I have. You could also use this to control volume levels, run apps, stop apps, and practically anything else. I am going to add one soon to start video recording on motion detection.