srv-stage/templates/scripts/testSrv.sh.j2
2025-01-19 20:23:14 +01:00

283 lines
7.7 KiB
Django/Jinja
Executable File

#!/bin/bash
############################################################
# Decription: Test server stage
# Author: Gilles Mouchet (gmo@ville-ge.ch)
# Creation Date: 20-Jun-2018
# Version: 1.1
# Usage: ./sendWelcomeMail.sh
# Changelog:
#
# V1.2 - 18-Dec-2023 - GMo
# Added
# - Test chat-box
#
# V1.1 - 01-Dec-2021 - GMo
# Added
# - Create array with jinja template
# V1.0 - 20-Jun-2018 - GMo
# Added
# - Creation of script from scratch
#
############################################################
# Variables
srv_stage=srv-stage.stage-ge.org
srv_stage_shortname=`echo $srv_stage | cut -d . -f1`
srv_ldap=ldap.stage-ge.org
# ldap
#declare -A ldap_user_to_test=(["Stagiaire Un"]="password" ["Stagiaire Deux"]="password" )
declare -A ldap_user_to_test=({% for key, value in stagiaires.items() %}["{{ value.name }} {{ value.firstname }}"]="{{ plain_user_passwd }}" {% endfor %})
#declare -A sasauthd_user=(["mailadmin"]="password" \
#["stage1"]="password" \
#["stage2"]="password")
declare -A sasauthd_user=(["mailadmin"]="{{ plain_mailadmin_passwd }}" \
{% for key, value in stagiaires.items() %}["{{ key }}"]="{{ plain_user_passwd }}" \
{% endfor %})
#["site_to_test"]="affichage_testé_par_grep"
#declare -A sites_to_test=["webmail.stage-ge.org"]="Roundcube Webmail Connexion" ["http://www.stage-ge.org/lam/templates/login.php"]="LDAP Account Manager" \
#["www.stage-ge.org/stage1/"]="Index of /stage1" \
#["www.stage-ge.org/stage2/"]="Index of /stage2")
declare -A sites_to_test=(["webmail.stage-ge.org"]="Roundcube Webmail Connexion" \
["http://www.stage-ge.org:8001"]="Stage informatique - Chat" \
["http://www.stage-ge.org/lam/templates/login.php"]="LDAP Account Manager" \
{% for key, value in stagiaires.items() %}["www.stage-ge.org/{{ key }}/"]="Index of /{{ key }}" \
{% endfor %})
# Couleurs
RED="\e[1;31m" # Test négatif
WHITE="\e[0m" # Ecriture normale
GREEN="\e[1;32m" # Test positif
YELLOW="\e[1;33m" # Impossible de réaliser le test
function header {
echo -n -e ${WHITE}$1
}
function testOk {
echo -n -e " ${WHITE}[${GREEN} OK ${WHITE}]\n"
}
function testNotOk {
echo -n -e " ${WHITE}[${RED} Not OK ${WHITE}]\n"
}
function testFailed {
echo -n -e " ${WHITE}[${YELLOW} Failed ${WHITE}]\n"
}
# Config Mutt
#cat << EOF > ~/.muttrc
#set smtp_url = "smtp://smtp.stage-ge.org"
#set from="Mail Admin <mailadmin@stage-ge.org>"
#EOF
# Message de test
cat << EOF > /tmp/msgtst
Bonjour,
**Test Test**
Bonne journée
EOF
# On test si le serveur est présent
# Ce test nous permet de vérifier que :
# 1. Nous sommes dans le même réseau que le serveur stage
# 2. Que le serveur est up
header "Server $srv_stage up"
ping -c 1 -w 1 $srv_stage > /dev/null 2>&1
if [ $? -ne 0 ] ; then
testNotOk
exit
else
testOk
fi
#On test le DNS et faisant des requêtes nslookup
header "Test DNS\n"
for i in "${domain_to_test[@]}"; do
header "\tTest DNS domain $i"
nslookup $i $srv_stage > /dev/null 2>&1
if [ $? -ne 0 ] ; then
testNotOk
else
testOk
fi
done
# Test le service LDAP en faisant une requête ldapsearch
header "Test LDAP\n"
for i in "${!ldap_user_to_test[@]}";do
header "\tTest l'accès ldap de l'utilisateur $i"
if [ "${i:0:9}" == "Stagiaire" ]; then
common_name="${i},ou=people"
else
common_name="${i}"
fi
ldapsearch -H ldap://${srv_ldap} -x -D "cn=${common_name},dc=stage-ge,dc=org" -b "dc=stage-ge,dc=org" CN=* -w ${ldap_user_to_test[${i}]} -LLL > /dev/null 2>&1
if [ $? -ne 0 ] ; then
testNotOk
else
testOk
fi
done | sort -r
#toto=1
#if [ "$toto" == "1" ]; then
# On test l'autentification avec saslauthd
header "Test de l'authentification\n"
for user in "${!sasauthd_user[@]}";do
# result=`ssh $srv_stage_shortname /usr/sbin/testsaslauthd -u $user -p ${sasauthd_user[$user]} 2>&-` #> /dev/null 2>&1
result=`/usr/sbin/testsaslauthd -u $user -p ${sasauthd_user[$user]} 2>&-` #> /dev/null 2>&1
#echo -e "\t$result"
case $result in
'0: OK "Success."')
header "\tConnection de l'utilisateur $user"
testOk
;;
'0: NO "authentication failed"')
header "\tConnection de l'utilisateur $user"
testNotOk
;;
*)
header "\tConnection SSH"
testFailed
;;
esac
done
#
#
# On test la messagerie en envoyant un mail à toutes les boîtes
header "Test d'envoi d'un mail\n"
for user in "${!sasauthd_user[@]}";do
header "\tEnvoi d'un message de test à $user"
mutt -s "Message de test" $user@stage-ge.org < /tmp/msgtst #> /dev/null 2>&1
if [ $? -ne 0 ] ; then
testNotOk
else
testOk
fi
done
# on attend que les messages soient délivrés
header "On attend 5 sec. que les messages soient délivrés\n"
sleep 5
#if [ "$toto" == "1" ]; then
# On vérifie que le message est bien arrivé dans la boîte
header "Test des mailbox\n"
for user in "${!sasauthd_user[@]}";do
header "\tTest mailbox $user"
#curl -s imap://imap.stage-ge.org -u stagiaire1:password -X 'examine inbox'
curl -s imap://imap.stage-ge.org -u $user:${sasauthd_user[$user]} -X 'examine inbox'| grep "1 EXISTS" > /dev/null 2>&1
if [ $? -ne 0 ] ; then
testNotOk
else
testOk
fi
done
#if [ "$toto" == "1" ]; then
# On supprime les messages des boîtes
header "Suppression des messages\n"
for user in "${!sasauthd_user[@]}";do
header "\tSuppression des messages de la mailbox de $user"
cat << EOF > delMsg.py
# https://stackoverflow.com/questions/3180891/imap-deleting-messages
import imaplib
box = imaplib.IMAP4('imap.stage-ge.org')
box.login("$user","${sasauthd_user[$user]}")
box.select('Inbox')
typ, data = box.search(None, 'ALL')
for num in data[0].split():
box.store(num, '+FLAGS', '\\Deleted')
box.expunge()
box.close()
box.logout()
EOF
python3 delMsg.py > /dev/null 2>&1
if [ $? -eq 0 ];then
testOk
else
testNotOk
fi
done
#
# On copie un fichier sur le home via ftp
# Création du fichier à copier
cat << EOF > index.html
<h1>Bonjour !</h1>
EOF
header "Test la copie d'un fichier via FTP\n"
for user in "${!sasauthd_user[@]}";do
if [ $user == "mailadmin" ]; then
flagNotUsed=0
else
header "\tCopie le fichier sur /home/$user"
cat << EOF > ftpcmd.txt
open ftp.stage-ge.org
user $user ${sasauthd_user[$user]}
put index.html index.html
bye
EOF
ftp -i -n > /dev/null 2>&1 1> /tmp/ftp.log < ftpcmd.txt
# ftp -n -v ftp.stage-ge.org << EOT
#user $user ${sasauthd_user[$user]}
#put index.html index.html
#bye
#EOT
rcFtp=$?
result=`cat /tmp/ftp.log`
if [ $rcFtp -ne 0 ] || [ ! -z "${result}" ] ; then
testNotOk
else
testOk
fi
fi
done
# On supprime le fichier distant
header "Test la suppression d'un fichier via FTP\n"
for user in "${!sasauthd_user[@]}";do
if [ $user == "mailadmin" ]; then
flagNotUsed=0
else
header "\tSuppression du fichier sur /home/$user"
cat << EOF > ftpcmd.txt
open ftp.stage-ge.org
user $user ${sasauthd_user[$user]}
delete index.html
bye
EOF
ftp -i -n > /dev/null 2>&1 1> /tmp/ftp.log < ftpcmd.txt
rcFtp=$?
result=`cat /tmp/ftp.log`
if [ $rcFtp -ne 0 ] || [ ! -z "${result}" ] ; then
testNotOk
else
testOk
fi
fi
done
# On test les sites
header "Test des sites web\n"
for site in "${!sites_to_test[@]}";do
header "\tTest du site $site"
curl -s "$site" | grep "${sites_to_test[$site]}" > /dev/null
if [ $? -eq 0 ];then
testOk
else
testNotOk
fi
done
# On supprime les scritps créés
header "Delete tempo scripts"
rm -rf ftpcmd.txt delMsg.py
if [ $? -eq 0 ];then
testOk
else
testNotOk
fi