How to login to SSH session and send two consecutive commands

I would like to create a button on my Home Assistant dashboard to SSH into my a device on my network and send two commands. To do this, I need to SSH into the device, which I have a username and password for (no SSH key). After initiating the connection, I need to send two consecutive commands.

What is the best way to achieve this? I have tried searching for how to do this, but most forum threads have info on either using an SSH key or how a single command is sent.

write the 2 commands into a script and then send that as 1 command?

You can run two commands in a row using &&. e.g. this writes a file size to a file then does a delay:

docker exec addon_a0d7b954_influxdb du -s /data/influxdb/data/homeassistant > /config/file_sensors/ha_influx_db_size.txt && sleep 300

&& means “run the next command if the first one succeeds”

1 Like

With the commands, where exactly do I enter them to make it easily automatable? Also, I am having trouble finding the exact commands that I need to run. As I said in the OP, I have come across forum threads where the ssh command is run with a reference to the ssh key file, but in my case, I have to enter credentials. Does the entering of the username and password need two extra commands?

I would also appreciate a specific name of the commands that I have to enter for this ssh process, at least enough to try and google the commands and figure out the syntax and stuff myself. I am fairly new to this world of Linux and CLI, so please excuse me if my questions have really easy solutions.

To automate ssh commands without a key, you’ll need to use the sshpass command.

https://linux.die.net/man/1/sshpass

Believe me, set up a key.