刚刚去了解了一下 Kaggle,一周有 30h 的免费 GPU 算力,但是是使用 jupyter notebook 提供服务的,我不太习惯使用 notebook 来使用 python 环境,感觉适合使用单文件代码来快速实现想法的工具,对于层级很多的工程化代码,有一股代码分割感,所以我想试试是否可以 SSH 连上去
可行方案#
使用 ngrok 内网穿透将 kaggle 中的 SSH 服务器暴露出来,以下是 notebook cell 代码
# 1. 安装并配置ngrok
!pip install pyngrok
from pyngrok import ngrok
# 设置ngrok认证token
ngrok.set_auth_token("your_auth_token")
# 2. 启动SSH服务
!DEBIAN_FRONTEND=noninteractive apt-get install -y openssh-server
!mkdir -p /var/run/sshd
# 配置SSH服务,允许root登录并启用密码认证
!echo "root:your_password" | chpasswd
!echo 'PermitRootLogin yes' >> /etc/ssh/sshd_config
!echo 'PasswordAuthentication yes' >> /etc/ssh/sshd_config
!service ssh start
# 3. 使用ngrok暴露SSH端口
ssh_tunnel = ngrok.connect(22, "tcp")
print("SSH Tunnel:", ssh_tunnel.public_url)
# 4. 输出本地SSH连接命令
ssh_host, ssh_port = ssh_tunnel.public_url.replace("tcp://", "").split(":")
print(f"Use the following command to connect via SSH:\nssh root@{ssh_host} -p {ssh_port}")
限制
运行发现,ngrok 需要绑定信用卡或者借记卡才能使用 TCP 端口,然而大陆的卡好像无法验证通过
使用 HTTP 代理服务出去
将 Kaggle 上的 HTTP 服务穿透出去
%cd ../../
%cd kaggle
%ls
%cp -r input/testfilebox /my
%cd /my
%cd fcb-fronted
!npm install
!npm run build
%cd ../
%pip install -r requirements.txt
# 设置ngrok认证token
ngrok.set_auth_token("your token")
# 2. 使用ngrok暴露FastAPI服务的HTTP端口
http_tunnel = ngrok.connect(12345, "http")
print("Public URL:", http_tunnel.public_url)
!python main.py
此文由 Mix Space 同步更新至 xLog
原始链接为 https://me.liuyaowen.cn/posts//fun/20240901and1