você está aqui: Home  → Arquivo de Mensagens

Criando md5sum sem perder tempo

Colaboração: Jansen Sena

Data de Publicação: 14 de Outubro de 2006

Em muitas oportunidades, administradores de servidores FTP e HTTP que dispobinilizam arquivos para download, necessitam disponibilizar o hash MD5 de cada arquivo disponibilizado para download. Esse e uma alternativa simples e interessante que permite com que o usuario verifique, apos finalizar o download, se o arquivo copiado esta integro e de acordo com o original disponibilizado no servidor.

Entretanto, apos criar um novo diretorio com varios arquivos a serem disponibilizados, gerar o hash MD5 para cada um pode ser uma tarefa repetitiva. Mesmo sendo possível resolver esse problema com uma única linha de comando, combinando alguns "pipes" ou um simples laco "for" em shell script, essa saída nem sempre é tão simples fundamentalmente para administradores menos experientes e não tão ambientados com os recursos mais avançados da shell.

Para resolver esse problema, você pode utilizar um simples shell script que batizei de mkmd5sum4me.sh. Basta executá-lo passando como parâmetro os diretórios cujos arquivos devem ter os hashes MD5 criados. Em outras palavras, o script criará para cada arquivo de cada diretório repassado um arquivo com o mesmo nome do original adicionado da extensão ".md5sum" contendo o seu hash. Por exemplo:

  $ mkmd5sum4me.sh /var/www/downloads/distros /var/www/myfiles

mkmd5sum4me.sh

  #!/bin/bash
  #
  # 
  #Copyright Holder: Jansen Sena <jansen@comunidadesol.org>
  #
  #License:
  #
  #   This package is free software; you can redistribute it and/or modify
  #   it under the terms of the GNU General Public License as published by
  #   the Free Software Foundation; either version 2 of the License, or
  #   (at your option) any later version.
  #
  #   This package is distributed in the hope that it will be useful,
  #   but WITHOUT ANY WARRANTY; without even the implied warranty of
  #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  #   GNU General Public License for more details.
  #
  #   You should have received a copy of the GNU General Public License
  #   along with this package; if not, write to the Free Software
  #   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  #   02111-1307, USA.
  #
  # < A NAME="note" HREF="#textnote">< SUP>< A NAME="note" HREF="#textnote">< SUP>< A NAME="note" HREF="#textnote">< SUP>< A NAME="note" HREF="#textnote">< SUP>##
  #  System utilities
  # < A NAME="note" HREF="#textnote">< SUP>< A NAME="note" HREF="#textnote">< SUP>< A NAME="note" HREF="#textnote">< SUP>< A NAME="note" HREF="#textnote">< SUP>##
  #
  MD5SUM="/usr/bin/md5sum"
  LS="/bin/ls"
  ECHO="/bin/echo"
  ALL_UTILS="MD5SUM LS ECHO"
  #
  # < A NAME="note" HREF="#textnote">< SUP>< A NAME="note" HREF="#textnote">< SUP>< A NAME="note" HREF="#textnote">< SUP>#
  #  DEFINITIONS
  # < A NAME="note" HREF="#textnote">< SUP>< A NAME="note" HREF="#textnote">< SUP>< A NAME="note" HREF="#textnote">< SUP>#
  #
  MD5FILE_SUFFIX="md5sum"
  SYNTAX_MSG="Usage: mkmd5sum4me.sh <dir> [<dir> [<dir>...]]"
  #
  # < A NAME="note" HREF="#textnote">< SUP>< A NAME="note" HREF="#textnote">< SUP>< A NAME="note" HREF="#textnote">< SUP>< A NAME="note" HREF="#textnote">< SUP>< A NAME="note" HREF="#textnote">< SUP>< A NAME="note" HREF="#textnote">< SUP>< A NAME="note" HREF="#textnote">< SUP>< A NAME="note" HREF="#textnote">< SUP>###
  #  Checking the number of parameters
  # < A NAME="note" HREF="#textnote">< SUP>< A NAME="note" HREF="#textnote">< SUP>< A NAME="note" HREF="#textnote">< SUP>< A NAME="note" HREF="#textnote">< SUP>< A NAME="note" HREF="#textnote">< SUP>< A NAME="note" HREF="#textnote">< SUP>< A NAME="note" HREF="#textnote">< SUP>< A NAME="note" HREF="#textnote">< SUP>###
  #
  if [ $# -eq 0 ] ; then 
      $ECHO $SYNTAX_MSG
      exit 2
  fi
  #
  # < A NAME="note" HREF="#textnote">< SUP>< A NAME="note" HREF="#textnote">< SUP>< A NAME="note" HREF="#textnote">< SUP>< A NAME="note" HREF="#textnote">< SUP>< A NAME="note" HREF="#textnote">< SUP>< A NAME="note" HREF="#textnote">< SUP>< A NAME="note" HREF="#textnote">< SUP>< A NAME="note" HREF="#textnote">< SUP>< A NAME="note" HREF="#textnote">< SUP>< A NAME="note" HREF="#textnote">< SUP>< A NAME="note" HREF="#textnote">< SUP>< A NAME="note" HREF="#textnote">< SUP>< A NAME="note" HREF="#textnote">< SUP>< A NAME="note" HREF="#textnote">< SUP># 
  #  Checking if all the utilities aer present in the system
  # < A NAME="note" HREF="#textnote">< SUP>< A NAME="note" HREF="#textnote">< SUP>< A NAME="note" HREF="#textnote">< SUP>< A NAME="note" HREF="#textnote">< SUP>< A NAME="note" HREF="#textnote">< SUP>< A NAME="note" HREF="#textnote">< SUP>< A NAME="note" HREF="#textnote">< SUP>< A NAME="note" HREF="#textnote">< SUP>< A NAME="note" HREF="#textnote">< SUP>< A NAME="note" HREF="#textnote">< SUP>< A NAME="note" HREF="#textnote">< SUP>< A NAME="note" HREF="#textnote">< SUP>< A NAME="note" HREF="#textnote">< SUP>< A NAME="note" HREF="#textnote">< SUP>#
  #
  for util in $ALL_UTILS ; do 
      eval util_aux=\$$util
      if [ ! -x $util_aux ] ; then
  	$ECHO "The $util_aux file is not present in your system or it doesn't have execution permission."
  	$ECHO "Aborting."
  	exit 1
      fi
  done
  #
  # < A NAME="note" HREF="#textnote">< SUP>< A NAME="note" HREF="#textnote">< SUP>### 
  #  FUNCTIONS
  # < A NAME="note" HREF="#textnote">< SUP>< A NAME="note" HREF="#textnote">< SUP>###
  #
  #
  # This function check if a directory exists and if there is execute permission on it.
  # Syntax: CHECK_DIRECTORY <DIRECTORY PATH> <SHORT DIRECTORY TYPE DESCRIPTION>
  #
  function CHECK_DIRECTORY() {
      DIRECTORY=$1
      TYPE=$2
      if [ ! -d $DIRECTORY ] ; then
         $ECHO "The $DIRECTORY $TYPE directory does not exist or it is not a directory."
         $ECHO "Aborting."
         exit 1
      elif [ ! -x $DIRECTORY ] ; then
            $ECHO "The $DIRECTORY $TYPE directory does not have execute permission."
            $ECHO "Aborting."
            exit 1
      fi
  }
  #
  # < A NAME="note" HREF="#textnote">< SUP>< A NAME="note" HREF="#textnote">< SUP>< A NAME="note" HREF="#textnote">< SUP>< A NAME="note" HREF="#textnote">< SUP>< A NAME="note" HREF="#textnote">< SUP>##
  #  Checking directories
  # < A NAME="note" HREF="#textnote">< SUP>< A NAME="note" HREF="#textnote">< SUP>< A NAME="note" HREF="#textnote">< SUP>< A NAME="note" HREF="#textnote">< SUP>< A NAME="note" HREF="#textnote">< SUP>##
  #
  for dir in $* ; do 
      CHECK_DIRECTORY $dir ""
  done
  #
  # < A NAME="note" HREF="#textnote">< SUP>< A NAME="note" HREF="#textnote">< SUP>< A NAME="note" HREF="#textnote">< SUP>< A NAME="note" HREF="#textnote">< SUP>< A NAME="note" HREF="#textnote">< SUP>< A NAME="note" HREF="#textnote">< SUP>< A NAME="note" HREF="#textnote">< SUP>< A NAME="note" HREF="#textnote">< SUP>< A NAME="note" HREF="#textnote">< SUP>< A NAME="note" HREF="#textnote">< SUP>< A NAME="note" HREF="#textnote">< SUP>< A NAME="note" HREF="#textnote">< SUP>< A NAME="note" HREF="#textnote">< SUP>< A NAME="note" HREF="#textnote">< SUP>
  #  Calculting the md5 for all the files in each directory
  # < A NAME="note" HREF="#textnote">< SUP>< A NAME="note" HREF="#textnote">< SUP>< A NAME="note" HREF="#textnote">< SUP>< A NAME="note" HREF="#textnote">< SUP>< A NAME="note" HREF="#textnote">< SUP>< A NAME="note" HREF="#textnote">< SUP>< A NAME="note" HREF="#textnote">< SUP>< A NAME="note" HREF="#textnote">< SUP>< A NAME="note" HREF="#textnote">< SUP>< A NAME="note" HREF="#textnote">< SUP>< A NAME="note" HREF="#textnote">< SUP>< A NAME="note" HREF="#textnote">< SUP>< A NAME="note" HREF="#textnote">< SUP>< A NAME="note" HREF="#textnote">< SUP>
  #
  for dir in $* ; do 
      cd $dir
      FILES=`ls`
      #
      # To be solved: it will not work with there is some directory including white spaces.
      #
      $ECHO -n "Calculating MD5 sum for $dir directory..."
      for file in $FILES ; do 
  	$MD5SUM $file > $file.$MD5FILE_SUFFIX 2>/dev/null
  	if [ $? -ne 0 ] ; then 
  	    $ECHO "There was some problem during the md5 calculation for $file in $dir directory."
  	    $ECHO "Ignoring."
  	fi
      done    
      $ECHO "done."
  done

Bem, espero que seja útil algum dia.

Saudações livres!

Jansen Sena, Comunidade SOL Software Livre



Veja a relação completa dos artigos de Jansen Sena

 

 

Opinião dos Leitores

Seja o primeiro a comentar este artigo
*Nome:
Email:
Me notifique sobre novos comentários nessa página
Oculte meu email
*Texto:
 
  Para publicar seu comentário, digite o código contido na imagem acima
 


Powered by Scriptsmill Comments Script