Using long-lived access tokens to execute hassio commands over ssh

Solved my own problem. There are several ways to do it:

  • cURL

    • Create a LLAT by going to your profile in the Hass UI (circle at the top-level, scroll to the bottom, create a token)

    • Detailed instructions here on how to construct the request: Authentication API | Home Assistant Developer Docs

      Mine looks like this: (replace homeassistant/check with whatever command you want to run)

$ curl -X POST http://<server>:8123/api/hassio/homeassistant/check -H ‘Authorization: Bearer token_from_ui’

  • SSH to the hassio ssh addon

    • Follow the directions to install the basic ssh addon from the hassio addon store, pick a port, and add your RSA public key into the “authorized keys” section of the config

    • Log in via SSH using the port you chose, type “env” at the prompt and find the “HASSIO_TOKEN=xxx” line. That’s the token you need to use.

    • Run it like this (replace “ha info” with whatever command you want):

$ ssh -p port_number_from_addon root@server_name hassio --api-token “token_from_above” ha info

  • Build the hassio-cli executable and use it from anywhere
    • Generate long-lived access token as in the first option
    • Follow directions to download and build hassio-cli from here: GitHub - home-assistant/cli: 🔳 Home Assistant command line interface
      • When you build it with gox, make sure to specify the platform you need. That was -osarch="linux/amd64" for me, and the example at the link is for ARM.
    • Run it like this (change “ha check” to whatever command you want to run):

$ hassio --endpoint <server>:8123/api/hassio --api-token “token_from_above” ha check

In the end, I used the third option (build hassio-cli locally) and wrote a shell script to run it:

~/bin/hassio-cli \
–endpoint rcoleman-linux:8123/api/hassio \
–api-token “<long-lived-access-token?” \
$*

and I call it “hassio”, so I can use the same command that I use from within the add-on:

hassio ha info

Let me know if you find any typos or have any questions.

2 Likes