#!/bin/bash
# @angeldp
# Activa o desactiva de forma permanente las interfaces de red
# presentes en sistemas RedHat based.
#
NETDIR="/etc/sysconfig/network-scripts"
AUXFICH="${TEMP}/auxfich.tmp"
ROJO="\e[1;31m"
VERDE="\e[1;32m"
ORIGIN="\e[1;m"
#
#
function echoRojo(){
echo -e "${ROJO}${*}${ORIGIN}"
}
function echoVerde(){
echo -e "${VERDE}${*}$ORIGIN"
}
function fallo(){
clear
echoRojo "¡Se ha producido un error!"
echo $1
read -s -n1 -p "Pulse para finalizar..."
rm -rf $AUXFICH &> /dev/null
clear
exit
}
function ayuda(){
clear
echoVerde Sintaxis:
echo "$0 '[ --KO | --OK ]'"
echo -e "--KO\tDeshabilita las interfaces de red permanentemente"
echo -e "--OK\tHabilita las interfaces de red de forma permanente"
echo "Sin parámetros se mostrará esta ayuda"
read -n1 -sp "Pulse para finalizar..."
clear
rm -rf $AUXFICH &> /dev/null
exit
}
function tumba(){
clear
for NIC in $*
do
more $NETDIR/$NIC | grep -iv ^ONBOOT > $AUXFICH
echo "ONBOOT=no" >> $AUXFICH
mv -f $AUXFICH $NETDIR/$NIC 2> /dev/null
[ $? -eq 0 ] || fallo "Se produjo un error con $NETDIR/$NIC"
echo -ne "`echo $NIC | cut -d- -f2`\t"
echoRojo Deshabilitada
done
}
function levanta(){
clear
for NIC in $*
do
more $NETDIR/$NIC | grep -iv ^ONBOOT > $AUXFICH
echo "ONBOOT=yes" >> $AUXFICH
mv -f $AUXFICH $NETDIR/$NIC 2> /dev/null
[ $? -eq 0 ] || fallo "Se produjo un error con $NETDIR/$NIC"
echo -ne "`echo $NIC | cut -d- -f2`\t"
echoVerde Habilitada
done
}
#
#
#Se tiene que ejecutar con no más de 1 parámetro
[ $# -le 1 ] || fallo "Sintaxis incorrecta"
#Debe existir el directorio de configuración de la red
[ -d $NETDIR ] || fallo "No existe el directorio $NETDIR"
#Se debe ejecutar con privilegios administrativos
[ $UID -eq 0 ] || fallo "Debe ejecutarse como administrador"
#
#Según sea el primer parámetro se le da un valor a la variable OPCION
OPCION=""
case $1 in
--KO | --ko )
OPCION="K";;
--OK | --ok )
OPCION="S";;
* )
ayuda;;
esac
# Obtener las interfaces configuradas
IFACES=`ls -1 $NETDIR | grep ^ifcfg- | grep -v ifcfg-lo`
numIFACES=`echo $IFACES | wc -w`
# Si el número de interfaces es 0 lo indico y salgo
[ $numIFACES -gt 0 ] || fallo "No se han encontrado interfaces para configurar"
if [ "$OPCION" = "K" ]
then
tumba $IFACES
service network restart
else
levanta $IFACES
service network restart
fi
echo -e "\n\n"
read -n1 -sp "Pulse para finalizar.."
rm -rf $AUXFICH 2> /dev/null
clear
exit