Hi All,
Here is a simple guide on how to automatically execute commands within Home Assistant to your ESXi Host.
List of guide and links that I use to make it happen. Credit to them!
-
Guide: Start up and shut down remote linux PC using Home Assistant
-
ESXi keeps prompting for password after adding ssh public key to authorized_keys
For starters, below is the list of my environment setup information:
-
ESXi Host - Intel NUC11PAHi7
-
NAS Storage - Synology DS922+
-
ESXi Version - ESXi-8.0U3-24022510-standard (VMware, Inc.)
-
Home Assistant Version - Home Assistant 2024.8.3
-
Supervisor Version - Supervisor 2024.08.0
-
Operating System Version - Operating System 13.1
-
Frontend Version - Frontend 20240809.0
Prerequisite:
*** Disclaimer ***
Always make a full backup for all the related devices and system which involve in the activities.
You have been warned
ESXi Host
1. Enable SSH on ESXi
- Select the TSM-SSH entry on the list
- Click Actions - > Policy
- Choose Start and stop with host, and the SSH service will activate after every host restart
2. Enable Autostart on ESXi
- Select System
- Click Edit Setting - > Enable (choose Yes)
Home Assistant Instance
1. Install official ‘SSH & Web Terminal’ Add-on
- Untick the ‘Protection mode’
- Start the Add-on after installation
- Open the Web UI
2. Creating SSH key pair
- OPTIONAL - Create a new user for our Home Assistant instance by SSH into ESXi Host
- Start by creating the RSA public and private key pairs
mkdir /config/ssh_keys
ssh-keygen -t rsa -b 4096 -f /config/ssh_keys/id_rsa_homeassistant
- When a password is queried, leave it empty. If fulfilled with password the SSH connection will require both key and password and can’t be used for remote commands by home assistant
- In above lines we first create a directory for our ssh keys, because Home Assistant is run in a docker container and does not have visibility to default /root/.ssh folder at all
- Second line starts the key generation. The ssh key is generated with by default 2048 bits, but should be 4096 bits
3. Next we are going to copy the public key to our remote ESXi Host
cat /config/ssh_keys/id_rsa_homeassistant.pub | ssh -m hmac-sha2-512 -A username@remote_esxi_host_ip 'cat >>/etc/ssh/keys-root/authorized_keys'
- Please change the ‘username’ according to user created for SSH
- Please change the ‘remote_esxi_host_ip’ according to your ESXi Host IP’s
4. Verify that it’s working by SSH’ing into ESXi Host from Home Assistant shell
ssh -m hmac-sha2-512 -A -i /config/ssh_keys/id_rsa_homeassistant username@remote_esxi_host_ip
- If everything is working as expected, no password will be queried
5. We going to create a shell commands which is done through configuration.yaml
shell_command:
turn_off_maya: ssh -m hmac-sha2-512 -A -i /config/ssh_keys/id_rsa -o StrictHostKeyChecking=no username@remote_esxi_host_ip "/sbin/shutdown.sh && /sbin/poweroff"
- Save it, Check and Restart
6. We going to create a scripts which is done through scripts.yaml
alias: General - Turn Off ESXi
sequence:
- repeat:
count: "3"
sequence:
- delay:
hours: 0
minutes: 0
seconds: 7
milliseconds: 0
- service: notify.all_device
data:
title: ٱلسَّلَامُ عَلَيْكُمْ وَرَحْمَةُ ٱللَّٰهِ وَبَرَكَاتُهُ
message: Power outage !!! at {{states('sensor.time')}}
- delay:
hours: 0
minutes: 1
seconds: 0
milliseconds: 0
- service: button.press
data: {}
target:
entity_id: button.kenanga_shutdown
- delay:
hours: 0
minutes: 1
seconds: 0
milliseconds: 0
- service: shell_command.turn_off_esxi
data: {}
mode: single
icon: mdi:laptop-account
- FYI - ‘button.kenanga_shutdown’ is my NAS Storage
7. We going to create an automation which is done through automations.yaml
alias: General - Automate Power
description: ""
trigger:
- platform: state
entity_id:
- switch.group_tasmota
to: unavailable
for:
hours: 0
minutes: 3
seconds: 0
id: main_power_off
condition: []
action:
- choose:
- conditions:
- condition: trigger
id: main_power_off
sequence:
- service: script.turn_on
data: {}
target:
entity_id: script.general_turn_off_esxi
mode: single
- FYI - ‘switch.group_tasmota’ is the group of Sonoff POW R2 powered my appliance and stuff
- FYI - My System Infrastructure was covered by UPS in-case of sudden power outage
Conclusion
The shell command and command line integrations are without a doubt some of the most powerful integrations available in Home Assistant. They open up a whole world of possibilities!
You should now be able to integrate remote bash scripts into Home Assistant, allowing almost unlimited control of any Linux-based remote device!
Congratulations if you made it to the end!