Skip to content

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?

Trackbacks

ads' corner on : Manage Serendipity installations with Spartacus

Show preview
I have to manage some Serendipity (s9y) installations. Only one of them is my personal blog, the others are customer installations, all on different servers. Apart from new serendipity versions there's also the problem with updated plugins. How can one Comments ()

darlonv.wordpress.com on : PingBack

Show preview
Comments ()

Comments

Display comments as Linear | Threaded

akretschmer on :

Reinvent the wheel? I know, you are using Debian. But why not dsh for such a task? Quote from apt-cache show dsh: Description: dancer's shell, or distributed shell Executes specified command on a group of computers using remote shell methods such as rsh or ssh. This tool is handy for administration of PC clusters, and multiple hosts.
Comments ()

Andreas 'ads' Scherbaum on :

Most of the time i do interactive work on the remote machines. dsh would be no help here. With konsole i get a tab for each server and in addition i can switch between tabs by hitting shift+ or shift+. That's handy. The ability to start commands like "apt-get update" on all servers is just an additional feature. If you don't add a command you just get a interactive shell. I also know about cluster ssh but this tool is only useful if all machines are 100% identical.
Comments ()

tomas on :

try replace sleep 2 by this line: while [ `dcop $konsole $newsession sessionPID` -eq 0 ]; do sleep 0.1 done and i also suggest to add -t argument to ssh, this forces ssh to allocate tty and you could pass a command 'apt-get upgrade; bash -i' and end up with normally operating bash surviving ctrl-c
Comments ()

logu on :

# start a new konsole window and save the handle in $konsole konsole=$(dcopstart konsole-script) what is this "konsole-script". What is stored in konsole. -logu
Comments ()

Andreas 'ads' Scherbaum on :

"konsole-script" is a "dcopstart" option and opens a new "Konsole" window. $konsole stores the reference to the newly opened window which can be used in subsequenc "dcop" calls.
Comments ()

Nate on :

Try multi-gnome-terminal?
Comments ()

Andreas 'ads' Scherbaum on :

Why? Does this provide better functionality for my scripts? Does this fix any of the problems in my posting? Is gnome-terminal controllable with dcop or dbus? Is gnome-terminal even well integrated into KDE? I don't think so. So can you please provide some hints, why i should switch the terminal program?
Comments ()

Piotr Dobrogost on :

Thanks for sharing this with us. Do you have any plans to update it for KDE4/dbus?
Comments ()

Add Comment

Enclosing asterisks marks text as bold (*word*), underscore are made via _word_.
E-Mail addresses will not be displayed and will only be used for E-Mail notifications.
To leave a comment you must approve it via e-mail, which will be sent to your address after submission.
Form options