Shell Script para SQUID (Gerenciador)
Autor: Matheus Leão | Publicado: 2012-11-08T13:43:00-02:00

Pessoal, fiz um script para adicionar (escrever) nos arquivos de configuração do squid utilizado interface gráfica o "dialog", então para que vocês possam usar esse script deve seguir os seguintes passos.

# 1º
# Se estiver usando debian ou ubuntu
apt-get install dialog&nbsp;

# 2º
# Alterar os arquivos de configuração do script de acordo com as suas acl's do squid.


liberados="/etc/squid3/ips_liberados"
senhas="/etc/squid3/senhas_usuarios"
solitaria="/etc/squid3/solitaria"

# 3º
# Copiar os script abaixo e salvar no dir do squid de preferência e dar permissão de&nbsp;execução ao mesmo.

chmod +x squid.sh

# 4º 

./squid.sh

_______________________________________________________________________
# Obs: A função&nbsp;ValidaIP() não é interpretada por todas as versões do bash, se a sua versão não conseguir interpreta-la é só remover a função.




#!/bin/bash


###########ARQUIVOS E DIRETÓRIOS############

liberados="/etc/squid3/ips_liberados"
senhas="/etc/squid3/senhas_usuarios"
solitaria="/etc/squid3/solitaria"

while [ 1 ]
do

ValidaIP() {
&nbsp; &nbsp;local numero_ip=$1

&nbsp; &nbsp;# Suporte 'built-in' a ER's! legal... =)
&nbsp; &nbsp;[[ $numero_ip =~ ^[0-9]{2,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]] || return 1
&nbsp; &nbsp;
&nbsp; &nbsp;local primeiro_octeto segundo_octeto terceiro_octeto quarto_octeto
&nbsp; &nbsp;
&nbsp; &nbsp;OLD_IFS=$IFS
&nbsp; &nbsp;IFS="."
&nbsp; &nbsp;
&nbsp; &nbsp;set - $numero_ip
&nbsp; &nbsp;primeiro_octeto=$1
&nbsp; &nbsp;segundo_octeto=$2
&nbsp; &nbsp;terceiro_octeto=$3
&nbsp; &nbsp;quarto_octeto=$4
&nbsp; &nbsp;
&nbsp; &nbsp;IFS=$OLD_IFS
&nbsp; &nbsp;
&nbsp; &nbsp;# Checa o 2o octeto especifico de cada faixa
&nbsp; &nbsp;case $primeiro_octeto in
&nbsp; &nbsp; &nbsp; 10) &nbsp;[[ $segundo_octeto =~ ^0[0-9][0-9]? ]] &amp;&amp; return 1 # Invalida tipos '0x' ou '0xx'
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(( segundo_octeto &gt;= 0 &amp;&amp; segundo_octeto &lt;= 255 )) || return 1 ;;
&nbsp; &nbsp; &nbsp; 172) (( segundo_octeto &gt;= 16 &amp;&amp; segundo_octeto &lt;= 31 )) || return 1 ;;
&nbsp; &nbsp; &nbsp; 192) (( segundo_octeto == 168 )) || return 1 ;;
&nbsp; &nbsp; &nbsp; *) return 1
&nbsp; &nbsp;esac
&nbsp; &nbsp;
&nbsp; &nbsp;# Ja que o 3o e 4o octetos sao comuns as tres faixas
&nbsp; &nbsp;[[ $terceiro_octeto =~ ^0[0-9][0-9]? ]] || [[ $quarto_octeto =~ ^0[0-9][0-9]? ]] &amp;&amp; return 1
&nbsp; &nbsp;(( terceiro_octeto &gt;= 0 &amp;&amp; terceiro_octeto &lt;= 255 )) || return 1
&nbsp; &nbsp;(( quarto_octeto &gt;= 0 &amp;&amp; quarto_octeto &lt;= 255 )) || return 1

}


##### FUNÇÃO PEGAR IP#########
ip (){

IP=$( dialog &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \
&nbsp; &nbsp;--title 'IP' &nbsp; --stdout &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \
&nbsp; &nbsp;--inputbox 'Por favor, Informe o IP:' &nbsp;\
&nbsp; &nbsp;0 0 )
}

##### FUNÇÃO PEGAR SENHA#########
senha (){

senha=$( dialog &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \
&nbsp; &nbsp;--title 'SENHA' &nbsp; --stdout &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \
&nbsp; &nbsp;--passwordbox 'Por favor, informa a senha:' &nbsp;\
&nbsp; &nbsp;0 0 )
}

##### FUNÇÃO PEGAR NOME#########
nome (){

NOME=$( dialog &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \
&nbsp; &nbsp;--title 'IP' &nbsp; --stdout &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \
&nbsp; &nbsp;--inputbox 'Por favor, o nome da pessoa:' &nbsp;\
&nbsp; &nbsp;0 0 )
}

##### FUNÇÃO MOSTRAR MSG#########
erro_validacao(){

erro=$( dialog &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\
&nbsp; &nbsp;--title 'ERRO' &nbsp;--stdout &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\
&nbsp; &nbsp;--msgbox 'Erro na validação de IP ou esse IP já existe no arquivo e IPs!' &nbsp;\
&nbsp; &nbsp;6 40 )
}


##### FUNÇÃO MOSTRAR MSG#########
msg(){

MSG=$( dialog &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\
&nbsp; &nbsp;--title 'Parabéns' &nbsp;--stdout &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\
&nbsp; &nbsp;--msgbox 'Tarefa executada com sucesso!' &nbsp;\
&nbsp; &nbsp;6 40 )
}

##### FUNÇÃO SAIR#########
sair(){

sair=$( dialog &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\
&nbsp; &nbsp;--title 'SAIR' &nbsp;--stdout &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\
&nbsp; &nbsp;--msgbox 'ATÉ A PRÓXIMA CARA :D!' &nbsp;\
&nbsp; &nbsp;6 40 )
}

##### FUNÇÃO MENU#########
MENU=$( dialog &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \
&nbsp; &nbsp;--title 'SQUID3' &nbsp;--stdout &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\
&nbsp; &nbsp;--menu 'O que deseja:' &nbsp;\
&nbsp; &nbsp;0 0 0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \
&nbsp; &nbsp;LIBERAR &nbsp; &nbsp; &nbsp; 'Liberar IP' &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \
&nbsp; &nbsp;SOLITARIA &nbsp; &nbsp; 'Mandar para SOLITÁRIA' &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \
&nbsp; &nbsp;AUTENTICACAO &nbsp;'Criar senha para AUTH'  \
&nbsp; &nbsp;SAIR  'SAIR DO SQUID' )

##### CASE #########
case $MENU in

LIBERAR) ip
  if ValidaIP $IP
  then
  if ! cat $liberados | grep $IP
  then
   nome
   echo "$IP # $NOME" &gt;&gt; $liberados
   msg
  else
   erro_validacao

  fi
 else
  erro_validacao
 fi
 
 ;;
SOLITARIA)  ip 
  if ValidaIP $IP
   then
   if ! cat $solitaria | grep $IP
   then
    nome
    echo "$IP # $NOME" &gt;&gt; $solitaria
    msg
   else
    erro_validacao

   fi
  else
   erro_validacao
  fi
  ;;
AUTENTICACAO) nome
  &nbsp; &nbsp; &nbsp;htpasswd $senhas $NOME
  
 ;;

SAIR) sair
 break
 ;;

*)echo " Opção Inválida !" ;;

esac

done