Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
projects:ssh_reverse_tunnel [2016/06/08 19:06]
over23
projects:ssh_reverse_tunnel [2016/06/10 00:29] (current)
over23
Line 4: Line 4:
 |**Related:​**| [[project:​single_boards|[Project Single Boards]]]| |**Related:​**| [[project:​single_boards|[Project Single Boards]]]|
 |**References:​**|[[http://​www.tunnelsup.com/​raspberry-pi-phoning-home-using-a-reverse-remote-ssh-tunnel]]| |**References:​**|[[http://​www.tunnelsup.com/​raspberry-pi-phoning-home-using-a-reverse-remote-ssh-tunnel]]|
-|**Materials:​**| dynamic IP device aka PICKA, static IP device aka RemoteHost, ssh able to connect between devices by keys|+|**Materials:​**| dynamic IP device aka PICKA, static IP device aka RemoteHost, ​[[project:​single_boards#​remove_password_to_ssh|ssh able to connect between devices by keys]]| 
 +|**THX to:**| thx to:  Jack Rhysider; @TunnelsUp; http://​www.tunnelsup.com/​raspberry-pi-phoning-home-using-a-reverse-remote-ssh-tunnel ​|
 |**License:​** | [[https://​creativecommons.org/​licenses/​by-sa/​3.0/​|CC Attribution-Share Alike 3.0 Unported]] ​ |  ​ |**License:​** | [[https://​creativecommons.org/​licenses/​by-sa/​3.0/​|CC Attribution-Share Alike 3.0 Unported]] ​ |  ​
 +{{ projects:​tunnel-of-plants-252820_960_720.jpg?​500|Taken from: https://​pixabay.com/​static/​uploads/​photo/​2014/​01/​27/​03/​28/​tunnel-of-plants-252820_960_720.jpg under CC; thx}}
 ===== Legend: ===== ===== Legend: =====
   * **Picka** = bannanaPi, raspberryPi,​ any othere device on dynamic IP   * **Picka** = bannanaPi, raspberryPi,​ any othere device on dynamic IP
Line 16: Line 17:
   * we have **RemoteHost**,​ that we can see from anywhere and Picka is able to SSH to that server   * we have **RemoteHost**,​ that we can see from anywhere and Picka is able to SSH to that server
   * we will build ssh tunnel from **Picka** to **RemoteHost**   * we will build ssh tunnel from **Picka** to **RemoteHost**
-  * RemoteHost and Picka should be able to reach each other by certificate,​ not by passwd+  * [[project:​single_boards#​remove_password_to_ssh|RemoteHost and Picka should be able to reach each other by certificate,​ not by passwd]]
  
 ==== on Picka do ==== ==== on Picka do ====
Line 25: Line 26:
  
 **explanation of parameters of tunnel:** **explanation of parameters of tunnel:**
-  ​ssh - shell command + ssh - shell command 
-  -N do not execute anything when connection successful + -N do not execute anything when connection successful 
-  -R bind port 22 on remote server [192.168.1.1] to 2222 + -R bind port 22 on remote server [192.168.1.1] to 2222 
  
 ==== so now connect to RemoteHost and ====  ==== so now connect to RemoteHost and ==== 
Line 40: Line 41:
 That is essentially what the remote tunnel does. That is essentially what the remote tunnel does.
  
 +
 +==== persistent tunnel from Picka to RemoteHost ==== 
 +
 +Now let’s take a step back and look at what we’ve done. When the Raspberri Pi is on, it will check every minute to see if an ssh connection to your linux server exists. If it doesn’t it will create one. The tunnel it creates is really a reverse remote tunnel. Once the tunnel is up, anyone who ssh’s into port 2222 of the linux server will then be redirected to the Pi. Incredible!
 +
 +=== OnPicka ===
 +
 +<​code>​
 +touch ~/​ssh_tunnel.sh
 +chmod 700 ~/​ssh_tunnel.sh
 +vim ~/​ssh_tunnel.sh
 +</​code>​
 +
 +
 +  * add and save
 +
 +<​code>​
 +#!/bin/bash -x
 +createTunnel() {
 +##### fill in vars
 +  RemoteHost="​IP_HERE"​
 +  ServerUser="​NICK_HERE"​
 +##### are wars filled?
 +
 +  /​usr/​bin/​ssh -N -R 2222:​localhost:​22 ${ServerUser}@${RemoteHost} &
 +  if [[ $? -eq 0 ]]; then
 +    echo Tunnel to RemoteHost IP: ${RemoteHost} created successfully
 +  else
 +    echo An error occurred creating a tunnel to RemoteHost IP: ${RemoteHost}. ReturnCode was: ${?}
 +  fi
 +}
 +/bin/pidof ssh
 +if [[ ${?} -ne 0 ]]; then
 +  echo Creating new tunnel connection to RemoteHost IP: ${RemoteHost}
 +  createTunnel
 +fi
 +</​code>​
 +
 +  * add script to cron
 +
 +<​code>​
 +crontab -e
 +</​code>​
 +
 +  * and add (every minute check if the ssh connection is up, if not, attempt to bring it up)
 +
 +<​code>​
 +*/1 * * * * ~/​ssh_tunnel.sh > ~/​ssh_tunnel.log 2>&1
 +</​code>​
 +
 +
 +==== Note for MAC users ====
 +* in case you have problem, that wiifi is turned off when screensaver is on
 +* remember en1 should be replaced by your wiifi interface
 +<​code>​
 +cd /​System/​Library/​PrivateFrameworks/​Apple80211.framework/​Versions/​Current/​Resources
 +sudo ./airport en1 prefs DisconnectOnLogout=NO
 +</​code>​
  • projects/ssh_reverse_tunnel.1465405594.txt.gz
  • Last modified: 2016/06/08 19:06
  • by over23