使用Ngrok绕过防火墙访问内网树莓派

下载Ngrok

去官方网站下载即可

https://dashboard.ngrok.com/get-started

注意在下载的时候选择LinuxARM版本的.

[email protected]:~$ sudo wget https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-arm.zip
--2019-07-05 10:16:27--  https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-arm.zip
Resolving bin.equinox.io (bin.equinox.io)... 52.54.237.49, 52.45.111.123, 54.165.51.142, ...
Connecting to bin.equinox.io (bin.equinox.io)|52.54.237.49|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 16486783 (16M) [application/octet-stream]
Saving to: ‘ngrok-stable-linux-arm.zip’

ngrok-stable-linux-arm.z 100%[=================================>]  15.72M  46.1KB/s    in 3m 48s

2019-07-05 10:20:16 (70.7 KB/s) - ‘ngrok-stable-linux-arm.zip’ saved [16486783/16486783]

解压到指定目录

[email protected]:~$ unzip ngrok-stable-linux-arm.zip -d ./ngrok
Archive:  ngrok-stable-linux-arm.zip
  inflating: ./ngrok/ngrok

注册Ngrok账户并连接

解压完之后cd到目录中运行authtoken 命令进行账户验证

$ ./ngrok authtoken 验证码

运行Ngrok

将22端口转发出去,走的是TCP协议.这时候会得到一个公网的IP地址和端口号,接下来拿着这个公网IP地址和端口号就可以在任何有网络的地方访问我们家里的树莓派了.

[email protected]:~/ngrok$ ./ngrok TCP 22
NAME:
  ngrok - tunnel local ports to public URLs and inspect traffic

DESCRIPTION:
    ngrok exposes local networked services behinds NATs and firewalls to the
    public internet over a secure tunnel. Share local websites, build/test
ngrok by @inconshreveable
Session Status                online
Account                        (Plan: Free)
Version                       2.3.30
Region                        United States (us)
Web Interface                 http://127.0.0.1:4040
Forwarding                    tcp://公网IP和端口号 -> localhost:22

Connections                   ttl     opn     rt1     rt5     p50     p90     
                              1       1       0.00    0.00    36.43   36.43 

使用Powershell进行连接

PS C:\Users\xxx> ssh [email protected]提供的公网IP -p 端口号
[email protected]'s password:
Welcome to Ubuntu 18.04.2 LTS (GNU/Linux 4.15.0-1031-raspi2 aarch64)

* Documentation:  https://help.ubuntu.com
* Management:     https://landscape.canonical.com
* Support:        https://ubuntu.com/advantage

  System information as of Fri Jul  5 10:41:46 UTC 2019

  System load:  0.16              Processes:           109
  Usage of /:   42.9% of 7.09GB   Users logged in:     1
  Memory usage: 17%               IP address for eth0: 192.168.1.111
  Swap usage:   2%

* MicroK8s 1.15 is out! It has already been installed on more
  than 14 different distros. Guess which ones?

    https://snapcraft.io/microk8s

  Get cloud support with Ubuntu Advantage Cloud Guest:
    http://www.ubuntu.com/business/services/cloud

0 packages can be updated.
0 updates are security updates.


Last login: Fri Jul  5 10:29:52 2019 from 127.0.0.1
Welcome to Ubuntu 18.04.2 LTS (GNU/Linux 4.15.0-1031-raspi2 aarch64)

* Documentation:  https://help.ubuntu.com
* Management:     https://landscape.canonical.com
* Support:        https://ubuntu.com/advantage

  System information as of Fri Jul  5 10:41:46 UTC 2019

  System load:  0.16              Processes:           109
  Usage of /:   42.9% of 7.09GB   Users logged in:     1
  Memory usage: 17%               IP address for eth0: 192.168.1.111
  Swap usage:   2%

* MicroK8s 1.15 is out! It has already been installed on more
  than 14 different distros. Guess which ones?

    https://snapcraft.io/microk8s

  Get cloud support with Ubuntu Advantage Cloud Guest:
    http://www.ubuntu.com/business/services/cloud

0 packages can be updated.
0 updates are security updates.


Last login: Fri Jul  5 10:29:52 2019 from 127.0.0.1
[email protected]:~$

保持Ngrok后台运行

下载Screen

sudo apt-get install screen

screen的作用就是管理远程会话的.

使用Screen

Screen -S ngrokSSH

-S(大写) 创建Screen会话,并给这个会话指定一个名字

接着输入Ngrok的运行命令

./ngrok tcp 22

完事儿,不信你试试关闭窗口之后再使用Ngrok给我们提供的公网IP和端口进行连接看看能不能连的上.


  目录