SOLVED: Remote ON & OFF an ILO server (HP Microserver) with SSH script and key

I have a Gen8 Microserver and would like to be able to be able to boot the machine using a ILO command sent to the server. The current ILO integration is sensor only:

I have successfully been able to turn it on and off using the commands below, logged in as root to Hassio

shell_command:
server_on: ‘ssh -i /config/ssh/hassio_ilo hassio@ilo12345 “power on”
server_off: ‘ssh -i /config/ssh/hassio_ilo hassio@ilo12345 “power off”

but error is:

ERROR (MainThread) [homeassistant.components.shell_command] Error running command: ssh -i /config/ssh/hassio_ilo hassio@ilo12345 "power off", return code: 255
NoneType: None

Any help welcome!

SOLVED: SOLVED: Remote ON & OFF an ILO server (HP Microserver) with SSH script and key

You could use the iLO API to script whatever you need. https://developer.hpe.com/platform/ilo-restful-api/home
You might also be able to use expect to create very simple script with ssh???

Thanks its unfortunately outside of my expertise at this stage so hoping there could be someone looking to do the same thing a little more knowledgeable :slight_smile:

The joys of working with Home Assistant are that you get to learn new things. Can you ssh into the iLO interface and turn the server on? If you can, you should be able to use expect to create a VERY simple script to do this from HA.

Check your docs for possible BIOS settings required for WOL.

thanks for this advice, I have managed to find a good link for anyone interested:

Which shows how to send a SSH command to power up which I have confirmed works with the correct user settings The command I am using is:

plink -i userkey user@iloserver "power on"

where

userkey = the key filename (generated with putty keygen)
user = username on ILO
and iloserver = the server hostname or IP

Can someone else me with putting this SSH command into a switch or button in HA? I have got as

shell_command:
  jserv_on: ssh user@iloserver "power on"
  jserv_off: ssh user@iloserver "power off" 

within the configuration.yaml

but I need to be able to add in the key files and also a button to run the commands

OK after a number of hours of googling and experimenting I am very close to getting this to work. I have a script which when I SSH in works perfectly, however via a script in HA it gives me an error:

ERROR (MainThread) [homeassistant.components.shell_command] Error running command: `ssh -i /config/ssh/hassio_ilo hassio@ilo12345 "power off"`, return code: 255
NoneType: None

The code I am using is:

in configuration.yaml

shell_command:
  server_on: 'ssh -i /config/ssh/hassio_ilo hassio@ilo12345 "power on"'
  server_off: 'ssh -i /config/ssh/hassio_ilo hassio@ilo12345 "power off"'

in scripts.yaml

# Turn on Server
server_on:
    alias: SERVER ON Script
    sequence:
    -  service: shell_command.server_on

server_off:
    alias: SERVER OFF Script
    sequence:
    -  service: shell_command.server_off

I have the private key (exported as Open SSH key from PuttyGen) in the /config/ssh/ dir with no extension and CHMOD to 600.

If I SSH into HA and run:

ssh -i /config/ssh/hassio_ilo hassio@ilo12345 "power on"

It executes no problem and powers up the server. Note the first time I executed I had to confirm the RSA fingerprint with ‘yes’

Please help!

make sure you are the hass/homeassistant user when you are testing.

I have an RSA key installed and I am unable to login with any other user?

You can change the user.
use
su -s /bin/bash username
replace username with whatever user homeassistant is started with

So I have run

su -s /bin/bash bin

and I get the error:

Could not create directory '/bin/.ssh'.

Sorry I appreciate these are probably basic linux issues!

not sure if I have the right user on Hassio but my users are:

root
bin
daemon
adm
lp
sync
shutdown
halt
mail
news
uucp
operator
man
postmaster
cron
ftp
sshd
at
squid
xfs
games
postgres
cyrus
vpopmail
ntp
smmsp
guest
nobody

Why are you trying to switch to a user called bin?

Lol. Because I have no idea which user hassio would be using out of the available users and presumed it would be bin :sleepy:

updated first post to add current bit I am stuck on

I suspect this is the answer to my issues although have not been able to SSH onto port 2222

https://megamorphf.github.io/homeassistant/hyperion/ssh/hassio/2018/01/22/controlling-anything-via-ssh.html#fn:3

You’d need to set up your ssh server to listen on 2222 (not that I can see the point of using a different port.)

My impression from reading it is there is port 22 for the master OS then port 2222 for the docker container for HA hence the different ports.

Ssh on regular 22 only allows root access and hence the scripts run but fail when trying to run via HA

It’s port 22222 for debug mode not 2222

managed to get this working in the end. For anyone else interested, I gave up with Hass.io. Hassbian has a less complex way of managing users and permissions and the only way the n00b I am could get it to work.

This example relates specifically to turning on and off a HP Microserver via ILO but in principle can be used for any SSH script requiring a private key.

Also not that this assumes you already have the RSA key installed on the server, which I managed to find in a googled tutorial for ILO, and basically requires you to upload one from puttygen.

in this example client_ip = the ILO server IP address

  1. Install Hassbian OS - https://www.home-assistant.io/docs/installation/hassbian/installation/
  2. Run first time connected to the network
  3. Login SSH with pi / raspberry (username / password) with Putty
  4. Install Samba (see: https://github.com/home-assistant/hassbian-scripts/blob/dev/docs/samba.md)
    sudo hassbian-config install samba
  5. to login to your home assistant account type:
    sudo su -s /bin/bash homeassistant
  6. navigate to /home/homeassistant/.homeassistant
  7. create a ssh directory using
    mkdir .ssh
  8. open your samba share in windows
    \\ipaddress
  9. navigate to .ssh directory and copy your private key (OpenSSH format) - i.e. privatekey_filename
  10. Test your script using the format:
    ssh -i "/home/homeassistant/.homeassistant/.ssh/privatekey_filename" privatekey_username@client_ip "power off"
    where privatekey_username is the username if the key installed to your client
  11. This should test OK.

Then goto configuration.yaml and add:

shell_command:
    server_on: ssh -i "/home/homeassistant/.homeassistant/.ssh/privatekey_filename" privatekey_username@client_ip "power on"`
    server_off: ssh -i "/home/homeassistant/.homeassistant/.ssh/privatekey_filename" privatekey_username@client_ip "power off"`

In scripts.yaml add:

server_on:
  alias: Server ON Script
  sequence:
  - service: shell_command.server_on
server_off:
  alias: Server OFF Script
  sequence:
  - service: shell_command.server_off

Reboot and this script will show in HA. The same can be done for Server Off.

Enjoy!

5 Likes

Great job.

1 Like