0

The following works flawlessly, in that it allows me to access a webpage hosted on the Pi at my domain which is hooked up to a google compute vm.

pi@raspberrypi:~ $ ./pi_home_reverse_proxy/scripts/establish_remote_connection.sh
Tunnel to host created successfully
pi@raspberrypi:~ $

I've attempted to make this functionality persistent by running the script 'at boot' via the usual methods (crontab, /etc/rc.local, creating a service via systemctl) and none have achieved the functionality that is achieved by running the script from the shell after boot.

I have confirmed that that various methods of scheduling at boot have indeed ran the script by including a command at the end of the script to create a file or log some details. I have focused my efforts on the systemctl service method, as this allows for waiting until the network is 'up', as outlined in the solution to this question:

Running a script after an internet connection is established

This is the service file:

[Unit]
Description=Establish Remote Connection Service
Wants=network-online.target
After=network-online.target

[Service]
Type=simple
User=pi
WorkingDirectory=/home/pi
ExecStart=/home/pi/pi_home_reverse_proxy/scripts/establish_remote_connection.sh

[Install]
WantedBy=multi-user.target

A status check on this service results in:

pi@raspberrypi:~ $ sudo systemctl status establish_remote_connection.service
● establish_remote_connection.service - Establish Remote Connection Service
     Loaded: loaded (/etc/systemd/system/establish_remote_connection.service; enabled; vendor preset: enabled)
     Active: inactive (dead) since Mon 2022-03-07 19:27:22 AWST; 9s ago
    Process: 976 ExecStart=/home/pi/pi_home_reverse_proxy/scripts/establish_remote_connection.sh > ~/tunnel.log (code=exited, status=0/SUCCESS)
   Main PID: 976 (code=exited, status=0/SUCCESS)
        CPU: 118ms

Mar 07 19:27:21 raspberrypi systemd[1]: Started Establish Remote Connection Service.
Mar 07 19:27:22 raspberrypi establish_remote_connection.sh[976]: Tunnel to host created successfully
Mar 07 19:27:22 raspberrypi systemd[1]: establish_remote_connection.service: Succeeded.

The contents of the script (with the public ip/domain removed) are as follows:

#!/bin/bash
# establish_remote_connection.sh
# Establishes a remote connection with the cloud server
# intended to be run on a Raspberry Pi serving something

# The IP of your running service in the cloud
# export REMOTE_HOST=<vm-ip-address>
export REMOTE_HOST=<domain>

# Keep this at 5000, it is configured in nginx config remotely
export REMOTE_PORT=5000

# The local host of the service to forward (usually 0.0.0.0)
export LOCAL_HOST=0.0.0.0

# The local port of the running service to forward
export LOCAL_PORT=3000


ssh -f -N -T -R $REMOTE_PORT:$LOCAL_HOST:$LOCAL_PORT piconnect@$REMOTE_HOST
if [[ $? -eq 0 ]]; then
    echo "Tunnel to host created successfully"
else
    echo "An error occurred creating a tunnel to host. RC was $?"
fi

I have followed the advice in the nearest issue I could find:

Create ssh tunnel on startup

Yet it has not yielded any results.

Would be great to get to the bottom of this!

1 Answer 1

0

SOLVED!

I ended up following the crontab configuration in this video:

https://www.youtube.com/watch?v=lTYXKAKv-ok

To summarise:

crontab -e

And adding this line at the bottom:

*/1 * * * * ~/pi_home_reverse_proxy/scripts/establish_remote_connection.sh > tunnel.log 2>&1

Resolved my issue! I can finally restart the pi and have the website accessible from my domain afterwards.

Having said that... I'd still like to know why! And, I'd like to know why I'm getting this error in the log file:

Tunnel to host created successfully
Warning: remote port forwarding failed for listen port 5000

Thanks in advance!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.