check-virtualhosts 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #!/bin/bash
  2. sendEmail() {
  3. # sendEmail $to $from $pass $subject $host $port $message
  4. if [ $# -ne 7 ]; then
  5. echo -en "ERROR:\tError interno en sendEmail.\n"
  6. exit 1
  7. fi
  8. to=$1
  9. shift
  10. from=$1
  11. shift
  12. pass=$1
  13. shift
  14. subject=$1
  15. shift
  16. host=$1
  17. shift
  18. port=$1
  19. shift
  20. message=$@
  21. echo -en "$message" | mailx -s "$subject" -S smtp-use-starttls -S smtp-auth=login -S smtp=smtp://$host:$port -S from="$from" -S smtp-auth-user="$from" -S smtp-auth-password="$pass" $to >> $logFile 2>&1
  22. unset to from pass subject host port message
  23. }
  24. getVirtualHosts() {
  25. # getVirtualHost $virtualHostPath
  26. cont=1
  27. for virtualHost in $(find -L "$1" -type f);do
  28. grep ^[[:space:]]*auth_basic "$virtualHost" > /dev/null 2>&1
  29. if [ $? -eq 0 ];then
  30. # Si tiene la directiva "auth_basic" activada no lo comprobamos
  31. continue
  32. fi
  33. linea=$(grep ^[[:space:]]*server_name "$virtualHost")
  34. if [ $? -ne 0 ];then
  35. # Si no tiene la directiva "server_name" no lo comprobamos
  36. continue
  37. fi
  38. for hostname in $linea;do
  39. if [ $hostname != "server_name" ] && [ $hostname != "localhost" ];then
  40. hostnames[$cont]=$(echo "$hostname" | sed 's/;//g')
  41. cont=$((cont+1))
  42. fi
  43. done
  44. done
  45. # Eliminar repetidos
  46. hostnames=($(echo "${hostnames[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' '))
  47. echo -en "Hostnames: ""${hostnames[@]}""\n"
  48. unset linea cont hostname
  49. }
  50. # Variables iniciales
  51. logFile="."$(basename $0)".log"
  52. hostnames=""
  53. getVirtualHosts "/etc/nginx/sites-enabled/"