Skip to content

Instantly share code, notes, and snippets.

@robsonalexandre
Last active July 19, 2018 13:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robsonalexandre/78da59e43145b50726f3ff41c22a72c3 to your computer and use it in GitHub Desktop.
Save robsonalexandre/78da59e43145b50726f3ff41c22a72c3 to your computer and use it in GitHub Desktop.
Realiza a comparação de hash md5 de sites para verificar se houve alteração de seu conteúdo
#!/bin/bash
#
# Utilize o script para verificar o hash md5 de sites realizando o dump
# com wget e comparando seus hashes previamente cadastrados em um arquivo de hashes
#
# Conteúdo do arquivo de hashes deve seguir o padrão
# url;hash_md5
# http://localhost/blog;9ad421244b28db9cb4a6857edd297ef5
# http://localhost/admin;ad421244b28db9cb4a6857edd297ef53
#
# Para gerar a tabela de hashes use
# urls=(
# http://localhost
# https://localhost/admin
# http://localhost/blog
# )
# for url in ${urls[@]}; do md5=( $(wget -qO- $url | md5sum) ); echo "$url;$md5"; done > hash_file
#
function check_hash() {
local url hash
url=$1
hash=$2
md5=( $(wget -qO- $url | md5sum) )
if [[ $md5 == $hash ]]; then
MSG_OK+=( "$url: Hash md5 Ok" )
else
MSG_ERROR+=( "$url: Hash md5 não compativel" )
fi
}
while getopts ":f:m:u:" opt; do
case $opt in
m) hash_md5=$OPTARG;;
u) url=$OPTARG;;
f) hash_file=$OPTARG;;
\?) echo "Use: $0 [-f arquivo_hashes.txt] [-u http://www.site.com.br/diretorio] [-m hash_md5]"; return 1;;
:) echo "Opção -$OPTARG requer parâmetro."; return 1;;
esac
done
shift $(($OPTIND - 1))
if [[ -v "hash_file" && -s "$hash_file" ]]; then
while IFS=';' read url hash; do
check_hash "$url" "$hash"
done < $hash_file
elif [[ -v "url" && -v "hash_md5" ]]; then
check_hash "$url" "$hash_md5"
fi
if [[ -v "MSG_ERROR" ]]; then
MSG=${MSG_ERROR[@]}
EXIT_CODE=2
else
MSG=${MSG_OK[@]}
EXIT_CODE=0
fi
echo $MSG
exit $EXIT_CODE
# Predefined exit codes for Nagios/NetSaint
# UNKNOWN = -1
# OK = 0
# WARNING = 1
# CRITICAL = 2
#!/bin/bash
#
#
#
while getopts ":d:o:" opt; do
case $opt in
d) dir=$OPTARG;;
o) output=$OPTARG;;
\?) echo "Use: $0 [-d diretorio] [-o lista-de-hashes.txt]"; return 1;;
:) echo "Opção -$OPTARG requer parâmetro."; return 1;;
esac
done
shift $(($OPTIND - 1))
urls=(
http://localhost
https://localhost/admin
http://localhost/blog
)
for url in ${urls[@]}; do md5=( $(wget -qO- $url | md5sum) ); echo "$url;$md5"; done > $output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment