nevernew
nevernew

Reputation: 672

List ssh sessions and their open xwindows with bash

I'm trying to write a bash script to call from my .bashrc that will show how many ssh connections there are from the current clients machine and how many xwindows are open in each of those ssh connections.

I went about it by looking at the results of w, first getting the number of lines containing the ssh client ip address (the number of ssh connections from the client)

numConnectionsFromClient=$(w | grep $(echo "$SSH_CLIENT" | awk '{print $1;}') | wc -l);

then getting the number of xwindows by looking in w for the $DISPLAY value (usually localhost:10.0)

numXwindowsFromCurrentConnection=$(w | grep "$DISPLAY" | wc -l);

That should get the amount of xwindows opened in the current connection, but the "FROM" value isn't always the same as the "$DISPLAY" value.

If there are two shh connections from the client, all the "FROM" values say "localhost:10.0" but on the second ssh connection the "$DISPLAY" is "localhost:11.0" So they don't match and my script thinks there are 2 xwindows open in the first connection and none in the second, when there is actually 1 started from each... Thus, confusion.

... Basically I want the script to display the number of connections from the current client, and the number of xwindows started from the current connection from that client. Something like this:

Welcome blah blah blah
 - number of ssh connections open on your machine: 2
 - number of xwindows open in this ssh session: 1

or even better

Welcome blah blah blah
Current ssh connections from your machine:
    $detailsOfSession1 - 1 xwindow open
    $detailsOfSession2 - 1 xwindow open

where details of session are "user@ip:port > their DISPLAY setting"

The aim of all this is so that if there are 0 xwindows open for the current session, it will open up 3 graphical terminals in xwindows and position them how I like for dev work. The terminals open in the xwindows fine, but because it always thinks there are no xwindows open in the second connection, each terminal that is opened then opens another 3, then each of them another 3 and so on in a never ending loop until I'm swamped in terminal windows! -.-

It's quite a complex question, I wasn't sure how to phrase it, although I'm hoping the answer will be simple.. Thanks in advance :)

Upvotes: 1

Views: 563

Answers (1)

JvO
JvO

Reputation: 3106

I have a suggestion: rather than check if/how many terminals there are, you can instruct SSH to execute a script on the remote side that opens the 3 terminals. This way you keep control of the situation. If you want an additional 'regular' SSH connection, start one that does not call the script.

Executing a script remotely with SSH is trivial:

ssh -X your.development.box ~/bin/open3terms

Where open3terms is the shell script on the remote machine that opens the 3 terminals to your liking. To open an additional SSH connection without terminals, just use

ssh your.development.box

Make this into 2 aliases (say '2dev3' resp. '2dev') and you save yourself a lot of typing.

Upvotes: 0

Related Questions