Start multiple ssh sessions with KDE konsole

Posted by ads' corner on Thursday, 2008-09-11
Posted in [Kde][Shell]

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 pid from ps and similar workarounds. Nothing which only uses dcop. Ok, here’s my version:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/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

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:

1
./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 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?


Categories: [Kde] [Shell]