snork.ca: Welcome to the brown age of computing! Partly Cloudy, 14.9°C - No precip

Favourite SSH Sessions Automation 2023-09-30


I usually have a bunch of SSH sessions open all the time. Some of them have logfiles scrolling through them, while others are just interactive sessions waiting for me to run commands. I often open a very similar bunch of sessions every time I log in to my workstation. For a while now I have been using asbru-cm to manage my sessions, but it seems like an unnecessary thing to have installed and is a bit of an eyesore when I run "ps aux" because it is a wall of perl commands in that list. So recently I asked The Fediverse if anyone knew of a way to automate opening a bunch of sessions all at once with a regular terminal application. I got a number of replies about using tmux and screen, both of which I have tried before... and byobu which I haven't tried before. The problem with those, is that they are not actual tabs, they are individual sessions that can be switched between. A subtle difference, but it means that I do not get a row of tabs that I can switch between with one click.

I know there are CLI lovers who can't wrap their heads around it, but there really are some tasks that are simply more easily managed with a mouse. You have a directory with ten randomly named files and want to select the third, seventh, and ninth files. CTRL plus three clicks beats tab completion... sorry. Anyways, tabs have been a thing for a long time already and they have their place... anyone who does't believe that needs to leave my workflow alone and go hassle browser developers. You'll get about the same traction in either case. So, what about my SSH sessions? Well, after some fartin' around I got it to work with xfce4-terminal (and I just don't think it is possible with lxterminal).

I started off by making a little script on my server for each log file I wish to follow, like these:

logtail-mail #!/bin/bash
tail -F /var/log/mail.log

logtail-nginx #!/bin/bash
tail -F /var/log/nginx/access.log

Then I made a ~/.ssh/config file on my workstation, with connections for each log file and each interactive session. You'll see that all three of these connections are to the same server, but the first two run the above scripts while the third just opens a regular session:

~/.ssh/config Host lily-mail
  HostName 10.0.0.7
  User root
  IdentityFile ~/path/mykey.key
  RemoteCommand "logtail-mail"

Host lily-nginx
  HostName 10.0.0.7
  User root
  IdentityFile ~/path/mykey.key
  RemoteCommand "logtail-nginx"

Host lily
  HostName 10.0.0.7
  User root
  IdentityFile ~/path/mykey.key

Then I installed xfce4-terminal on my workstation and ran a command like this within it:

xfce4-terminal --tab --title "lily-mail" --command "ssh lily-mail" --tab --title "lily-nginx" --command "ssh lily-nginx" --tab --title "lily" --command "ssh lily"

You'll notice that the names in the --title and --command sections match what I put in my ~/.ssh/config above. This opens up tabs, with the names I want, with SSH session to the servers I want, and will even start scrolling the log files I want on my various servers. It is really a shame lxterminal (my preferred term app as of late) doesn't support this... especially since the man pages for it make it look like it comes so close to being able to. You'll also see in the screenshot below that any time there is activity in one of my sessions the tab name turns red which helps quickly identify where "stuff" is happening. :-)

Screenshot of my xfce4-terminal window open and a selection of sessions to choose from Booyeah!

Thanks a heap to everyone who replied to my #askfedi request, I really think this may work out for me.

Made with Notepad++ & FastStone, without javascript, cookies, or the help of Clippy or ai. Hosted on Devuan with nginx & powered by NK shrooms.