Accessing the HEMMER cluster and GRID from outside NTNU is normally restricted. Also might be restricted when using WiFi.

One way to connect to them is either to connect to VPN or to use the login.(ansatt | phys | stud).ntnu.no as jumphosts. This usually means slow sessions or unresponsive X11 GUI sessions.

One way to make it faster is to use SSH tunnels, and better SSH multiplexing.

To make it easier, here is a script sshtun.sh  that creates a function to do all that with a single command. You can copy or source it in your .bashrc

# SourceMe
# Recomended to set up passwordless ssh authentication https://linuxize.com/post/how-to-setup-passwordless-ssh-login/
# Copy the .pub key to both login.ansatt.ntnu.no and physics cluster workstation

function sshtun() {
  # Replace $USER with your NTNU username
  local USERNAME=$USER
  if [[ -z "$USERNAME" ]]; then
    echo "USERNAME is not defined"
    return 1
  fi
  local tunnel_sessions="${HOME}/.ssh/ssh-tunnels-created"
  if [[ ${1} == "help" ]] || [[ -z "$1" ]]; then
    local __help="
    Usage: ${FUNCNAME[0]} hostname
    where hostname must be a FQDN hostname! (eg. igor.phys.ntnu.no)
    This function will create a multiplexed ssh tunnel through one of the 3 hosts at NTNU
    after which will establish an ssh with X11 forwarding connection to your host using 
    the tunnel. This way the connections, especially those requiring GUIs, will be really
    fast and responsive.
    Each time a new tunnel will be created even if you're trying to connect to the same 
    host. To connect multiple times to the same host, follow the instructions on the screen
    after establishing a connection or, check the file \"$tunnel_sessions\"
    "
    echo "$__help"
    return 0
  fi
  host ${1}
  if [ $? -eq 0 ]; then
    local options=( "login.ansatt.ntnu.no" "login.phys.ntnu.no" "login.stud.ntnu.no" )
    # Print the menu options
    echo "Select the tunneling host"
    for ((i=0; i<${#options[@]}; i++)); do
      echo "$((i+1)). ${options[$i]}"
    done

    # Prompt the user to enter a choice
    read -p "Enter your choice (1-${#options[@]}) (1): " choice
    # Default option [1]; Change to another one if you want
    if [[ -z "$choice" ]]; then
      choice=1
    fi
    echo "Choice was: $choice"
    # Validate the input
    re='^[0-9]+$'
    if ! [[ $choice =~ $re ]] || (( choice < 1 || choice > ${#options[@]} )); then
      return 1
    fi
      # Process the chosen option
    case $choice in
      1)
        t_host=tun-ansatt
        m_host=login.ansatt.ntnu.no
        ;;
      2)
        t_host=tun-phys
        m_host=login.phys.ntnu.no
        ;;
      3)
        t_host=tun-stud
        m_host=login.stud.ntnu.no
        ;;
      *)
        echo "Not a valid choice. Exiting"
        return 1
        ;;
    esac
    
    echo "Connecting to ${1} through ${m_host} with user ${USERNAME}"
    local PORT=$(( ((RANDOM<<15)|RANDOM) % 63001 + 2000 ))
    local counter=0
    while [[ $(netstat -ant -p TCP | grep ${PORT}) -ne 0 ]]; do
      echo "Trying new port ..."
      local PORT=$(( ((RANDOM<<15)|RANDOM) % 63001 + 2000 ))
      ((counter++))
      if [[ $counter -gt 5 ]]; then
        echo "Couldn't find a free port"
        return 1
      fi
      sleep 1
    done

      local SSHCPath="${HOME}/.ssh/${USERNAME}@${m_host}:${PORT}-${1}"
      ssh -4 -f -N -T -M \
          -o ExitOnForwardFailure=yes \
          -o ControlMaster=autoask \
          -o ControlPersist=10m \
          -o ControlPath="${SSHCPath}" \
          -L 127.0.0.1:${PORT}:${1}:22 ${USERNAME}@${m_host}
      
      local exit_code=$?
      if [[ $exit_code -eq 0 ]]; then 
        sleep 3
        
        echo -e "\n****************************************************" >> $tunnel_sessions
        echo "* @ $(date +%F/%T)" >> $tunnel_sessions
        echo "****************************************************" >> $tunnel_sessions
        echo -e "You can reconnect/reuse the tunnel to ${1} with:\n ssh -4 -X  ${USERNAME}@127.0.0.1 -p ${PORT}" | tee -a "$tunnel_sessions"
        echo "Check ${tunnel_sessions} for tunnels opened"
        echo "FORWARDING added to port: ${PORT}"
        echo -e "Check for ControlMaster connections status:\n ssh -O check -S \"${SSHCPath}\" ${1}" | tee -a "$tunnel_sessions"
        echo -e "Stop the tunnels from accepting connections:\n ssh -O stop -S \"${SSHCPath}\" ${1}" | tee -a "$tunnel_sessions"
        ssh  -4 -X ${USERNAME}@127.0.0.1 -p${PORT}
      else
        echo "ERROR: Couldn't create multiplexed connection!" 
        retun 1
      fi
  else
    echo -e "Hostname not found. Must be a FQDN hostname! (eg. igor.phys.ntnu.no)"
    echo "Type \"${FUNCNAME[0]} help\" for more info."
  fi
}

These multiplexed tunnels can be used also to forward connection to remote Jupyter notebooks or to copy files with rsync for example.

  • Connect to a remote jupyter notebook session
    1. Launch the jupyter notebook session on your remote PC, with --no-browser option set.
      Note the port given in the links: http://localhost:8889/?token=c6f45f0918daa1380fd402a35c63c7c8826d7b8b9ffd9601
    2. Establish a port forwarding session with ssh by using the command given by sshtun which looks like: ssh -4 -X  username@127.0.0.1 -p 22177
      and  add: ssh -4 -X  -L 8889:127.0.0.1:8889 username@127.0.0.1 -p 22177
      and run the command, which will open a new session.
    3. Open the browser on your local PC and use the link given by Jupyter notebook.
  • Copy files with rsync
    • Note the port used by the ssh tunnel, eg. 22177
    • to copy from remote PC:

      rsync -auv -e "ssh -p 22177" username@127.0.0.1:~/some_file   ./
      
    • to copy to remote PC: 

      rsync -auv -e "ssh -p 22177" ./  username@127.0.0.1:~/some_file  
  • No labels

0 Comments

You are not logged in. Any changes you make will be marked as anonymous. You may want to Log In if you already have an account.