Skip to content

Instantly share code, notes, and snippets.

@VojtechVitek
Last active August 23, 2021 14:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save VojtechVitek/4d5317324a139952255b53d41897b45f to your computer and use it in GitHub Desktop.
Save VojtechVitek/4d5317324a139952255b53d41897b45f to your computer and use it in GitHub Desktop.
Advertise and route your VPN routes (ppp0) through a Tailscale node
#!/bin/bash
mask2cdr ()
{
# Based on https://stackoverflow.com/questions/20762575/explanation-of-convertor-of-cidr-to-netmask-in-linux-shell-netmask2cdir-and-cdir
local mask=$1
# In RFC 4632 netmasks there's no "255." after a non-255 byte in the mask
local left_stripped_mask=${mask##*255.}
local len_mask=${#mask}
local len_left_stripped_mask=${#left_stripped_mask}
local conversion_table=0^^^128^192^224^240^248^252^254^
local number_of_bits_stripped=$(( ($len_mask - $len_left_stripped_mask)*2 ))
local signifacant_octet=${left_stripped_mask%%.*}
local right_stripped_conversion_table=${conversion_table%%$signifacant_octet*}
local len_right_stripped_conversion_table=${#right_stripped_conversion_table}
local number_of_bits_from_conversion_table=$((len_right_stripped_conversion_table/4))
echo $(( $number_of_bits_stripped + $number_of_bits_from_conversion_table ))
}
sysctl net.ipv4.ip_forward=1
sysctl net.ipv6.conf.all.forwarding=1
routes="$(netstat -rn | grep ppp0 | awk '{print $1 " " $3}' | while read ip ipRange; do printf "%s," $(echo $ip/$(mask2cdr $ipRange)); done)"
echo tailscale up --advertise-routes=${routes::-1}
tailscale up --advertise-routes=${routes::-1}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment