Précédent   Forum des professionnels en informatique > Systèmes > Linux > Contribuez
Contribuez Vos contributions pour la rubrique Linux : articles, cours, tutoriels, faq, comparatifs, tests, sources, ...
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse Proposer ce sujet en actualité
 
Outils de la discussion
Publicité
Vieux 09/08/2007, 08h09   #1
Invité de passage
 
Inscription : août 2007
Messages : 2
Détails du profil
Informations forums :
Inscription : août 2007
Messages : 2
Points : 0
Points : 0
Par défaut creation de folders, attributions de quotas sur NAS - DELL EMC

salut à tous,
je suis autodidacte du bash (grace à internet et aux forums comme celui ci), et j'ai créé un script permettant la création automatique de folders sur un NAS EMC.
Chaque folder est identifié par son id
Ces folders sont ensuite partagés sous conditions (xcacls) dans un environnement MSAD via un script VB

Comme ce produit utilise une console de gestion sous linux, c'est le pied pour développer/bidouiller
donc je me suis simplifié la vie, et peut etre cela sera utile à d'autres.
Les commentaires peuvent etre en anglais ou francais (selon mon humeur du moment), les "echo" sont tous en anglais (environnement international oblige)

ATTENTION ! comme dit le proverbe, il y a toujours d'autres manières de faire, et je suis persuadé que ma manière n'est pas la plus optimale. Encore une fois, je suis newbie dans le dev
Ce script s'appuie sur la seule commande 'nas_quotas', dont la syntaxe n'est pas des plus aisée.

A ma connaissance, cette commande n'est disponible que sur les équipements Dell EMC.
Les outils linux utilisés sont : for, while, if, case, cat, grep, tr, sed, awk, wc, echo, tail, head, read, date.

trève de bavardages, voici la "bête" , pondu en 7 jours ouvrés

Citation:
#!/bin/bash
version="1.0.0813"
echo " *** nas_quotas made easy version $version - by me *** "
echo
USAGE () # Description des arguments - affichage lors de l'utilisation de listes
{
echo -e " --------------------------------------------------------------------------------------------------------------\n Usage : $ ./easyquota.bash [--add | -a] 'input_file' [--quota | -q] 'quota' [--filesystem | -fs] 'filesystem'\n\n Adds 'input_file' list to 'filesystem' with specified 'quota'\n 'input_file' must have only one name per line. No special characters is allowed\n 'quota' is the lower limit in MB\n 'filesystem' is the filesystem where the quota must be set : could be [IDrives_Adm | A] , [IDrives_Fac | F] , [store_ata_1 | S1] , [store_ata_2 | S2] , [store_ata_3 | S3] , [Promo_1_3 | P13] , [Promo_2_4 | P24] , [Test | T]\n\n if 'filesystem' is not specified , the program will ask for it\n if 'quota' is not specified, the program will ask for it\n folders are created in CAPITAL case\n if no option is specified, first argument is considered as 'input_file'\n"
}

TEST_ARGS () # teste les arguments de la ligne de commande
{
if [ -f "$1" ]; then list=$1; fi
while [ $# -gt 0 ]; do
case "$1" in
"-a"|"-add"|"--add")
list=$2
;;
"-q"|"-quota"|"--quota")
allquota=$2
;;
"-fs"|"-FS"|"--filesystem")
fs=$2
;;
esac
shift
done
# Test if file exists
if [ -f "$list" ]
then echo " $list exists : Ok"
num=`cat $list | wc | awk {'print $1'}`
echo
echo " Processing $num shares"
else echo " file $list does not exist"; USAGE; exit 2
fi
# Test if quota is specified AND is an integer
if [ "$allquota" = "" ]
then ASKQUOTA
else echo $allquota | grep [^0-9] > /dev/null 2>&1
if [ "$?" -eq "0" ]
then echo "'quota' $allquota is not valid"
ASKQUOTA
fi
fi
# Test if filesystem is specified
menufs=$fs
if [ "$menufs" = "" ]; then echo "Please select filesystem :"
FS_MENU
else TEST_FS
fi
}

FILE_TEST () # testing file 'liste' : 1 name per line, no special character
{
error1=0
error2=0
numline=0
if [ -f /tmp/err ]; then rm /tmp/err; fi
if [ -f /tmp/err2 ]; then rm /tmp/err2; fi
echo "file is $list"
echo "testing $list for special characters"
badchar=`grep [^a-Z0-9_+-] $list`
while [ "$numline" != "$num" ]; do
numline=$(($numline+1))
charline=`head -n $numline $list | tail -n 1`
numword=`echo $charline | wc | awk {'print $2'}`
badname=`echo $charline | grep [^a-Z0-9_+-]`
if [ "$numword" != "1" ]
then echo "Error in file \"$list\" : $numword names found on line $numline"
echo "$numline" >> /tmp/err
error1=$(($error1+1))
fi
if [ "$badname" != "" ]
then echo "Invalid character found in file \"$list\" on line $numline : $badname"
echo "$numline" >> /tmp/err2
error2=$(($error2+1))
else echo "line $numline Ok"
fi
done
DISPLAY_ERRORS
}

DISPLAY_ERRORS () # affiche les erreurs par ligne du fichier 'liste'
{
echo
if [ "$error1" != "0" ]; then
echo "Error1 : $error1 line found empty or with multiple names"
echo "Press Enter to show errors"
read
for i in `cat /tmp/err`; do
echo "line $i : `cat $list | head -n $i |tail -n 1`"
done
echo
else echo "No multiple names found in file $list"
fi
if [ "$error2" != "0" ]; then
echo "Error2 : $error2 line(s) found with bad characters"
echo "Press Enter to show errors"
read
for j in `cat /tmp/err2`; do
echo "line $j : `cat $list | head -n $j |tail -n 1`"
done
echo
else echo "No bad character found in file $list"
fi
if [ "$error1" = "0" ]
then if [ "$error2" = "0" ]
then echo "No error found in $list. Going on with share creation"
else echo "Aborting"; exit 3
fi
else echo -e "\nPlease correct errors"; exit 3
fi
}

TEST_FS ()
{
menufs=`echo $menufs | tr a-z A-Z`
case "$menufs" in
"T" | "TEST" )
fileSystem=promo_1_3
storepath="/Promo_test/"
;;
"P24" | "PROMO_2_4" )
fileSystem=promo_2_4
while [ "$storepath" = "" ]; do
echo -e "\nPlease select path for $fileSystem : Promo_[2] , Promo_[4] :"
read storepath
storepath=`echo $storepath | tr a-z A-Z`
case "$storepath" in
"2")
storepath="/Promo_2/"
;;
"4")
storepath="/Promo_4/"
;;
* )
echo -e " Invalid path\n"
storepath=
;;
esac
done
;;
"P13" | "PROMO_1_3" )
fileSystem=promo_1_3
while [ "$storepath" = "" ]; do
echo -e "\nPlease select path for $fileSystem : Promo_[1] , Promo_[3] , Promo_[T]est :"
read storepath
storepath=`echo $storepath | tr a-z A-Z`
case "$storepath" in
"1")
storepath="/Promo_1/"
;;
"3")
storepath="/Promo_3/"
;;
"t" | "T" )
storepath="/Promo_test/"
;;
* )
echo -e " Invalid path\n"
storepath=
;;
esac
done
;;
"A" | "IDRIVES_ADM" )
fileSystem=IDrives_Adm
storepath="/IDrives_Adm/"
;;
"F" | "IDRIVES_FAC" )
fileSystem=IDrives_Fac
storepath="/IDrives_Fac/"
;;
"S1" | "STORE_ATA_1" )
fileSystem=store_ata_1
storepath="/Shares/"
;;
"S2" | "STORE_ATA_2" )
fileSystem=store_ata_2
storepath="/Share_Adm/"
;;
"S3" | "STORE_ATA_3" )
fileSystem=store_ata_3
while [ "$storepath" = "" ]; do
echo -e "\nPlease select path for $fileSystem : [K]iosk_MBA , [M]ix_Share_MBA , [P]romo_0 , [u]sr_edp :"
read storepath
storepath=`echo $storepath | tr a-z A-Z`
case "$storepath" in
"K")
storepath="/Kiosk_MBA/"
;;
"M")
storepath="/Mix_Share_MBA/"
;;
"P")
storepath="/Promo_0/"
;;
"U")
storepath="/usr_edp/"
;;
* )
echo -e " Invalid path\n"
storepath=
;;
esac
done
;;
"Q" | "q" )
echo -e "\n Bye\n"
exit 0
;;
* )
echo -e " \"$menufs\" not available\n"
menufs=
;;
esac
if [ "$storepath" = "" ]
then echo "Please select filesystem :"
else echo " Shares will be created on '$fileSystem' with path '$storepath'"
fi
}

FS_MENU () # Menu de selection du filesystem +fileSystem +path
{
path=
fileSystem=
menufs=
while [ "$menufs" = "" ]; do
echo -e "\n Available fileSystems\n ---------------------\n\n [A] IDrives_Adm\n [F] IDrives_Fac\n [S1] store_ata_1\n [S2] store_ata_2\n [S3] store_ata_3\n [P13] promo_1_3\n [P24] promo_2_4\n -------------------\n [Q] Quit"
read menufs
TEST_FS
done
echo
}

FINDID () # Recherche de l'ID +id ; +dispsh ; +nshare
{
if [ -f "/tmp/idtmp" ]
then rm "/tmp/idtmp"
fi
cmd=`nas_quotas -list -tree -fs $fileSystem | grep -i "$storepath$i " > /tmp/idtmp`
id=`cat /tmp/idtmp | awk {'print $2'}`
showsh=`cat /tmp/idtmp | awk {'print $4'} | sed 's/^\(.*\)\///'`
dispsh=`cat /tmp/idtmp | awk {'print $4'}`
}

ASKQUOTA ()
{
quota=$allquota
while [ "$minsize" = "" ]; do
echo -e "\n Enter quota soft limit in MB (HARD limit will be 10% more)"
read quota
if [ "$quota" != "" ]
then
minsize=$(($quota * 1024))
maxsize=$(($minsize + ($minsize * 10 / 100) ))
# check if quota is not equal to 0
if [ "$minsize" -lt "1024" ]
then echo -e " Wrong size\n"
quota=
fi
fi
echo " quota HARD limit size is $maxsize kB"
echo -e " quota soft limit size is $minsize kB\n"
done
}

ADDQUOTA ()
{
cancel=
nquota=`cat $list | wc | awk {'print $2'}`
if [ "$nquota" -le "1" ]
then i=$sharelist
fi
if [ "sharelist" = "" ] ; then sharelist=$share; fi
for i in $sharelist; do
echo -e "\n ----------------------------------------------------------"
echo " Making sure \"$i\" does not exist on $fileSystem$storepath"
FINDID
for j in $showsh; do
if [ `echo $i | tr a-z A-Z` = `echo $j | tr a-z A-Z` ]
then echo "'$i' already exists on $fileSystem as '$j'"
echo " Aborting"
doublon=$(($doublon+1))
cancel=1
echo "$i" >> /tmp/errorlist
else echo " Ok"
fi
done
if [ "$cancel" != "1" ]
then echo "Creating $storepath$i on $fileSystem"
nas_quotas -on -tree -fs $fileSystem -path `echo $storepath$i`
FINDID
id=`grep "$i " /tmp/idtmp | awk {'print $2'}`
DOQUOTA
echo "$i" >> /tmp/donelist
echo " $i created on $fileSystem with ID $id and a quota soft limit of $minsize kB"
fi
cancel=0
done
}

DOQUOTA () # Application de nouveaux quotas - PAUSE
{
ASKQUOTA
nas_quotas -edit -tree -fs $fileSystem -block $maxsize:$minsize $id
}

# START
if [ -f /tmp/errorlist ]; then rm /tmp/errorlist; fi
if [ -f /tmp/donelist ]; then rm /tmp/donelist; fi
doublon=0
nbargs=$#
if [ "$#" = "0" ]
then USAGE
else echo "Testing arguments"
TEST_ARGS $@
echo "Testing input list file"
FILE_TEST
echo "Setting '$list' entries in CAPITAL case"
#cat $list |tr a-z A-Z > /tmp/listtmp
#sharelist=`cat /tmp/listtmp`
sharelist=`cat $list |tr a-z A-Z`
echo -e "done\n"
ADDQUOTA
DOQUOTA
echo -e "\n Number of doublon(s) found : $doublon"
# Logging errors and creation in files
echo >> /home/nasadmin/donelist.log
date >> /home/nasadmin/donelist.log
cat /tmp/donelist >> /home/nasadmin/donelist.log
echo >> /home/nasadmin/errorlist.log
date >> /home/nasadmin/errorlist.log
cat /tmp/errorlist >> /home/nasadmin/errorlist.log
echo " Log files located in /home/nasadmin/donelist.log and /home/nasadmin/errorlist.log"
fi
echo -e "\n Bye\n"
Commentaire, suggestions, critiques, remerciements et moqueries acceptés et bienvenus

mcbzh


edit 14/08: echo -e "..\n.." , pour les sauts de lignes; $sharelist défini directement ; "date >> ..." au lieu de "echo `date` >> ..."
... ò0 ...

Dernière modification par mcbreizh ; 14/08/2007 à 19h35.
mcbreizh est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 11/08/2007, 20h38   #2
Rédacteur
 
Avatar de Arnaud F.
 
Homme Arnaud Feltz
Développeur .NET
Inscription : août 2005
Messages : 5 213
Détails du profil
Informations personnelles :
Nom : Homme Arnaud Feltz
Âge : 24
Localisation : France

Informations professionnelles :
Activité : Développeur .NET
Secteur : Transports

Informations forums :
Inscription : août 2005
Messages : 5 213
Points : 6 122
Points : 6 122
Une critique, tous les echo fait pour faire sauter des lignes comme :

Code :
1
2
 echo "    folders are created in CAPITAL case"
echo
Se fait en une seule et même ligne :
Code :
 echo -e     " folders are created in CAPITAL case\n"
Y donc de quoi alléger le script

De plus, le echo est inutile ici :
Code :
         echo `date` >> /home/nasadmin/donelist.log
devient :
Code :
         date >> /home/nasadmin/donelist.log
Pourquoi :
Code :
1
2
         cat $list |tr a-z A-Z > /tmp/listtmp
        sharelist=`cat /tmp/listtmp`
et pas :
Code :
         sharelist=`cat $list |tr a-z A-Z`
sachant que listtmp n'est plus utilisé autre part?

++
__________________
C'est par l'adresse que vaut le bûcheron, bien plus que par la force. Homère

Installation de Code::Blocks sous Debian à partir de Nightly Builds
Arnaud F. est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 13/08/2007, 10h39   #3
Invité de passage
 
Inscription : août 2007
Messages : 2
Détails du profil
Informations forums :
Inscription : août 2007
Messages : 2
Points : 0
Points : 0
Salut buchs, et merci pour tes conseils

- j'ai modifié mes "echo" en "echo -e" quand c'est possible, je gagne déjà quelques lignes
- idem pour les "echo `date`", on gagne une commande à chaque fois
- j'ai aussi modifié le $sharelist ; je saurai pas dire pourquoi j'ai voulu passer par un fichier temp à ce niveau... peut etre lors des premiers debug afin de garder une trace

Pour la lisibilité du fofo, est que je repost le script ou bien j'edite mon 1er message ?
Merci

mybzh
mcbreizh est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 13/08/2007, 22h04   #4
Rédacteur
 
Avatar de Arnaud F.
 
Homme Arnaud Feltz
Développeur .NET
Inscription : août 2005
Messages : 5 213
Détails du profil
Informations personnelles :
Nom : Homme Arnaud Feltz
Âge : 24
Localisation : France

Informations professionnelles :
Activité : Développeur .NET
Secteur : Transports

Informations forums :
Inscription : août 2005
Messages : 5 213
Points : 6 122
Points : 6 122
Edite le premier
__________________
C'est par l'adresse que vaut le bûcheron, bien plus que par la force. Homère

Installation de Code::Blocks sous Debian à partir de Nightly Builds
Arnaud F. est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse Proposer ce sujet en actualité
Outils de la discussion



Fuseau horaire GMT +1. Il est actuellement 03h00.


 
 
 
 
Partenaires

Hébergement Web