Saturday, January 8, 2022

[SOLVED] SSH bridge between two dynamic-ip pcs through third static-ip pc for jupyter notebook

Issue

normally I use this kind of script (this is part of a larger bash script) to connect my pc to a static-ip pc and run a jupyter notebook remotely:

sudo fuser -k 8881/tcp
echo 'Port 8881 is free now'
ssh -N -f -L localhost:8881:localhost:8888 $username@$servername 
read -p 'SSH Tunnel created - Press enter to continue'
ssh $username@$servername 'nohup jupyter notebook --no-browser --notebook-dir="$notebookdir"'
wait
echo 'Please, wait some seconds.'
$nohup $browser http://localhost:8881/

And it works. But now I need to access a third pc that have a dynamic ip and I cant use services like dyndns. One good solution is to create a simple VPN like was done here https://superuser.com/questions/315523/ssh-connection-between-two-behind-nat-computers-through-third-public-ip-computer.

This means that I have to do in the remote pc (dynamic-ip pc):

ssh -R 20000:127.0.0.1:22 user@RemoteHost 

On my local pc (which also have a dynamic-ip):

ssh -L 8000:127.0.0.1:20000 user@RemoteHost

And finally, if I do:

ssh 127.0.0.1 -p 8000

This creates the "VPN". But now I'm a little lost on how to run the jupyter notebook forwarding a port in away that I can use the jupyter notebook of my remote pc on my local browser, using the remotehost with static-ip as a bridge.

Any suggestions or tips are welcome. Thank you.


Solution

You could simplify it with your ~/.ssh/config on your local pc

HOST remotehost
    hostname remotehost.tld
    user myuser

Host thirdparty
    hostname localhost
    port 20000
    LocalForward 8081 localhost:8080
    ProxyJump remotehost

Now you can do a simple

ssh thirdparty "nohup jupyter notebook --no-browser ..."
ssh -N thirdparty
$nohup $browser http://localhost:8881/

If you ssh to thirdparty this will use the remotehost as a jump box (ProxyJump) and the LocalFoward in the config automatically builds up the port forwarding to your jupyter notebook



Answered By - jeb