3

When working remotely, I have a series of tabs that I open in gnome-terminal, and commands that I execute in them. I would like to automate all this setup as a single command.

If these commands could run independently and in parallel, I'd just adapt the answer to this question. In fact, I tried, using the following shell script:


gnome-terminal --working-directory="/home/superelectric" --tab -t "gate" -e 'bash -c "export BASH_POST_RC=\"ssh gate_tunnel\"; exec bash"' --tab -t "mydesktop" -e 'bash -c "export BASH_POST_RC=\"ssh tunneled_mydesktop\"; exec bash"'

Spread out over multiple lines, for readability:

gnome-terminal \
  --working-directory="/home/superelectric" \
  --tab \
  -t "gate" \
  -e \
    'bash -c "export BASH_POST_RC=\"ssh gate_tunnel\"; exec bash"' \
  --tab \
  -t "mydesktop" \
  -e \
    'bash -c "export BASH_POST_RC=\"ssh tunneled_mydesktop\"; exec bash"'

The first part opens a tab, names it 'gate', and executes 'ssh gate_tunnel' within it. This is an ssh alias that opens a tunnel to 'mydesktop' at school, through the school's outward-facing server, 'gate'.

The second part opens another tab, names it 'mydesktop', and executes 'ssh tunneled_mydesktop' within it. This is another ssh alias, which connects to mydesktop through the tunnel.

~/.ssh/config: 

Host gate_tunnel
  LocalForward 8023 <my_desktop_at_school>:22
  HostName <my_school_server>

That's the theory. In practice, the two commands execute in parallel, whereas I need to ensure that the first tab's command (open tunnel) completes before executing the second tab's command (connect through tunnel).

Is there maybe some command I can execute in the second tab, that 'waits' until the ssh tunnel is opened?

Community
  • 1
  • 1
SuperElectric
  • 17,548
  • 10
  • 52
  • 69
  • Does locking answer your question? http://stackoverflow.com/q/169964/2157640 If so, you can close it as a duplicate. – Palec Oct 06 '14 at 23:44
  • The easiest was would be to add something like `sleep 10;` before the second command, but of course this would only work if you could be sure your tunnel would open within 10 seconds (or whatever number you put). Can you tell me what the exact tunnel command is (or as close as you can get)? – Seth Oct 08 '14 at 01:25
  • the exact tunnel command is as quoted in the question, with the following entry in ~/.ssh/config: Host gate_tunnel LocalForward 8023 :22 HostName User SuperElectric ForwardX11 yes ServerAliveInterval 30 – SuperElectric Oct 08 '14 at 07:07
  • why do you need to open the tunnel in a separate tab? – glenn jackman Oct 10 '14 at 15:37
  • @glennjackman It's just the way I learned to use tunnels. The tab also allows me to easily see if the tunnel is still open. – SuperElectric Oct 10 '14 at 16:18
  • do you really want tabs, if its all the same to you, just use screen – Gaurav Joseph Oct 11 '14 at 06:30
  • @GauravJoseph That's a possibility, though I do prefer tabs because they're named and visible, whereas screens are numbered and hidden. – SuperElectric Oct 11 '14 at 11:45
  • I'm with glenn on this one, why separate both steps at all ? The first thing that comes to mind for reaching your school desktop from the outside is to ssh into the school gate and from there ssh into your desktop with something like `ssh -t gate.school.edu ssh desktop_machine_name`. There's only one tab then. – lemonsqueeze Oct 11 '14 at 12:34

2 Answers2

3

Ok, I think i get it. As i mentioned in the comments the first thing that comes to mind for reaching your school desktop from the outside is to ssh into the school gate and from there ssh into your desktop with something like:

$ ssh -t gate.school.edu ssh desktop_name

There's only one tab then, so your problem doesn't exist.

However there's something very cool with your current setup:
From home it's almost as if you had a direct connection to your desktop machine, so you can scp into it directly and forget about gate. With the solution above that's not possible anymore because we end up with an indirect connection: If you want to scp you have to do it from gate and that sucks.

Check out this article on using ssh's ProxyCommand feature:

You get the best of both worlds then :)

lemonsqueeze
  • 1,041
  • 8
  • 19
0

Hmm... this may not be a perfect solution. Ideally you should use something that monitors the ssh connection. But, you can check the ssh process with ps. And wait for ssh command to come alive.

#!/bin/bash

COUNTER=0
while [  $COUNTER -lt 10 ]; do # try 10 times
if ps aux ¦ grep <my_desktop_at_school> then
# the tunnel connected now execute the second command
'bash -c "export BASH_POST_RC=\"ssh tunneled_mydesktop\"; exec bash"'
else 
continue # or you could do something here if you wish
fi 
sleep 10 # sleep for 10 seconds and try again 
let COUNTER=COUNTER+1 
done

You will have to run this script in the second tab. Hope it helps.

Gaurav Joseph
  • 927
  • 14
  • 25