#!/bin/bash # OpenMPTCProuter VPS 6in4 service script if [ "$1" = "stop" ] && [ "$(ip link show omr-6in4 up)" ]; then ip route del fd00::/8 via fe80::a00:2 dev omr-6in4 ip link set omr-6in4 down ip tunnel del omr-6in4 exit 0 fi # Add IPv6 tunnel if [ "$(ip link show omr-6in4 up)" ]; then ip tunnel change omr-6in4 mode sit remote 10.0.0.2 local 10.0.0.1 else ip tunnel add omr-6in4 mode sit remote 10.0.0.2 local 10.0.0.1 fi ip link set omr-6in4 up ip route replace fd00::/8 via fe80::a00:2 dev omr-6in4 _ping() { local host=$1 ret=$(ping -4 "${host}" \ -W 5 \ -c 1 \ -q ) && echo "$ret" | grep -sq "0% packet loss" && { return } false } while true; do currentaddr=$(ip addr show omr-6in4 | grep link/sit | awk '{print $2}' | tr -d "\n") currentpeer=$(ip addr show omr-6in4 | grep link/sit | awk '{print $4}' | tr -d "\n") if [ -n "$currentpeer" ]; then _ping $currentpeer status=$? if ! $(exit $status); then allip_tcp=$(ip -4 addr show gt-tun0 | grep inet) allip_udp=$(ip -4 addr show gt-udp-tun0 | grep inet) allip="$allip_tcp $allip_udp" while IFS= read -r inet; do ip=$(echo $inet | awk '{print $2}' | cut -d/ -f1 | tr -d "\n") ipd=$(echo $ip | sed 's/.1/.2/' | tr -d "\n") _ping $ipd status=$? if $(exit $status); then ip tunnel change omr-6in4 mode sit remote $ipd local $ip break fi done < <(printf '%s\n' "$allip") fi fi sleep 5 done