Après avoir réalisé un tutoriel sur l'art de scripter ( https://www.jeuxvideo.com/forums/1-38-7670273-1-0-1-0-tutoriel-rediger-un-script.htm ), je propose qu'on regroupe sur ce topic les demandes/propositions de scripts.
Ce topic sera donc un complément du tutoriel sur lequel on trouvera des scripts "complets" qui pourront pour les plus simples d'entre eux servir d'illustration.
Je commence avec le script que j'ai rédigé pour mon stage de fin d'année.
Je doute que ce script vous serve à titre personnel (son but etant de contrôler le trafic sur sa machine à savoir, ajout de délai, perte de paquets, etc...), mais pour les plus motivés, ça fait pratiquement 1000 lignes à étudier ^^° .
SCRIPT_NAME=$( basename $0 );
wrong_param="
Wrong parameter.\n
You can call the program with those parameters:
\t -h(--help) \t\t Help menu.
\t -v(--version)\t\t Program version.
\t --about \t\t About the program.
";
help="
Nettool is a simple script writting to simplify the network configuration.
It was writting in the SRC at the Athlone Institute of Technology in May 2008.
You can execute the program with the following parameters:
\t $SCRIPT_NAME -h(--help) \t\t Help menu.
\t $SCRIPT_NAME -v(--version)\t\t Program version.
\t $SCRIPT_NAME --about \t\t About the program.
\t $SCRIPT_NAME \t\t Run the program.
";
history="
2.1 (2008-06-11) SANS Benjamin <sans.benjamin@gmail.com>
\t - using only tc instead of nistnet and tc
\t - changing bash interpretor
2.0 (2008-05-22) SANS Benjamin <sans.benjamin@gmail.com>
\t - new dynamic menu
\t - cleaning modifications when closing program
\t - parameters -h(--help), -v(--version), --about
\t - auto-load nistnet
1.0 (2008-05-19) SANS Benjamin <sans.benjamin@gmail.com>
\t - adding option \"Bad Quality\"
\t - adding colors
0.1 (2008-05-15) SANS Benjamin <sans.benjamin@gmail.com>
\t - First version using static menu without colors
";
about="
Developped in the SRC (Software Research Centre) at the Athlone Institute of Technology.
Developpers:
\t - SANS Benjamin starting on 2008-05-15
";
version=$( echo $history | cut -d " " -f1 );
if [ $# -gt 0 ] ; then
case $1 in
"-h"|"--help" )
echo -e "$help";
exit 0;
;;
"-v"|"--version" )
echo "$SCRIPT_NAME version $version";
echo -e "\nChanges history:"
echo -e "$history\n";
echo "run $SCRIPT_NAME --help for help menu."
exit 0;
;;
"--about" )
echo "$SCRIPT_NAME version $version";
echo -e "$about\n";
echo "run $SCRIPT_NAME --help for help menu."
exit 0;
;;
* )
echo -e "$wrong_param";
exit 0;
;;
esac
fi
log=n;
load=n;
pb=n;
CNISTNET_PATH=$( which cnistnet );
START=$PWD;
if [ $USER != "root" ] ; then
echo "$(tput setaf 1)You should be root to run this program $(tput sgr0)";
echo -en "Do you want the program logs you automatically as root ? [y|N] ";
read log;
if [ -z "$log" ] ; then
log=n;
fi
case $log in
"y"|"Y" )
sudo $0;
exit 0;
break;
;;
"n"|"N" )
echo "exiting program...";
exit 0;
break;
;;
* )
echo "Wrong answer";
echo "exiting program...";
exit 1;
break;
;;
esac
fi
ROUTER_ON=$(cat /proc/sys/net/ipv4/ip_forward);
WAS_ROUT=false;
if [ "$ROUTER_ON" -ne 1 ] ; then
echo -e "$(tput setaf 1)Your computer is not currently set as a router.$(tput sgr0)";
echo -en "Do you want the program set the routing automatically ? [y|N] ";
read rout;
if [ -z "$rout" ] ; then
rout=n;
fi
case $rout in
"y"|"Y" )
echo 1 > /proc/sys/net/ipv4/ip_forward;
break;
;;
"n"|"N" )
echo "exiting program...";
exit 0;
break;
;;
* )
echo "Wrong answer";
echo "exiting program...";
exit 1;
break;
;;
esac
else
WAS_ROUT=true;
fi
choix=0;
delay=0;
jitter=0;
number_if="$(ifconfig | grep eth | wc -l)";
nb_curr_changes=0;
choice_jitter=n;
choice_delay=y;
choice_drop=n;
interface=0;
ip1_default="192.168.3.100";
ip2_default="192.168.101.220";
ip1="";
ip2="";
drop=0;
drop_flag=false;
tab_flag=false;
changes=false;
changes_delay=false;
add_with_jitter() {
echo "The following interfaces were found:";
if [ "${#if_tab[*]}" == 0 ] ; then
echo -e `ifconfig | grep eth | cut -d \ -f 1`;
else
cpt=0;
nb_found=0;
while [ "$nb_found" -lt "$number_if" ] ; do
if [ ! -z "$(ifconfig | grep eth$cpt)" ] ; then
((nb_found+=1));
if [ -z "${if_tab[$cpt]}" ] ; then
echo -n "eth$cpt";
fi
fi
((cpt+=1));
done
unset cpt;
unset nb_found;
fi
echo "";
echo -en "Type the number of the one you choose: "
read interface;
if [ -z "$interface" ] ; then
clear;
echo "$(tput setaf 1)Error: You must choose one interface!";
else
nodigits="$(echo $interface | sed 's/[[:digit:]]//g')"
if [ -z "$nodigits" ] ; then
if [ -z "$(ifconfig | grep eth$interface)" ] ; then
clear;
echo "$(tput setaf 1)Error: $interface no matching interface.";
else
if [ ! -z "${if_tab[$interface]}" ] ; then
clear;
echo "$(tput setaf 1)Error: Interface $interface already set";
else
echo -en "Now enter the delay: ";
read delay;
nodigits="$(echo $delay | sed 's/[[:digit:]]//g')"
if [ -z "$nodigits" ] ; then
jitter;
else
clear;
echo "$(tput setaf 1)Error: The delay must be a number";
fi
fi
fi
else
clear;
echo "$(tput setaf 1)Error: The interface number must be a digit";
fi
fi
}
drop() {
echo "The following interfaces were found:";
if [ "${#if_tab[*]}" == 0 ] ; then
echo -e `ifconfig | grep eth | cut -d \ -f 1`;
else
cpt=0;
nb_found=0;
while [ "$nb_found" -lt "$number_if" ] ; do
if [ ! -z "$(ifconfig | grep eth$cpt)" ] ; then
((nb_found+=1));
if [ -z "${if_tab[$cpt]}" ] ; then
echo -n "eth$cpt";
fi
fi
((cpt+=1));
done
unset cpt;
unset nb_found;
fi
echo "";
echo -en "Type the number of the one you choose: "
read interface;
if [ -z "$interface" ] ; then
clear;
echo "$(tput setaf 1)Error: You must choose one interface!";
else
nodigits="$(echo $interface | sed 's/[[:digit:]]//g')"
if [ -z "$nodigits" ] ; then
if [ -z "$(ifconfig | grep eth$interface)" ] ; then
clear;
echo "$(tput setaf 1)Error: $interface no matching interface.";
else
if [ ! -z "${if_tab[$interface]}" ] ; then
clear;
echo "$(tput setaf 1)Error: Interface $interface already set";
else
echo -en "Now enter the % of drop: ";
read drop;
nodigits="$(echo $drop | sed 's/[[:digit:]]//g')"
if [ -z "$nodigits" ] ; then
if $changes ; then
clear;
tc qdisc change dev eth$interface root netem loss $drop%
else
clear;
tc qdisc add dev eth$interface root netem loss $drop%
fi
if_tab[$interface]="eth$interface";
changes=true;
changes_delay=true;
else
clear;
echo "$(tput setaf 1)Error: The % must be a number";
fi
fi
fi
else
clear;
echo "$(tput setaf 1)Error: The interface number must be a digit";
fi
fi
}
jitter() {
echo -en "Would you like to add jitter ? [y|N] ";
read choice_jitter;
if [ -z "$choice_jitter" ] ; then
choice_jitter=n;
fi
case $choice_jitter in
"y"|"Y" )
echo -en "Now enter the jitter: ";
read jitter;
nodigits="$(echo $jitter | sed 's/[[:digit:]]//g')"
if [ -z "$nodigits" ] ; then
clear;
changes_delay=true;
changes=true;
echo "$(tput setaf 6)";
if_tab[$interface]="eth$interface";
tc qdisc add dev eth$interface root netem delay ${delay}ms ${jitter}ms 15%
else
clear;
echo "$(tput setaf 1)Error: The jitter must be a number";
fi
;;
"n"|"N" )
clear;
changes_delay=true;
changes=true;
echo "$(tput setaf 6)";
if_tab[$interface]="eth$interface";
tc qdisc add dev eth$interface root netem delay ${delay}ms
;;
* )
clear;
echo "$(tput setaf 1)Error: Wrong choice...";
;;
esac
}
change_with_jitter() {
echo "The following interfaces were found:";
echo "${if_tab[*]}";
echo "";
echo -en "Type the number of the one you choose: "
read interface;
if [ -z "$interface" ] ; then
clear;
echo "$(tput setaf 1)Error: You must choose one interface!";
else
nodigits="$(echo $interface | sed 's/[[:digit:]]//g')"
if [ -z "$nodigits" ] ; then
if [ -z "$(ifconfig | grep eth$interface)" ] ; then
clear;
echo "$(tput setaf 1)Error: $interface no interface matching.";
else
if [ ! -z "${if_tab[$interface]}" ] ; then
echo -en "Now enter the delay: ";
read delay;
nodigits="$(echo $delay | sed 's/[[:digit:]]//g')"
if [ -z "$nodigits" ] ; then
echo -en "Would you like to add jitter ? [y|N] ";
read choice_jitter;
if [ -z "$choice_jitter" ] ; then
choice_jitter=n;
fi
case $choice_jitter in
"y"|"Y" )
echo -en "Now enter the jitter: ";
read jitter;
nodigits="$(echo $jitter | sed 's/[[:digit:]]//g')"
if [ -z "$nodigits" ] ; then
clear;
echo "$(tput setaf 6)";
tc qdisc change dev eth$interface root netem delay ${delay}ms ${jitter}ms 15%
else
clear;
echo "$(tput setaf 1)Error: The jitter must be a number";
fi
;;
"n"|"N" )
clear;
echo "$(tput setaf 6)";
tc qdisc change dev eth$interface root netem delay ${delay}ms
;;
* )
clear;
echo "$(tput setaf 1)Error: Wrong choice...";
;;
esac
else
clear;
echo "$(tput setaf 1)Error: The delay must be a number";
fi
else
clear;
echo "$(tput setaf 1)Error: eth$interface is not already set.";
fi
fi
else
clear;
echo "$(tput setaf 1)Error: The interface number must be a digit";
fi
fi
}
delete() {
echo "The following interfaces were found:";
echo "${if_tab[*]}";
echo "";
echo -en "Type the number of the one you choose: "
read interface;
if [ -z "$interface" ] ; then
clear;
echo "$(tput setaf 1)Error: You must choose one interface!";
else
nodigits="$(echo $interface | sed 's/[[:digit:]]//g')"
if [ -z "$nodigits" ] ; then
if [ -z "$(ifconfig | grep eth$interface)" ] ; then
clear;
echo "$(tput setaf 1)Error: $interface no interface matching.";
else
if [ ! -z "${if_tab[$interface]}" ] ; then
clear;
echo "$(tput setaf 6)";
unset if_tab[$interface];
tc qdisc del dev eth$interface root
else
clear;
echo "$(tput setaf 1)Error: eth$interface is not already set.";
fi
fi
else
clear;
echo "$(tput setaf 1)Error: The interface number must be a digit";
fi
fi
}
statut() {
clear;
echo "$(tput setaf 6)";
i=0;
cpt=0;
nothing=true;
while [ "$cpt" -lt "$number_if" ] ; do
if [ -z "$(ifconfig | grep eth$i)" ] ; then
sleep 0;
else
string="$(tc qdisc show dev eth$i | cut -d : -f 2 | grep 'ms\|%')";
if [ -z "$string" ] ; then
sleep 0;
else
echo -en "\neth${i}:$string";
nothing=false;
fi
((cpt+=1));
fi
((i+=1));
done
echo -e "\n"
if $nothing ; then
echo "There is no changes";
fi
unset i;
unset nothing;
unset cpt;
}
very_bad() {
delete_all;
clear;
echo "$(tput setaf 6)";
i=0;
cpt=0;
while [ "$cpt" -lt "$number_if" ] ; do
if [ -z "$(ifconfig | grep eth$i)" ] ; then
sleep 0;
else
tc qdisc add dev eth$i root netem delay 500ms loss 15%
if_tab[$i]="eth$i";
((cpt+=1));
fi
((i+=1));
done
changes=true;
changes_delay=true;
unset i;
unset cpt;
}
undrop() {
clear;
echo "$(tput setaf 6)";
if $tab_flag ; then
cnistnet -r ${tab[*]}
tab_flag=false;
else
if $drop_flag ; then
cnistnet -r $ip1 $ip2
fi
fi
drop_flag=false;
}
delete_all() {
undrop;
i=0;
cpt=0;
while [ "$i" -lt "$number_if" ] ; do
if [ -z "$(ifconfig | grep eth$cpt)" ] ; then
sleep 0;
else
string="$(tc qdisc show dev eth$cpt | cut -d : -f 2 | grep 'ms\|%')";
if [ -z "$string" ] ; then
sleep 0;
else
tc qdisc del dev eth$cpt root
fi
((i+=1));
fi
((cpt+=1));
done
changes=false;
changes_delay=false;
unset if_tab;
unset i;
unset string;
unset cpt;
}
gen_menu() {
if [ "${#if_tab[*]}" -lt "$number_if" ] ; then
echo -e "\t 1. add delay (may also add jitter)";
else
echo -e "\t 1. change delay and/or jitter";
fi
if $changes_delay && [ "${#if_tab[*]}" -lt "$number_if" ] ; then
echo -e "\t 2. change delay and/or jitter";
echo -e "\t 3. current modifications";
echo -e "\t 4. Bad Quality";
# if $drop_flag ; then
# echo -e "\t 5. remove drop";
# else
echo -e "\t 5. add drop";
# fi
echo -e "\t 6. delete delay (and jitter/drop if set)";
echo -e "\t 7. delete all changes";
else
echo -e "\t 2. current modifications";
echo -e "\t 3. Bad Quality";
# if $drop_flag ; then
# echo -e "\t 4. remove drop";
# else
echo -e "\t 4. add drop";
# fi
if $changes ; then
echo -e "\t 5. delete all changes";
if $changes_delay ; then
echo -e "\t 6. delete delay (and jitter/drop if set)";
fi
fi
fi
}
sortie(){
if ! $WAS_ROUT ; then
echo 0 > /proc/sys/net/ipv4/ip_forward;
echo "exiting routing mode...";
fi
echo -n "Cleaning variables";
unset choix;
unset delay;
unset jitter;
unset number_if;
echo -n ".";
unset nb_curr_changes;
unset choice_jitter;
unset interface;
unset ip1_default;
unset ip2_default;
unset ip1;
unset ip2;
echo -n ".";
unset drop;
unset drop_flag;
unset tab_flag;
unset changes;
unset changes_delay;
unset log;
unset load;
unset pb;
echo -n ". ";
unset CNISTNET_PATH;
unset START;
unset tab;
unset title;
unset menu;
echo -e "done\n";
}
title="$(tput setaf 2)
\n/********************************\\
\n|\t SIMPLE NETWORK\t\t |
\n|\t CONFIGURATION\t\t |
\n\\********************************/
\n
";
menu="
Choose an action in the following list:
";
echo "$(tput setab 0)";
echo "$(tput setaf 7)";
delete_all;
clear;
echo -e $title;
while true; do
echo "$(tput setab 0)";
echo "$(tput setaf 7)";
echo -e $menu;
gen_menu;
echo -e "\n\t 10. exit";
read choix;
if [ -z "$choix" ] ; then
choix=0;
fi
case $choix in
"1" )
if [ "${#if_tab[*]}" -lt "$number_if" ] ; then
add_with_jitter;
else
change_with_jitter;
fi
;;
"2" )
if $changes_delay && [ "${#if_tab[*]}" -lt "$number_if" ] ; then
change_with_jitter;
else
statut;
fi
;;
"3" )
if $changes_delay && [ "${#if_tab[*]}" -lt "$number_if" ] ; then
statut;
else
very_bad;
fi
;;
"4" )
if $changes_delay && [ "${#if_tab[*]}" -lt "$number_if" ] ; then
very_bad;
else
if [ "${#if_tab[*]}" -lt "$number_if" ] ; then
drop;
fi
fi
;;
"5" )
if $changes_delay && [ "${#if_tab[*]}" -lt "$number_if" ] ; then
#
if [ "${#if_tab[*]}" -lt "$number_if" ] ; then
drop;
fi
else
if $changes ; then
delete_all;
else
clear;
choix=0;
fi
fi
;;
"6" )
if $changes_delay && [ "${#if_tab[*]}" -lt "$number_if" ] ; then
delete;
else
if $changes_delay ; then
delete;
else
clear;
choix=0;
fi
fi
;;
"7" )
if $changes_delay && [ "${#if_tab[*]}" -lt "$number_if" ] ; then
delete_all;
else
clear;
choix=0;
fi
;;
"10" )
# Breaking loop to exit
break;
;;
* )
# wrong menu choice
clear;
echo "$(tput setaf 1)Invalide choice, please try again...";
;;
esac
done
echo "$(tput sgr0)";
clear;
delete_all;
echo "$(tput sgr0)";
echo -e "exiting program...\n";
sortie;
exit 0;
Un petit script que j'utilise pour afficher les informations de rhythmbox dans conky :
song=$(rhythmbox-client --print-playing --no-start);
if [ "$song" == "" ]
then
echo -e "Rhythmbox isn't running";
else
artist=$(rhythmbox-client --print-playing-format "%ta" --no-start);
album=$(rhythmbox-client --print-playing-format "%at" --no-start);
title=$(rhythmbox-client --print-playing-format "%tt" --no-start);
duration=$(rhythmbox-client --print-playing-format "%td" --no-start);
elapsed=$(rhythmbox-client --print-playing-format "%te" --no-start);
if [ "$title" != "" ] ; then
if [ "$title" != "Unknown" ] ; then
echo -e "title:\t $title";
fi
fi
if [ "$artist" != "" ] ; then
if [ "$artist" != "Unknown" ] ; then
echo -e "artist:\t $artist";
fi
fi
if [ "$album" != "" ] ; then
if [ "$album" != "Unknown" ] ; then
echo -e "album:\t\t$album";
fi
fi
if [ "$duration" != "Unknown" ] ; then
if [ "$elapsed" != "Unknown" ] ; then
echo -e "elapsed:\t$elapsed/$duration";
fi
elif [ "$elapsed" != "Unknown" ] ; then
echo -e "elapsed time:\t$elapsed";
fi
fi
Je déterre ce topic dans l'espoir de concrétiser mon script.
J'aimerais créer un script me permettant d'effectuer quelques opérations de maintenance et cela en appuyant simplement sur un petit lanceur dans ma barre de tâche.
Voilà le script : http://rafb.net/p/yeGc8x96.html
Simpliste mais pratique. Le problème est qu'il ne se lance pas dans une console...
J'aimerais si possible, que quand je clique sur le lanceur, une console s'ouvre et effectue la totalité des opérations contenues dans ce script sous mes yeux, ce qui me permettrait, entre autre, de valider ou non les mises à jour après « aptitude safe-upgrade » (on sait jamais...). La console devrait se refermer une fois tout les tâches effectuées.
L'ennui c'est que j'ignore comment faire tout ça... ![]()
Dans le lanceur, mets une commande du style :
xterm -e /home/tonlogin/tonscript.sh
La console se referme aussitôt. ![]()
J'ai essayé ça et ça fonctionne : gksu -- env -u ORBIT_SOCKETDIR gnome-terminal -e /home/franck/Bureau/Entretien.sh
En revanche, la console se ferme à partir de dpkg..., surement parce qu'elle ne renvoie aucun résultat.
Quoi que normalement elle doit quand même me le signaler... ![]()
Hum... gksu c'est pas bon. (ça fait presque partie de la catégorie des virus
)
En revanche, tu soulèves un bon point... il faut lancer tout ton script en root sinon ça ne fera rien du tout. Si tu as un sudo bien configuré, essaie
xterm -e "sudo /home/franck/Bureau/Entretien.sh"
(note, je ne sais plus s'il faut des " ou pas)
Après, pour ton dernier problème, tu peux ajouter à la fin un :
&& sleep 100
voire un
&& echo 'C'est fini - appuyer sur entrer pour terminer.' && read
Bah le problème à la fin persiste.
Mais le début ça a l'air ok.
Si t'as confiance en ton compte Linux (c'est à dire que tu es le seul à y avoir accès), tu peux automatiser la saisie du password root je pense.
Faudrait faire un truc du genre
echo "monsuperpassword" | (sudo masupercommande)
Je regarderai si c'est faisable, je pense que oui. Par contre, ça implique de mettre son mot de passe en clair dans le script, donc chmod 700 obligatoire, et session PRIVÉE.
Après une rapide lecture du man :
echo "monsuperpassword" | (sudo -S masupercommande)
echo "monsuperpassword" | (sudo -S masupercommande)
intéressant... c'est toujours bon de stocker son mot de passe dans le .bash_history (ou équivalent) au cas où on l'oublierait.
![]()
Bah disons que n'étant pas le seul a y accéder je préfère laisser mes passes... ![]()