How to quickly run TUI programs via SSH
Hello, and welcome to another blog post! I hope everyone had a lovely and restful Easter.
Very often, I want to run a command on a remote machine via SSH and leave it in a terminal in 1 corner of my screen whilst I work in another terminal on that same machine.
Up until now, I've always SSHed into the machine in question and then run the command manually:
user@local:~$ ssh bob@bobsrockets.com
# .....
bob@bobsrockets.com:~$ sudo htop
This is fine, but it takes a moment to connect & setup the terminal on the remote end. What if there was a way to specify the command to run remotely?
Well, it turns out there is. SSH lets you specify the command to run on the remote server instead of the default shell:
ssh sean@seanssatellites.io apt search beanstalk
Sadly, this doesn't always yield the results expected. Colour disappears from the output, and sometimes things like htop (ssh bill@billsboosters.co.uk htop) and sudo (ssh edgar@edsengineering.eu sudo apt update) break altogether:
Error opening terminal: unknown.
I can't remember how I figured it out, but I discovered that the issue is that when you specify the command instead of letting the default shell initialise, it treats it as some sort of 'script-mode', and doesn't allocate a pseudo-terminal on the remote machine.
Thankfully, there's a way to force it to allocate a pseudo-terminal. This is done with the -t flag:
ssh -t bob@bobsrockets.com sudo htop
This then enables interactive commands to work as intended, and causes colour to be displayed again :D
Found this useful? Got another great SSH tip? Comment below!