1

My university has access to some academic journals that I cannot access from home. To be able to download papers while I am away, my current proccess is:

  • Create an ssh tunnel to my university computer with ssh -D 12345 me@mylab.myuniversity.edu

  • Launch a new instance of Firefox using a separate profile that I configured to use that tunnel as a proxy: firefox -P uniprofile -no-remote. This way I can still have other windows open that don't send traffic to my uni.

  • Once I am done downloading my papers, I exit firefox and close my ssh connection.

What I can't figure out is how bundle all of this up into a single script so that I can create a desktop launcher that sets up the tunnel (prompting me for the password), launches firefox when that is done and then closes the ssh connection when I close the firefox window. My first attempt was to simply create a script with the ssh command followed by the firefox command but what ended up happening is that firefox only gets launched after I close the ssh connection.

#!/bin/bash
set -e
ssh -D 12345 me@mylab.myuniversity.edu
firefox -P uniprofile -no-remote
#...and at this point find a way to close the ssh connection...
  • Can you update your questions with what you currently have in the script? Also, did you try adding nohup and & to your ssh tunnel (like so: nohup ssh -D 12345 me@mylab.myuniversity.edu > /dev/null 2>&1 &) to background it which should allow firefox to run (this also assumes you aren't typing your password in each time by using ssh-keys or expect). You can capture the PID from the ssh tunnel using PID=$! before you launch firefox in your script and kill it at the end of your script (kill $PID). – devnull Feb 23 '15 at 1:51
  • Do you have ssh-keys for auto login? Or are you entering your password? – devnull Feb 23 '15 at 1:58
  • I'm entering the password everytime (and I'm ok with that for now). As for & I tried that once but what happened was that firefox opened before I had a chance to type my password... I don't think that is a huge deal but I would prefer if firefox only launched if the ssh connection was successful. – hugomg Feb 23 '15 at 2:01
  • Try my answer below. It should work in theory. But like I said, I don't have a GUI so it is hard for me to verify. – devnull Feb 23 '15 at 2:14
  • There maybe some useful info here stackoverflow.com/questions/21063765/get-pid-in-shell-bash – Red Cricket Feb 23 '15 at 2:44
3

This is untested (since I don't use GUIs) but try this:

#!/bin/bash

# From this answer http://unix.stackexchange.com/a/29949/82289
SSH_HOST=me@mylab.myuniversity.edu
ssh -f -N -D 12345 -M -S /tmp/ssh_tunnel_%h.sock -o ExitOnForwardFailure=yes $SSH_HOST && \
echo "ssh tunnel started successfully" || \
echo "ssh tunnel failed to start"

# Launch firefox
firefox -P uniprofile -no-remote

# We should only get here after firefox closes
# Close the ssh tunnel socket
ssh -S /tmp/ssh_tunnel_%h.sock -O exit $SSH_HOST
# End shell-script

The 2 flags -f and -N are what I think you want (from man ssh).

-f

Requests ssh to go to background just before command execution. This is useful if ssh is going to ask for passwords or passphrases, but the user wants it in the background. This implies -n. The recommended way to start X11 programs at a remote site is with something like ssh -f host xterm.


-N

Do not execute a remote command. This is useful for just forwarding ports (protocol version 2 only).

  • It worked perfectly, including calling it from a GUI launcher! Thanks! – hugomg Feb 23 '15 at 3:09
0

Alternatively, if you've installed firefox on that machine, you could open firefox and just have it displayed on your machine like so:

RUN:

export DISPLAY=:0.0; 
ssh -Y me@mylab.myuniversity.edu

Then when the SSH connection is set up, just run firefox on that machine:

firefox

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.