Do you have the portainer add-on? I’ve found that extremely helpful when trying to get my own shell commands and command line sensors working.
If you open that add-on you can go to the homeassistant container and connect to its console. Anything I’ve got working there works seems to work the same when running from one of these integrations (give or take the restrictions imposed in shell command around piping and such).
I have a similar shell command and to get that working I logged on to portainer, did the keygen there and then pushed the public key to my remote server. After that I was able to use an ssh command from my shell command and sensor without issue.