#!/bin/sh
#
#
# Date: 01.10.2010
# Author: Claudiu Vasadi
# Licence: BSD
#
######################## Description ##########################
# #
# Script to update the IP's for 1x "gif" interface #
# #
# CHECK THE "GLOBALS" VARIABLES BEFORE LAUNCHING THE SCRIPT ! #
# #
###############################################################
# Copyright 2010 Claudiu Vasadi. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification, are
# permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this list of
# conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice, this list
# of conditions and the following disclaimer in the documentation and/or other materials
# provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY Claudiu Vasadi ``AS IS'' AND ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
# FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Claudiu Vasadi OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# The views and conclusions contained in the software and documentation are those of the
# authors and should not be interpreted as representing official policies, either expressed
# or implied, of Claudiu Vasadi.
PATH=/bin:/sbin:/usr/bin:/usr/sbin
#set -x # Uncoment if needed for debugging
######################## GLOBALS #################################
# Interfaces
ext_if="tun0" # Local external interface name
int_if="rl0" # Local internal interface name
gif_if="gif0" # gif interface name+number
# Local section
local_int_IP="192.168.1.1" # Local internal IP
local_network="192.168.1.0/24" # Local network (with mask)
#local_dns="local_DNS_if_any.com" # Local DNS (if any)
# Remote section
remote_int_IP="192.168.0.1" # Remote internal IP
remote_network="192.168.0.0/24" # Remote network (with mask)
remote_dns="remote_DNS_goes_here.com" # Remote DNS (mandatory)
########################### CMD's ################################
# Check if gif_if exists. If it doesn't, create it.
ifconfig $gif_if >> /dev/null
if [ $? != "0" ];then
ifconfig $gif_if create
fi
# Get the local IP from the ext_if (with ifconfig cmd)
local_IP="`ifconfig $ext_if | grep inet | awk '{print $2}'`"
# Get the remote IP from a DNS server (with dig)
remote_IP="`dig $remote_dns | grep $remote_dns | awk '{print $5}' | grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}'`"
if [ -z $remote_IP ];then
echo "Cannot get ${remote_dns}'s IP. Exiting ..."
exit 1
fi
# Assume that the gif interface mentioned above has been created and:
# Get local ("tunnel") IP from the gif interface (if any at the first run of the script)
local_gif_tunnel_IP="`ifconfig $gif_if | grep "tunnel inet" | awk '{print $3}'`"
# Get the remote ("tunnel") IP from the gif interface
remote_gif_tunnel_IP="`ifconfig $gif_if | grep "tunnel inet" | awk '{print $5}'`"
# Get the local IP from the gif interface (int network IP)
local_gif_IP="`ifconfig $gif_if | grep netmask | awk '{print $2}'`"
# Get the remote IP from the gif interface (int network IP)
remote_gif_IP="`ifconfig $gif_if | grep netmask | awk '{print $4}'`"
# Check if the route exists
check_route="`netstat -f inet -rn | grep $remote_network | awk '{print $1}'`"
# Update the EXTERNAL IP's
if [ "$local_IP" != "$local_gif_tunnel_IP" ];then
local_IP1=$local_IP
if [ "$remote_IP" != "$remote_gif_tunnel_IP" ];then
remote_IP1=$remote_IP
ifconfig $gif_if tunnel $local_IP1 $remote_IP1
else
remote_IP2=$remote_IP
ifconfig $gif_if tunnel $local_IP1 $remote_IP2
fi
else # if equal, local_IP stays the same
if [ "$remote_IP" != "$remote_gif_tunnel_IP" ];then
remote_IP2=$remote_IP
ifconfig $gif_if tunnel $local_IP $remote_IP2
fi
fi
# Update the INTERNAL IP's
if [ "$local_int_IP" != "$local_gif_IP" ];then
local_int_IP1=$local_int_IP
if [ "$remote_gif_IP" != "$remote_int_IP" ];then
remote_int_IP1=$remote_int_IP
ifconfig $gif_if $local_int_IP1 $remote_int_IP1
else
ifconfig $gif_if $local_int_IP1 $remote_int_IP1
fi
else # if equal, local_IP stays the same
if [ "$remote_int_IP" != "$remote_gif_IP" ];then
ifconfig $gif_if $local_int_IP $remote_int_IP1
fi
fi
# Add a static route
if [ "$check_route" != "$remote_network" ];then
route add -net $remote_network $remote_int_IP
fi