Bash script to up/down vlan or virutal interfaces
id : a8o87cw4ao
category : computer
blog : unixlinux
created : 02/12/10 - 20:15:35

Introduction :


This is part of a RHCS firewalling solution

  • First of all be aware that this script only run on Redhat like systems, you need to have a single configuration file
  • for each vlan or virtual interface. They are generally located in /etc/sysconfig/network-scripts/ directory.

  • Configuration file name ---> network-lan.conf
  • Script file name ---> network-lan.sh

Configuration file : network-lan.conf


You can setup the script with this configuration file (named network-lan.conf) :
  • network_script_dir --> Where network configuration scripts are located.
  • ifcfg_prefix --> Network configuration file prefixes.
  • interfaces --> Names of physical interfaces this scripts run on .

network_scripts_dir="/etc/sysconfig/network-scripts/"
ifcfg_prefix="ifcfg-"
interfaces="vlan1101 eth0 eth1 lan man wan"

Script : network-lan.sh


Script usage :
  • ./network-lan.sh
    • start : start all your vlan or virtual interfaces associated with physical interfaces described in configuration
file.
    • stop : stop all your vlan or virutal interfaces associated with physical interfaces described in configuration fii
le.
    • restart : just stop/start aliase.

.01#!/bin/bash
.02
.03#Printing usage
.04usage() {
.05        echo "Usage : $1 start|stop|restart"
.06        exit 1
.07}
.08
.09#Start or stop interfaces with ifconfig (args : list of interfaces, stop or start
.10ifcfg_stop_start() {
.11
.12        #Absolute binaries used for this function
.13        IFCONFIG="/sbin/ifconfig"
.14
.15        #Interface list separated by spaces (ex: "eth1.1 eth0:35 eth3.48")
.16        interfaces_list=$1
.17
.18        #Command (start or stop)
.19        command=$2
.20
.21        #Translate stop in down and start in up
.22        case $command in
.23                stop)
.24                command="down"
.25                ;;
.26
.27                start)
.28                command="up"
.29                ;;
.30        esac
.31
.32        #Do the job
.33        for interface in ${interfaces_list}; do
.34                ${ECHO} "${IFCONFIG} ${interface} ${command}"
.35        done
.36}
.37
.38remove_physical_interfaces() {
.39
.40        #Absolute binaries used for this function
.41        SED="/bin/sed"
.42
.43        #List of physical interfaces
.44        physical_interfaces=$1
.45
.46        #Founded subifs
.47        subifs_names=$2
.48
.49        for physical_interface in ${physical_interfaces}; do
.50                subifs_names=`${ECHO} ${subifs_names} | ${SED} 's/eth1\s//g'`
.51        done
.52}
.53#Usefull vars
.54configuration_file="/scripts/network-lan/network-lan.conf"
.55. ${configuration_file}
.56
.57#Absolute binaries
.58ECHO="/bin/echo"
.59LS="/bin/ls"
.60AWK="/bin/awk"
.61BASENAME="/bin/basename"
.62DEVNULL="/dev/null"
.63
.64if [ $# -ne 1 ]; then
.65        usage `${BASENAME} $0`
.66fi
.67
.68subifs_list=""
.69
.70#For each interface get configured interfaces
.71for interface in ${interfaces}; do
.72        local_subif=`${LS} ${network_scripts_dir}${ifcfg_prefix}${interface}* 2>${DEVNULL}`
.73        subifs_list=${subifs_list}${local_subif}
.74done
.75
.76
.77subifs_names=""
.78
.79#For each interface get interface name (last field in absolute path, and last field in ifcfg
.80for subif in ${subifs_list}; do
.81        local_subif=`${ECHO} $subif | ${AWK} -F'/' {'print $NF'} | ${AWK} -F'-' {'print $NF'}`
.82        subifs_names=${subifs_names}" "${local_subif}
.83done
.84
.85remove_physical_interfaces "${interfaces}" "${subifs_names}"
.86
.87case $1 in
.88        stop)
.89        ifcfg_stop_start "${subifs_names}" stop
.90        exit 0
.91        ;;
.92
.93        start)
.94        ifcfg_stop_start "${subifs_names}" start
.95        exit 0
.96        ;;
.97
.98        restart)
.99        ifcfg_stop_start "${subifs_names}" stop
.100        ifcfg_stop_start "${subifs_names}" start
.101        exit 0
.102        ;;
.103
.104        *)
.105        echo "Invalid argument $1"
.106        usage `${BASENAME} $0`
.107        ;;
.108esac