I have shell accounts on different servers. Sometimes - especially if new Debian/Ubuntu/Redhat/Fedora updates are available - I have to open a
ssh session to each server. That takes time and too many keyboard clicks and being a lazy admin I thought this job can be scripted.
Last year I switched from my long-time favourite windowmanager fvwm2 to KDE. Given the fact that most of my work is still done using a terminal, I decided to give konsole a chance and found that this tool has some nice advantages compared to the simple xterm which I used before. One lovely feature is the tabbing functionality, another one the integrated scripting which can be used with
dcop (Desktop Communication Protocol).
That should be enough functionality for the task in question. I could not find a feature-complete script on the www, just some basic examples but most of them poke around with extracting the
ps and similar workarounds. Nothing which only uses
dcop. Ok, here’s my version:
This script is using
dcop to retrieve all necessary information about
konsole, terminals and sessions. My own script accepts an extra parameter to pick a smaller group of servers from the full list but this is just overhead for this example. Usage:
You start the script and a new
konsole window will be opened and maximized. A new tab is created for each server and a ssh connection is started. By using
exec the tab will automatically close if you
ssh session. Any extra script parameter will be passed to each
ssh command - nice for executing one command on all servers, running the update as example. You still have one tab for each server in - contrary to other solutions which start all sessions in one single window.
Note: you have to quote your command properly or else your current shell will be picky. Example:
This will list the content of the current directory on each server and wait
5 seconds to give you a chance to actually see the output. If you forget the ``, the
ls is sent to each server and the connection is immediately closed because the command finishes. The
sleep 5 is interpreted by your local shell.
This script has one small problem: I don’t know if the newly created shell (
newSession) is actually ready to accept commands (
sendSession). That’s why I added the
sleep 2, but on a heavily loaded machine that might not be enough. Does someone know a way to detect if the shell is ready?