Advertisement
angeldp

redCentOS

Dec 10th, 2015
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.56 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # @angeldp
  4. # Activa o desactiva de forma permanente las interfaces de red
  5. # presentes en sistemas RedHat based.
  6.  
  7. # <Constantes>
  8. NETDIR="/etc/sysconfig/network-scripts"
  9. AUXFICH="${TEMP}/auxfich.tmp"
  10. ROJO="\e[1;31m"
  11. VERDE="\e[1;32m"
  12. ORIGIN="\e[1;m"
  13. # </Constantes>
  14.  
  15. # <Funciones>
  16. function echoRojo(){
  17. echo -e "${ROJO}${*}${ORIGIN}"
  18. }
  19.  
  20. function echoVerde(){
  21. echo -e "${VERDE}${*}$ORIGIN"
  22. }
  23.  
  24. function fallo(){
  25. clear
  26. echoRojo "¡Se ha producido un error!"
  27. echo $1
  28. read -s -n1 -p "Pulse para finalizar..."
  29. rm -rf $AUXFICH &> /dev/null
  30. clear
  31. exit
  32. }
  33.  
  34. function ayuda(){
  35. clear
  36. echoVerde Sintaxis:
  37. echo "$0 '[ --KO | --OK ]'"
  38. echo -e "--KO\tDeshabilita las interfaces de red permanentemente"
  39. echo -e "--OK\tHabilita las interfaces de red de forma permanente"
  40. echo "Sin parámetros se mostrará esta ayuda"
  41. read -n1 -sp "Pulse para finalizar..."
  42. clear
  43. rm -rf $AUXFICH &> /dev/null
  44. exit
  45. }
  46.  
  47. function tumba(){
  48. clear
  49. for NIC in $*
  50.     do
  51.     more $NETDIR/$NIC | grep -iv ^ONBOOT > $AUXFICH
  52.     echo "ONBOOT=no" >> $AUXFICH
  53.     mv -f $AUXFICH $NETDIR/$NIC 2> /dev/null
  54.     [ $? -eq 0 ] || fallo "Se produjo un error con $NETDIR/$NIC"
  55.     echo -ne "`echo $NIC | cut -d- -f2`\t"
  56.     echoRojo Deshabilitada
  57. done
  58. }
  59.  
  60. function levanta(){
  61. clear
  62. for NIC in $*
  63.     do
  64.     more $NETDIR/$NIC | grep -iv ^ONBOOT > $AUXFICH
  65.     echo "ONBOOT=yes" >> $AUXFICH
  66.     mv -f $AUXFICH $NETDIR/$NIC 2> /dev/null
  67.     [ $? -eq 0 ] || fallo "Se produjo un error con $NETDIR/$NIC"
  68.     echo -ne "`echo $NIC | cut -d- -f2`\t"
  69.     echoVerde Habilitada
  70. done
  71. }
  72. # </Funciones>
  73.  
  74. # <Comprobaciones>
  75. #Se tiene que ejecutar con no más de 1 parámetro
  76. [ $# -le 1 ] || fallo "Sintaxis incorrecta"
  77. #Debe existir el directorio de configuración de la red
  78. [ -d $NETDIR ] || fallo "No existe el directorio $NETDIR"
  79. #Se debe ejecutar con privilegios administrativos
  80. [ $UID -eq 0 ] || fallo "Debe ejecutarse como administrador"
  81. # </Comprobaciones>
  82.  
  83. #Según sea el primer parámetro se le da un valor a la variable OPCION
  84. OPCION=""
  85. case $1 in
  86.     --KO | --ko )
  87.     OPCION="K";;
  88.     --OK | --ok )
  89.     OPCION="S";;
  90.     * )
  91.     ayuda;;
  92. esac
  93. # Obtener las interfaces configuradas
  94. IFACES=`ls -1 $NETDIR | grep ^ifcfg- | grep -v ifcfg-lo`
  95. numIFACES=`echo $IFACES | wc -w`
  96. # Si el número de interfaces es 0 lo indico y salgo
  97. [ $numIFACES -gt 0 ] || fallo "No se han encontrado interfaces para configurar"
  98.  
  99. if [ "$OPCION" = "K" ]
  100.     then
  101.     tumba $IFACES
  102.     service network restart
  103. else
  104.     levanta $IFACES
  105.     service network restart
  106. fi
  107. echo -e "\n\n"
  108. read -n1 -sp "Pulse para finalizar.."
  109. rm -rf $AUXFICH 2> /dev/null
  110. clear
  111. exit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement