From a5cd6a5dd100db5121ea67a4118b927dcec7028a Mon Sep 17 00:00:00 2001 From: Jakub Vonka Date: Wed, 28 Jan 2026 11:53:07 +0100 Subject: [PATCH] Added jupyter link printing --- print_jupyter_link.sh | 55 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 print_jupyter_link.sh diff --git a/print_jupyter_link.sh b/print_jupyter_link.sh new file mode 100644 index 0000000..6ea2f48 --- /dev/null +++ b/print_jupyter_link.sh @@ -0,0 +1,55 @@ +#!/usr/bin/env bash + +# Script to extract where is the jupyter session running by parsing the log file +# This is useful when re-loging to the same node or if one misses it when the new jupyter session is initiated with start_jupyter.sh + +set -euo pipefail + +LOGFILE=$( + find "$HOME" -maxdepth 1 -type f -name "jupyterlab_*.log" -printf "%T@ %p\n" 2>/dev/null \ + | sort -n \ + | awk '{print $2}' \ + | tail -n 1 +) + +if [[ -z "${LOGFILE:-}" || ! -f "$LOGFILE" ]]; then + echo "No jupyterlab_*.log found in $HOME" + exit 1 +fi + +# Slurp the whole logfile and try multiple patterns. +perl -0777 -ne ' + my $s = $_; + $s =~ s/\r//g; + $s =~ s/\n/ /g; + + # 1) Direct URL that already contains token= + if ($s =~ m{(https?://[^\s"'"'"']+\?token=[^\s"'"'"'&]+)} ) { + print "$1\n"; exit 0; + } + if ($s =~ m{(https?://[^\s"'"'"']+/lab\?token=[^\s"'"'"'&]+)} ) { + print "$1\n"; exit 0; + } + if ($s =~ m{(https?://[^\s"'"'"']+/tree\?token=[^\s"'"'"'&]+)} ) { + print "$1\n"; exit 0; + } + + # 2) URL and token appear separately (wrapped / split) + my ($base) = ($s =~ m{(https?://[^\s"'"'"']+)}); # first URL-looking thing + my ($token) = ($s =~ m{token=([A-Za-z0-9]+)}); # token value + if ($base && $token) { + # Ensure we don’t end up with double ?token + $base =~ s/\?token=.*$//; + print $base . "?token=" . $token . "\n"; + exit 0; + } + + exit 1; +' "$LOGFILE" || { + echo "Could not find a Jupyter token link in:" + echo " $LOGFILE" + echo + echo "Try:" + echo " grep -n -E \"token=|http://|https://\" \"$LOGFILE\" | head -n 80" + exit 1 +}