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
Next revision Both sides next revision
projects:ssh_reverse_tunnel [2016/06/08 19:06]
over23
projects:ssh_reverse_tunnel [2016/06/08 19:29]
over23 [ALIVE] Reverse SSH Tunnel - nice CC tunnel picture added
Line 5: Line 5:
 |**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, 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 39: Line 40:
 The **RemoteHost** is listening on port 2222 for incoming ssh connections. If it receives one, it will forward all traffic it receives into the previous ssh connection that was established already. ​ The **RemoteHost** is listening on port 2222 for incoming ssh connections. If it receives one, it will forward all traffic it receives into the previous ssh connection that was established already. ​
 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
 +createTunnel() {
 +##### fill in vars
 +  RemoteHost="​INSERT.IP.HERE.NOW"​
 +  ServerUser="​NAME_FOR_REMOTE_USER"​
 +##### 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>​
 +
 +
 +
  
  • projects/ssh_reverse_tunnel.txt
  • Last modified: 2016/06/10 00:29
  • by over23