Skip to content

Instantly share code, notes, and snippets.

@dotcs
Created March 24, 2019 15:21
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 dotcs/fa6963eefad4cf93d5cbe80eb83405fc to your computer and use it in GitHub Desktop.
Save dotcs/fa6963eefad4cf93d5cbe80eb83405fc to your computer and use it in GitHub Desktop.
Script that creates an IPv4-to-IPv6 bridge
#!/usr/bin/env bash
# This script determines the IPv6 address of a given domain. The so found
# address is then used to create an IPv4-to-IPv6 tunnel from this device to the
# IPv6 address.
# [6tunnel](https://packages.debian.org/de/sid/6tunnel) is used to create the
# proxy.
#
# Usage: ./ipv6-mapper.sh <DOMAIN> <HOST_PORT=443> <TARGET_PORT=443>
set -e
DOMAIN=${1} # e.g. mysub.mydomain.com, must have a valid AAAA record
HOST_PORT=${2:-443}
TARGET_PORT=${3:-443}
if ! [ -x "$(command -v 6tunnel)" ]; then
>&2 echo "Fatal error: 6tunnel is not installed. Abort."
exit 1
fi
# Request current ipv6 address
IPv6=`dig +short "${DOMAIN}" AAAA`
if [ -z "$IPv6" ]; then
>&2 echo "Fatal error: IPv6 DNS entry not available. Abort."
exit 1
fi
echo "---------- [ start ipv4-to-ipv6-bridge ] ----------"
echo `date`
echo "Found IPv6 address: ${IPv6}"
proc_count=`ps -aux | grep "6tunnel ${HOST_PORT} ${IPv6}" | wc -l`
if [ proc_count = "0" ]; then
# Kill all existing tunnel processes
echo "Removing already established tunnel connections on this port"
kill -9 `ps -aux | grep "6tunnel ${HOST_PORT}" | awk '{ print $2 }'`
# Establish connection to new ip
echo "Establish new tunnel connection"
6tunnel ${HOST_PORT} ${IPv6} ${TARGET_PORT}
echo "6tunnel connection has been updated. Finish."
else
echo "6tunnel connection up to date. No need to update connection. Finish."
fi
echo "---------- [ end ipv4-to-ipv6-bridge ] ----------"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment