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

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

Still would love to do this with Hass.io but cannot figure the user permissions out so if anyone can help appreciated :slight_smile:

Kudos to @frenck and @cogneato on Discord who pointed me to this:

https://developers.home-assistant.io/docs/en/next/hassio_debugging.html#hassos-based-hassio

which helped solve the SSH connection issue and has allowed me to work this on Hass.io. Will work on a tutorial for it when I can retrace my steps!

2 Likes

So isn’t learning fun! With all of this you ended up learning a ton and will be more prepared for future curves.
Superb!

1 Like

Quick question, where in your hassio container’s file system did you place your key file?

EDIT: Oh, dur. I guess I can put it anywhere so long as it has the right perms. Thanks!

1 Like

For future readers interested in this topic, and controlling remote devices via SSH, the link to “A thorough guide on controlling anything SSH with Homeassistants’s Hass.io” above is broken but the material can still be found at https://dovydas.xyz/homeassistant/hyperion/ssh/hassio/2018/01/22/controlling-anything-via-ssh.html
Thanks for your useful contributions in this thread, John

1 Like

Useful thread

1 Like

I know its old thread, but stumbled upon it when looking to integrate my HP home server with iLO, and I did it via iLO API:

iLO 4 API: iLO RESTful API Data Model Reference (iLO 4)

My solution via RESTful Command: RESTful Command - Home Assistant

#RestAPI iLO start server
rest_command:
  server_poweron:
    url: "https://<IPADDRESS>/rest/v1/systems/1"
    method: POST
    headers:
      accept: "application/json"
    payload: '{"Action":"Reset","ResetType":"On"}'
    content_type:  'application/json; charset=utf-8'
    username: apiuser
    password: apipassword
    verify_ssl: false

I think that this is cleaner way of doing the same thing without the ssh, and you can create apiuser with only specific permissions on iLO.

happy iLOing :wink: