参考文献
I. 利用Ngrok通过SSH连接Colab
1. Colab端
-
1.1. 安装ngrok,启动ssh服务。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38import random, string, urllib.request, json, getpass
#Generate root password
password = ''.join(random.choice(string.ascii_letters + string.digits) for i in range(20))
#Download ngrok
! wget -q -c -nc https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-amd64.zip
! unzip -qq -n ngrok-stable-linux-amd64.zip
#Setup sshd
! apt-get install -qq -o=Dpkg::Use-Pty=0 openssh-server pwgen > /dev/null
#Set root password
! echo root:$password | chpasswd
! mkdir -p /var/run/sshd
! echo "PermitRootLogin yes" >> /etc/ssh/sshd_config
! echo "PasswordAuthentication yes" >> /etc/ssh/sshd_config
! echo "LD_LIBRARY_PATH=/usr/lib64-nvidia" >> /root/.bashrc
! echo "export LD_LIBRARY_PATH" >> /root/.bashrc
#Run sshd
get_ipython().system_raw('/usr/sbin/sshd -D &')
#Ask token
print("Copy authtoken from https://dashboard.ngrok.com/auth")
authtoken = getpass.getpass()
#Create tunnel
get_ipython().system_raw('./ngrok authtoken $authtoken && ./ngrok tcp 22 &')
#Get public address and print connect command
with urllib.request.urlopen('http://localhost:4040/api/tunnels') as response:
data = json.loads(response.read().decode())
(host, port) = data['tunnels'][0]['public_url'][6:].split(':')
print(f'SSH command: ssh -p{port} root@{host}')
#Print root password
print(f'Root password: {password}') -
1.2. 运行后会输出

- 第一行出现时点击链接,登录个人的ngrok账号获取token。(需要提前注册ngrok账号,免费)
- 验证后出现提示的 SSH command 和password,用于在VS Code上建立SSH连接时输入。
-
1.3. 验证、关闭SSH
1
2!service ssh status
!service ssh stop
2. VS Code端(也可直接在terminal上操作)
-
2.1 安装 Remote Development 插件,然后点击左下角绿色图标,选择 Remote-SSH: Connect to Host…

-
2.2 根据提示,依次输入Colab中得到的 SSH command和password,即可得到Colab服务器的权限

II. Ngrok进行普通内网穿透
1. 安装ngrok
- 下载并解压至bin目录
- 命令
ngrok -h 测试是否安装成功
2. 创建隧道
- 要求本机上部署并启动了相应的服务。
- 2.1 http 服务
- (1)创建
1
2
3
4
5
6#3000是ngrok服务的端口号;
#-auth可选,提供基本身份验证;
#-host-header允许通过定义在本机 `hosts` 文件中的域名进行访问,本例中为:`127.0.0.1 your-app.local`
ngrok http 3000 -auth="username:passwd" -host-header=your-app.local
#8080是web服务端口号,使用-region制定区域:us,eu,ap,au等。默认为us
ngrok http 8080 -region eu - (2)测试链接
1
2curl http://****.ngrok.io #上一步生成的url,get
curl http://****.ngrok.io -d name=hxy #post- (此处需要提前在本机上部署并启动web服务,如express:
node server.js )
- (此处需要提前在本机上部署并启动web服务,如express:
- (1)创建
- 2.2 TCP 服务
- 通过TCP隧道能够建立SSH、数据库等服务。以MongoDB为例:
- (1)创建链接:
ngrok tcp 27017 - (2)测试链接:
mongo #url ,其中的url 为上一步得到的链接
III. 在本地运行Tensorboard
- 后台运行Tb
1
2
3
4
5# Run Tensorboard in the background
LOGDIR = '/tmp/log'
get_ipython().system_raw(
'tensorboard --logdir {} --host 0.0.0.0 --port 6006 &'.format(LOGDIR)
) - 在服务器上安装并启动ngrok进行内网穿透
1
2
3# Use ngrok to tunnel traffic to localhost
! wget https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-amd64.zip
! unzip ngrok-stable-linux-amd64.zip - 获取Tb的超链接
1
2
3
4# Retrieve public url
get_ipython().system_raw('./ngrok http 6006 &')
! curl -s http://localhost:4040/api/tunnels | python3 -c \
"import sys, json; print(json.load(sys.stdin)['tunnels'][0]['public_url'])"
IV. Ngrok 其它特性
- ngrok提供了实时web UI,可以监测http隧道流量。该界面还提供简单的replay requests的功能。
http://#IP:#PORT/inspect/http
- 能够通过YAML文件进行配置
- 默认配置文件路径为:(
$HOME/.ngrok2/ngrok.yml ) - 创建链接时,参数
-config ****.yml 能够指定配置文件,可指定多个配置文件。 - 常见配置内容为隧道定义:
1
2
3
4
5
6
7
8
9
10
11
12tunnels:
app:
proto: http
addr: 3000
bind_tls: false
app-protected:
proto: http
addr: 3000
auth: "username:password"
mongo:
proto: tcp
addr: 27017- 此时通过
start 命令启动ngrok:- 启动某一个:
ngrok start -config ~/.ngrok2/ngrok.yml -config project-config.yml app-protected - 启动全部:
ngrok start -config ~/.ngrok2/ngrok.yml -config project-config.yml --all
- 启动某一个:
- 此时通过
- 默认配置文件路径为:(
- 如果要让ngrox运行子域名或者在自己的域名上,则需要注册账号并付费。


