Start multiple ssh sessions with KDE konsole
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 much 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 in the www, just some basic examples but most of them poke around with extracting the pid from ps and similar workarounds. Nothing which only uses dcop. Ok, here's my version:
----- cut -----
#!/bin/bash
SERVER="
root@server1.home
root@server2.job
root@server3.extern
"
# get extra parameters
param=""
for p in "$@"
do
param="$param $p"
done
echo "extra parameter: $param"
# start a new konsole window and save the handle in $konsole
konsole=$(dcopstart konsole-script)
# maximize the new window
dcop $konsole konsole-mainwindow#1 maximize
# get current session for the first (just created) window
thissession=$(dcop $konsole konsole currentSession)
# rename this window/session
dcop $konsole $thissession renameSession "init"
# start a new session tab for each server
for s in $SERVER ; do
# this output is displayed on the terminal which is running your script
echo "connect to server: $s"
# create another konsole tab and save handle in $newsession
newsession=`dcop $konsole konsole newSession "ssh $s"`
# wait for shell startup - raise if needed
sleep 2
# rename the new session
dcop $konsole $newsession renameSession "$s"
# and start the ssh session
dcop $konsole $newsession sendSession "exec ssh $s \"$param\""
done
# close the first session window
dcop $konsole $thissession closeSession > /dev/null
----- cut -----
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 exit the 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:
./script.sh "ls ; sleep 5"
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 finished. The "sleep 5" is interpreted by your current shell.
This script has one small problem: i don't know if the fresh created shell (newSession) is actually ready to accept commands (sendSession). That's why i'm 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?
Comments
Display comments as Linear | Threaded
akretschmer on :
Andreas 'ads' Scherbaum on :
tomas on :
logu on :
Andreas 'ads' Scherbaum on :
Nate on :
Andreas 'ads' Scherbaum on :
Piotr Dobrogost on :