Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Iristyle/267d7779d2218c79e8ad843d4e38d939 to your computer and use it in GitHub Desktop.
Save Iristyle/267d7779d2218c79e8ad843d4e38d939 to your computer and use it in GitHub Desktop.
Check tunnel from inside Transmission openvpn container
# based on tunnelChecker from
# https://github.com/mrjackyliang/tunnelChecker
# modified to work on Alpine and doesn't require a script in the container
CONTAINER=transmission
# alpine container will always have ash (even if bash installed)
docker exec -i $CONTAINER sh <<'EOF'
PORT_FORWARD=12345
IP_CHECK=http://ipinfo.io/ip
# address of the tunnel
VPN_ADDR=$(ip -4 addr show tun0 | grep -Eo '(\d{1,3}\.){3}\d{1,3}' | head -n1)
# alternate: ifconfig tun0 | grep "inet " | cut -d: -f2 | awk '{print $1}'
echo "Internal tun0 IP is ${VPN_ADDR}"
externalip() { curl -sS --interface $1 $IP_CHECK; }
TUN_IP=$(externalip tun0) # ip visible through the tunnel
ETH_IP=$(externalip eth0) # ip visible through eth0 (regular ISP IP)
echo "External tun0 (VPN) IP is ${TUN_IP}"
echo "External eth0 IP is ${ETH_IP}"
nc -z $VPN_ADDR $PORT_FORWARD
if [ $? -eq 0 ]; then
VPN_PORT="open";
else
VPN_PORT="closed";
fi;
echo "Network Port is ${VPN_PORT}"
if [ "$TUN_IP" = "$ETH_IP" ] || [ "$VPN_PORT" = "closed" ]; then
echo "Killing OpenVPN"
pkill -SIGINT openvpn
else
echo "VPN is already working!"
fi
exit 0
EOF
exit 0
@Iristyle
Copy link
Author

Iristyle commented Mar 7, 2020

This can be run as a scheduled task on a Synology to basically kick a transmission container that has lost its VPN port

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment