两个动态 ip pc 之间通过第三个静态 ip pc 的 SSH 桥接用于 jupyter notebook

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

通常我使用这种脚本(这是更大的 bash 脚本的一部分)将我的电脑远程连接到静态 IP 电脑和 运行 jupyter 笔记本:

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/

并且有效。但是现在我需要访问具有动态 ip 的第三台电脑,我不能使用像 dyndns 这样的服务。一个好的解决方案是创建一个简单的 VPN,就像在此处所做的那样 https://superuser.com/questions/315523/ssh-connection-between-two-behind-nat-computers-through-third-public-ip-computer

这意味着我必须在远程电脑(dynamic-ip pc)上做:

ssh -R 20000:127.0.0.1:22 user@RemoteHost 

在我的本地电脑上(也有动态 IP):

ssh -L 8000:127.0.0.1:20000 user@RemoteHost

最后,如果我这样做:

ssh 127.0.0.1 -p 8000

这将创建“VPN”。但是现在我有点迷失了如何 运行 jupyter notebook 转发一个端口,我可以在我的本地浏览器上使用我的远程 pc 的 jupyter notebook,使用带有静态 ip 的远程主机作为桥梁.

欢迎提出任何建议或提示。谢谢。

您可以在本地电脑上使用 ~/.ssh/config 简化它

HOST remotehost
    hostname remotehost.tld
    user myuser

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

现在你可以做一个简单的

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

如果你 ssh 到 thirdparty 这将使用远程主机作为跳转框 (ProxyJump) 并且配置中的 LocalFoward 自动建立到你的 jupyter notebook 的端口转发