﻿#!/bin/bash

# Copyright (c) Verax Systems.  All Rights Reserved.
#
# Script used to remove virtual interfaces (for Linux & UNIX)
# Description:
#     This script is used by Verax SNMP Simulator to turn virtual interfaces down.
#     All interfaces created by Verax SNMP Simulator are stored in temporary file $FILE
#     which is read by this script to make them all down.
#
# NOTE: This script is being invoked internally by Verax SNMP Simulator and cannot be invoked manually.

modprobe 8021q

#cat created_interfaces.txt | awk '{printf("ifconfig %s down",$1)}'| exec

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

FILE="$DIR/created_interfaces.txt"

# Checking if file $FILE exists and is readable
if [ ! -f $FILE ]; then
	echo "$FILE does not exists."
	exit 1
elif [ ! -r $FILE ]; then
	echo "$FILE cannot be read."
	exit 2
fi

# Reading file $FILE using the file descriptor
 
# Set loop separator to end of line
BAKIFS=$IFS
IFS=$(echo -en "\n\b")
exec 3<&0
exec 0<"$FILE"
while read -r line
do
	# Turning interface down 
	ifconfig $line down
done
exec 0<&3
echo "" > $FILE 

# Restore $IFS which was used to determine the field separators.
IFS=$BAKIFS
exit 0

