I must have used ssh for the past 15 years but I never knew this trick. So if you want to ssh into a remote server and run some command on it, and exit, you don’t actually need to login into the console and type the commands. It can be done by injecting commands through a ssh connection (and piping the output back too).
So if you want to ssh into the raspberry pi and do a simple ‘ls’, use this:
ssh pi@host -p port# 'ls'
If you use key authentication and have saved the keys, your login can be automated without the need for password (more on that later).
If you’d like to get the output and store it on the local host, use regular linux redirection:
ssh pi@host -p port# 'ls' > test.txt
Isn’t this great 🙂
More details can be found here: