#!/bin/sh # open an ssh tunnel for tunneling ssh traffic from offsite system # #@(#) nxtunnel #----------------------------------------------------------------------- # Version History #----------------------------------------------------------------------- # 1.1 2014/10/10 cuffe@jlab.org - re-write for acclogin # 1.0 2010/04/07 cuffe@jlab.org - inital revision #----------------------------------------------------------------------- # Debug variables #----------------------------------------------------------------------- # Print shell commands as they are executued #set -x # Run the script but do not actually execute any commands #set -n #----------------------------------------------------------------------- # Global Variables #----------------------------------------------------------------------- # TUNNEL_HOST is the gateway login server TUNNEL_HOST=acclogin.jlab.org # NX_HOST is the remote system that runs the NX server. NX_HOST=opsnx.acc.jlab.org # Other possible NX Hosts #NX_HOST=devnx.acc.jlab.org #NX_HOST=chlnx.acc.jlab.org #NX_HOST=felnx.acc.jlab.org #----------------------------------------------------------------------- # Command Variables #----------------------------------------------------------------------- SSH=`which ssh` BIN_LIST="$SSH" #----------------------------------------------------------------------- # Functions #----------------------------------------------------------------------- function usage { echo "usage: $0" echo "" echo "This script is used to open a tunnel through the jlab acclogin server" echo "and wait to kill the connection." echo "" } function checkfile { if [ ! -f $1 ];then echo "Could not find file $1" echo "Please make sure it is installed and in your path" exit 1 fi if [ ! -x $1 ];then echo "Could not execute file $1" exit 1 fi } function getuser { printf "Username: " read USER if [ ! -n "$USER" ];then getuser fi } #----------------------------------------------------------------------- # Process Options #----------------------------------------------------------------------- while getopts ":h" opt; do case $opt in h) usage exit 0 ;; \?) echo "Unknown Option: $opt" usage exit 1 ;; *) echo "Unknown Option: $opt" usage exit 1 ;; esac done # The expression $(($OPTIND - 1)) is an arithmetic expression equal to $OPTIND minus 1. # This value is used as the argument to shift. The result is that the correct number of # arguments are shifted out of the way, leaving the "real" arguments as $1, $2, etc. shift $(($OPTIND - 1)) #----------------------------------------------------------------------- # Data File Sanity Check #----------------------------------------------------------------------- for BIN in $BIN_LIST do checkfile $BIN done #----------------------------------------------------------------------- # User Info #----------------------------------------------------------------------- getuser #----------------------------------------------------------------------- # Open Tunnel and Desktop #----------------------------------------------------------------------- echo "Opening ssh tunnel to $NX_HOST through $TUNNEL_HOST for $USER" echo "Please provide your two-factor credential when asked for a password." ssh -l $USER -f -N -C -L 2222:$NX_HOST:22 $TUNNEL_HOST # Run NX Client #open -a 'NoMachine' #----------------------------------------------------------------------- # Kill any left over tunnels #----------------------------------------------------------------------- echo '' echo 'Waiting to cleanup and kill all tunnels' echo 'Would you like to kill all tunnels now?' read RESPONSE case $RESPONSE in y* | Y*) echo '' for i in `ps -ef | grep ssh | grep $NX_HOST | awk '{print $2}'` do echo "Killing Tunnel with PID $i" kill -9 $i done ;; *) echo "Please manually kill the following tunnels:" echo "ps -ef | grep ssh | grep $NX_HOST" ps -ef | grep ssh | grep $NX_HOST exit 0 ;; esac exit 0