我不是直接回答您的 STUN/ICE 问题,而是回答您的目标,即如何突破 NAT 并远程 ssh 到您的 pi。
解决此问题的最简单方法是使用反向 ssh 隧道,尤其是使用 autossh 和基于密钥的身份验证的隧道。
这要求你有自己的服务器和一个 ssh 端口供 pi 调用(我使用一个只是坐在我的家庭网络上,找出你的公共 IP 地址,如果你愿意,可以注册一个免费的 ddns 帐户使用一个容易记住的 URL)。
准备好你的树莓派,最好是在你自己的家庭网络上使用你将继续使用的服务器(只需将运行 linux 的旧桌面或另一个 pi 连接到你的路由器并保持打开状态。我会假设您已将外部端口 30022 转发到服务器上的端口 22,在此示例中从您的家庭路由器)。您还将使用基于密钥的身份验证。
在你的 pi 上:
sudo apt-get install autossh
# Generate key
sudo -u pi ssh-keygen
# Copy key to your server (while you're on your home network with the server is easiest, but not necessary)
sudo -u pi ssh-copy-id -i /home/pi/.ssh/id_rsa.pub [serverUser]@[serverIP]
然后,如果您愿意,您需要另一个“配置文件”,放在您的主目录中的 pi 上。如果你愿意,就叫它 myConf.sh
#!/bin/bash
rSSHPort=31001 # you'll use a different port for each pi you connect to your server. Make sure all these ports are forwarded on your home router to the server (port forward the range e.g. 31000-31100 would let you do 100 pi's)
# Phone Home
USER=pi # or whatever your pi user is named
KEY=/home/pi/.ssh/id_rsa
HOST=myServer.ddns.net # or whatever your server URL or public IP address is
REMOTE_USER=serverUser # or the user you want your pi to connect to the server as
REMOTE_PORT=30022 # or whatever port you have forwarded to your server for ssh (don't use 22 as its even more of a security vulnerability).
然后,您将拥有一个最终脚本,该脚本将实现对您的服务器的实际 ssh 回调。如果你愿意,可以称之为 connectServer.sh,并把它放在 /home/pi/ 中
#!/bin/bash
# Reverse Tunnel SSH in to server
# -f detach script from terminal
# -N no commands can be executed on server side
# -R reverse tunnel
# -p using server ssh port
# -i path to key file
# Source the Config File
source '/home/pi/myConf.sh'
connect()
{
# TODO need to check that autoSSH isn't already in process list
autoSSHProc=$( ps ax | grep autossh | wc -l )
if [ "$autoSSHProc" -le "1" ]
then
log
su -c "autossh -f -N -q -i ${KEY} -p ${REMOTE_PORT} -R ${rSSHPort}:localhost:22 ${REMOTE_USER}@${HOST} -oControlMaster=no -oUserKnownHostsFile=/dev/null -oStrictHostKeyChecking=no" $USER
fi
}
# Log connection details
log()
{
datStr=$(date)
echo "Connected system using autossh at " "$datStr" >> connection.log
}
connect
现在在你的 pi 上运行它
sudo ./connectServer
现在您想从笔记本电脑或您连接的任何设备通过 ssh 连接到您的服务器。
ssh [serverUser]@[serverIP] -p 30022
进入您的服务器后,您可以连接反向隧道
ssh pi@localhost -p 31001
瞧!
这是我到目前为止所使用的参考资料:
反向ssh转发:https://www.howtoforge.com/reverse-ssh-tunneling
使用 ssh 密钥设置服务器,传递给 pi 单位:http://www.thegeekstuff.com/2008/11/3-steps-to-perform-ssh-login-without-password-using-ssh-keygen-ssh-copy-id/
和http://jmatthews.us/blog/2013/02/18/rpi-dorm/
设置自动拨号主页:https://www.raspberrypi.org/forums/viewtopic.php?f=36&t=32077