Last active
April 12, 2016 10:12
-
-
Save tleonardi/78a7fda51f2083bf72e4acfb3af9e150 to your computer and use it in GitHub Desktop.
Function to make an ssh tunnel that forwards a local display port back to the login node's display port in an LSF cluster that doesn't support X Forwarding
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Tunnel Display | |
# This function sets up an ssh tunnel that forwards a local display port | |
# back to the login node's display port. The tunnel is controlled by the | |
# socket in $HOME/tmp/ssh-${LSB_SUB_HOST}-${HOSTNAME}-${DISPLAY_NUMBER}. | |
# The ssh tunnel is in the background and keeps running even after the | |
# interactive shell is closed, thus preventing completion of the LSF job. | |
# To avoid this, we setup a trap on SIGINT SIGTERM EXIT that uses the ssh | |
# control socket to signal the tunnel to exit. | |
tund(){ | |
if [[ -n $LSB_JOBID ]]; then | |
DISPLAY_NUMBER=$(echo $DISPLAY | sed 's/.\+:\([0-9]\+\)\.[0-9]\+/\1/') | |
COOKIE=$(xauth list | grep $LSB_SUB_HOST/unix:$DISPLAY_NUMBER | awk '{print $2,$3}') | |
xauth add $HOSTNAME/unix:$DISPLAY_NUMBER $COOKIE | |
SSH_CTRL_SOCKET="$HOME/tmp/ssh-${LSB_SUB_HOST}-${HOSTNAME}-${DISPLAY_NUMBER}" | |
if [[ -f $SSH_CTRL_SOCKET ]]; then | |
echo "Error: $SSH_CTRL_SOCKET SSH control socket already exists" | |
return 1 | |
fi | |
ssh -M -S $SSH_CTRL_SOCKET -f -N -L60${DISPLAY_NUMBER}:127.0.0.1:60${DISPLAY_NUMBER} $LSB_SUB_HOST | |
export DISPLAY=localhost:$DISPLAY_NUMBER | |
export SSH_CTRL_SOCKET | |
trap "if [[ -n "$SSH_CTRL_SOCKET" ]] && [[ -S "$SSH_CTRL_SOCKET" ]]; then ssh -S $SSH_CTRL_SOCKET -O exit $LSB_SUB_HOST; fi" SIGINT SIGTERM EXIT | |
else | |
echo "Not on a compute node" | |
return 1 | |
fi | |
} | |
export -f tund |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment