Create a ssh tunnel after network is ready on Debian/Raspberry Pi

Open
#16 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
48/100
Issue type
Documentation
Clarity
Mostly clear
Activity status
Stale
Tech stack
debian, raspberry-pi, shell

Research direction

Start by checking how this repository organizes programming tips, then use the issue's /etc/rc.local example and rc-local systemctl commands as the source material. Done means adding a clear tip explaining how to create the SSH tunnel after networking is ready on Debian or Raspberry Pi, including the required executable and service setup.

Written by the indexing model from the issue text.

Description

A ssh tunnel could be created in /etc/rc.local. As following:

vim /etc/rc.local

timeout=5
ssh -p 22 -o BatchMode=yes -o ConnectTimeout=3 -i /data/david/xxx.pem -fCNR 7280:localhost:22 root@101.20.5.6

while test $? -gt 0
do
   sleep 3 # highly recommended - if it's in your local network, it can try an awful lot pretty quick...
   echo "Trying again..."
   ssh -p 22 -o BatchMode=yes -o ConnectTimeout=3 -i /data/david/xxx.pem -fCNR 7280:localhost:22 root@101.20.5.6
   TIMEOUT=$((TIMEOUT-1))
   if [ $TIMEOUT -eq 0 ]; then
        echo "timed out"
        # error time out
        exit 1
   fi
done

sleep 3

Here the -o BatchMode=yes means the SSH connection will fail immediately if the key is rejected, instead of failing back to a password prompt. The ConnectionTimeout the timeout for ssh command, so it would not wait for a long time.

Note that for Debian 9/10/11, rc.local would not run by default.
You need to make the script executable, and also enable the service.

chmod +x /etc/rc.local
systemctl start rc-local
systemctl enable rc-local
Dominant language
Python
Stars
5
Forks
2
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from davideuler/programming-tips

All issues in davideuler/programming-tips

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.