#!/usr/bin/ksh
#
#ident	"@(#)net6des.stf	1.3	04/12/15 SMI"
#
# Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
# by Bill.Yan
#
# Script name:  net6des
########################################################
# Its function is to destroy IPv6 and IPv6 over IPv4 tunnel interfaces on the 
# local and remote machine.
#########################################################

# Variables
remotemachine=$1			# Remote machine as a client

#
#Check args
#
if [ $# -ne 1 ]
then
        echo "USAGE: $0 remote_hostname"
        echo "!ERROR: $0 called with bad arguments."
        exit 1
fi

#
# IPv4 interface is necessary for IPv6 destruction, first find out the IPv4 
# interface ok or not
#
if [ -z `/usr/sbin/ifconfig -a4 | /usr/bin/grep -v lo0 | grep UP`  ]
then
	#no IPv4 interface exit
	echo "ERROR:	Can't find IPv4 interface!"
	exit 1
fi

#
# Check remotemachine connectivity
#
/usr/sbin/ping $remotemachine
if [ $? -ne 0 ]
then
	#No connectivity to $remotemachine
	echo "ERROR:	No connectivity to $remotemachine!"	
	exit 1
fi

#
# Find out any IPv6 interface set up already
# on locah machine and remote machine. if so, unplumb them
#
if [ ! -z `/usr/sbin/ifconfig -a6` ]
then
	for i in `/usr/sbin/ifconfig -a6 | /usr/bin/grep flags \
			| /usr/bin/cut -d':' -f1`
	do
		/usr/sbin/ifconfig $i inet6 unplumb
		if [ $? -ne 0 ]
		then
			echo "ERROR:	$i can't be unplumbed!"
			exit 1
		fi
	done
fi

if [ ! -z `/usr/bin/rsh $remotemachine /usr/sbin/ifconfig -a6` ]
then
	for i in `/usr/bin/rsh $remotemachine /usr/sbin/ifconfig -a6 \
			| /usr/bin/grep flags | /usr/bin/cut -d':' -f1`
	do
		/usr/bin/rsh $remotemachine /usr/sbin/ifconfig $i inet6 unplumb
		if [ $? -ne 0 ]
		then
			echo "ERROR:	$i inet6 on $remotemachine can't be unplumbed!"
			exit 1
		fi
	done
fi
