Afficher / masquer le bureau gnome
Nom du script : showdesktop.sh
Compatible avec : bash (pas testé : sh, csh, tcsh)
Descriptif : Affiche ou masque le bureau gnome. A utiliser en ligne de commande ou avec un lanceur, particulièrement pour utilisation avec Avant Window Navigator.
Pré-requis: wmctrl (exemples d'utilisation)
Code:
Code:
1 2 3 4 5 6
| #!/bin/bash
if wmctrl -m | grep -q '"showing the desktop" mode: ON'; then
wmctrl -k off
else
wmctrl -k on
fi |
Téléchargement: [Visualiser]
equivalent de la commande ps en shell
Bonjour,
enfin un endroit ou on peut déposer nos scripts :P
Nom du script : Processlist.sh
Compatible avec : sh
Descriptif :Liste tous les processus (équivalent à un ps-e).
Pré-requis: Aucun.
Code:
1 2 3 4 5 6 7 8
|
#!/bin/sh
#script shell qui liste tous les processus en exécution
if (("$#">"0"));then if test $1="-h";then echo "PID | CMD ";else cat plistehelp;fi;fi;
cd ../../proc ;
for i in $(ls); do if (("$i">"0"));then [[ -d $i ]] && echo -n "$i| ";if test -f $i/cmdline ;then cat $i/cmdline;fi ;echo ;fi;done;
echo "script réalisé par cherif mohieddine belkacem";
cd ../$HOME; |
et le petit help de la commande qui va avec
Plistehelp.sh
Code:
1 2 3 4 5 6
|
utilisation : sh pliste.sh [-h]
./ pliste.sh [-h]
affiche la liste des processus (PID | CMD)
-h : affiche l'entete du tableau PID | CMD
sans option il n'affiche pas l'entete |
cordialement @+
script de sauvegarde sur bande
bonjour,
Nom du script: sauve
Compatible avec: sh
Descriptif: ce script vous permettra d'appliquer une stratégie de sauvegarde journalière à trois niveaux sur un mois.
supposons que vous voulez sauvegarder le répertoire /home. Prenons, par exemple, le lundi. Le premier lundi du mois, vous faites une sauvegarde totale:
Les autres lundi, vous sauvegarder les fichiers qui ont été modifiés depuis le premier lundi du mois.
le deuxième lundi du mois (s come semaine):
le troisième:
Ainsi de suite jusqu'au mois suivant.
Les autres jours, vous sauvegarder tous les fichiers modifiés depuis le dernier lundi.
Le mardi (j comme jours):
le mercredi:
Et ainsi de suite jusqu'à dimanche
Pré-requis: tar, cpio, find, mt
Code:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
| #!/bin/sh
# sauve, a backup script
# Copyright (C) 2007 2009 Mathieu SERPA
#
# This program 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 3 of the License, or
# (at your option) any later version.
# This program 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 program. If not, see <http://www.gnu.org/licenses/>.
# matser1973@free.fr
list(){
case "$1" in
"-t")
shift
find "$@";;
"-i")
moment="$2"
shift
shift
find "$@" -daystart -mtime "$moment";;
*)
echo usage:
echo "sauve -t répertoire(s) | -j nb_jours répertoire(s) | -s nb_semaines répertoire(s)"
esac
}
sauve_incr1(){
if [ $1 -le 4 ]; then
etendue=$(($1*7))
shift
(
while [ $etendue -ge 0 ];do
list -i $etendue $@
etendue=$(($etendue-1))
done
)|cpio -ovBH crc>/dev/st0
else
echo le nombre de semaine ne doit pas être spérieur à 4
fi
}
sauve_incr2(){
if [ $1 -le 6 ]; then
etendue=$1
shift
(
while [ $etendue -ge 0 ];do
list -i $etendue "$@"
etendue=$(($etendue-1))
done
)|cpio -ovBH crc>/dev/st0
else
echo le nombre de jours ne doit pas être supérieu à 6
fi
}
if [ $(whoami) = 'root' ]; then
cd $HOME
if [ -e sauve-etc.tar.bz2 ]; then
rm sauve-etc.tar.bz2
fi
tar -jcvf sauve-etc.tar.bz2 /etc
mt -f /dev/st0 retension
case $1 in
"-t")
shift
list -t "$@"|cpio -ovBH crc>/dev/st0;;
"-s")
etendue="$2"
shift
shift
sauve_incr1 $etendue "$@";;
"-j")
etendue="$2"
shift
shift
sauve_incr2 $etendue "$@";;
*)
echo usage:
echo "sauve -t répertoire(s) | -j nb_jours répertoire(s) | -s nb_semaine répertoire(s)";;
esac
mt -f /dev/st0 rewind
mt -f /dev/st0 eject
else
echo vous devez être root pour utiliser ce script
fi |
Mise à jour des règles de SELinux
Bonjour,
Nom du script: SEupdate
Compatible avec: Bash
Descriptif: Met à jour les règles de SELinux lorsqu'une nouvelle application veut accéder à un fichier sensible que SELinux interdit. Le script crée simplement les nouvelles règles et donne la commande pour les appliquer.
Pré-requis: Avoir SELinux qui tourne, bien sûr. Normalement, les alarmes arrivent dans /var/log/audit/audit.log. Si ce n'est pas le cas, changer le script et mettre à la place /var/log/messages.
Environnement: j'ai mis le script dans /root/selinux. Deux fichiers sont créés et doivent rester là pour l'exécution suivante, lorsqu'une prochaine alarme surviendra.
Lancement du script: La première fois, le fichier en paramètre n'existe pas. Ce n'est pas grave, il sera créé à la fin du script.
Code:
1 2 3
| sudo -i # si le fichier /etc/sudoers est bon, passage root sans mot de passe :-)
cd selinux # j'ai mis l'exéctable dans /root/selinux
./SEupdate selinux.te # lancement du script avec le nom du fichier de règles |
Code:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
| #!/bin/bash
# init
execName=$(basename $0)
tmpDir=$(mktemp -d /tmp/$execName.XXXXXX)
tmpFile=$tmpDir/$execName
# manage arguments
if [[ $# != 1 ]]
then
echo "Usage: $execName <Type_Enforcement_file>"
exit 1
fi
dstDir=$(dirname $1)
dstFile=$(basename $1)
dstFile=${dstFile%.te}
if [[ ! -s $1 ]]
then
echo "WARNING: <Type_Enforcement_file> non-existent or empty, creating it..."
touch $1
fi
cp $1 $tmpFile.te.src
# create new type enforcement file from audit file
audit2allow -m local -l -i /var/log/audit/audit.log > $tmpFile.te.add
# set header file
echo "
module local 1.0;
require {" > $tmpFile.te.new
# format and merge type lines
awk '
/^[[:space:]]*type / \
{
sub("^[[:space:]]*", " ")
print
}' $tmpFile.te.src > $tmpFile.type.src
awk '
/^[[:space:]]*type / \
{
sub("^[[:space:]]*", " ")
print
}' $tmpFile.te.add > $tmpFile.type.add
sort -u -k 2,2 $tmpFile.type.src $tmpFile.type.add >> $tmpFile.te.new
# format and merge class lines
awk '
/^[[:space:]]*class / \
{
sub("^[[:space:]]*", " ")
if ($0 !~ "[{}]")
{
$2=$2 " {"
if (length($NF) == 1) $NF="}" $NF
else sub(".$", " };", $NF)
}
print
}' $tmpFile.te.src | sort -k 3,3 > $tmpFile.class.src
awk '
/^[[:space:]]*class / \
{
sub("^[[:space:]]*", " ")
if ($0 !~ "[{}]")
{
$2=$2 " {"
if (length($NF) == 1) {$NF="}" $NF}
else {sub(".$", " };", $NF)}
}
print
}' $tmpFile.te.add | sort -k 3,3 > $tmpFile.class.add
[[ ! -s $tmpFile.class.src ]] && cp $tmpFile.class.add $tmpFile.class.src
while read srcLine
do
options=$(echo "$srcLine" | awk '
BEGIN {OFS="\n"}
{
for (i=4; i<NF; i++) {print $i}
}')
className=$(echo "$srcLine" | awk '{print $2}')
addOptions=$(awk -v c=$className '
BEGIN {OFS="\n"}
$2 == c \
{
for (i=4; i<NF; i++) {print $i}
}' $tmpFile.class.add)
[[ -n $addOptions ]] && options+=$'\n'$addOptions
line=" class $className { $(echo "$options" | sort -u | tr "\n" " ")};"
echo "$line" >> $tmpFile.te.new
done < $tmpFile.class.src
# close require block
echo "}" >> $tmpFile.te.new
# sort rules and format options
awk '
/^allow / \
{
if ($4 !~ "^{")
{
$4="{ " $4
sub(".$", " };", $NF)
}
print
}' $tmpFile.te.src $tmpFile.te.add |
sort -k 2,3 > $tmpFile.te.rules
# merge rules with same first three fields
cp -f $tmpFile.te.rules $tmpFile.te.rules.to.merge
> $tmpFile.te.rules.merged
while read ruleLine
do
selector=$(echo $ruleLine | cut -d " " -f 1-3)
options=$(awk -v s="$selector" '
BEGIN {OFS="\n"}
{
if ($0 ~ "^"s) {for (i=5; i<NF; i++) {print $i}}
}' $tmpFile.te.rules.to.merge)
mergedOptions=$(echo "$options" | sort -u | tr "\n" " ")
echo "$selector { $mergedOptions };" >> $tmpFile.te.rules.merged
sed -i '/^'"$selector"'/d' $tmpFile.te.rules.to.merge
done < $tmpFile.te.rules
sort -u -k 1,3 $tmpFile.te.rules.merged > $tmpFile.te.rules.uniq
# format rules by blocks
awk '
{
if ($2 != prevApp)
{
prevApp=$2
printf "\n#============= %s ==============\n", $2
}
print
}' $tmpFile.te.rules.uniq >> $tmpFile.te.new
# compile file
checkmodule -M -m -o $tmpFile.mod $tmpFile.te.new
# create policy package file
semodule_package -o $tmpFile.pp -m $tmpFile.mod
# replace policy package file
cp -f $tmpFile.pp $dstDir/$dstFile.pp
# replace type enforcement file
cp -f $tmpFile.te.new $dstDir/$dstFile.te
# clean temp dir up
rm -rf $tmpDir
# tell the next step to do, to install updated SELinux rules
echo "Now, run the command 'semodule -i $dstDir/$dstFile.pp'" |
J'ai débugué au mieux, mais peut-être reste-t-il encore des erreurs. Si vous en trouvez, dites-le moi.
Script Shell last_files_list
Mon premier script de super noob, merci pour l'accueil modéré :lol:
Nom du script: last_files_list <n_minutes>
Compatible avec: sh
Descriptif: permet de lister les fichiers vieux de n minutes pour une utilisation sur certaines distro "allégées", par ex celles des NAS, qui ne comportent pas la commande FIND avec toutes les options ( et notamment -amin -mmin -cmin...) permettant justement de lister les fichiers de moins de 1 jour.
Pré-requis: void
Code:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
| #!/bin/sh
# *******************************************************************
# * LOOK FOR THE LAST MODIFIED FILES since m minute_nows *
# * FIND cmde is uncomplete on the QNAP Linux version *
# * script call : my_find_lasted_files m, where m=minute_nows *
# * bug included: do not work for midnight research... (hour = 00) *
# *******************************************************************
# ***************************
# * INIT & GET TIME *
# ***************************
month_now=`date | awk '{print $2}'`
day_now=`date | awk '{print $3}'`
hour_now=`date | awk '{print $4}' | cut -d ':' -f 1`
minute_now=`date | awk '{print $4}' | cut -d ':' -f 2`
second_now=`date | awk '{print $4}' | cut -d ':' -f 3`
today_nowsfiles="/tmp/today_nowsfiles"
found_files="/tmp/foundfiles"
[[ -e $found_files ]] && rm $found_files
[[ -e $today_nowsfiles ]] && rm $today_nowsfiles
# format the input parameter required for the next search function : add a blank space if m-minute_now < 10
if [ ! -z $1 ]
then
if [ $1 -lt 10 ]
then
newerthan="0$1"
elif [ $1 -lt 60 ]
then
newerthan="$1"
else
newerthan="59"
fi
else
newerthan="05" #seek for files newer than 5 minute_nows
fi
# End searching time
hour_end=$hour_now
minute_end=$minute_now
# Start searching time
if [ $minute_now -gt $newerthan ]
then
hour_start=$hour_now
let "minute_start=$minute_now-$newerthan"
else
if [ $hour_now -gt 01 ]
then
let "hour_start=$hour_now-1"
let "minute_start=60-$newerthan+$minute_now"
else
let "hour_start=0"
let "minute_start=60-$newerthan+$minute_now"
fi
fi
# Convert time into minutes
if [[ $minute_start -lt 10 ]]
then
minute_start="0$minute_start"
fi
let "start_research_time=60*hour_start+$minute_start"
let "end_research_time=60*hour_end+$minute_end"
# debug
# echo "now $hour_now:$minute_now"
# echo "start $hour_start:$minute_start"
# echo "end $hour_end:$minute_end"
# format the spacer required for the next search function : add a blank space if d-day_now < 10 of the month_now
if [ $day_now -lt "10" ]
then
spacer=" "
else
spacer=" "
fi
# debug
# echo "grep $month_now$spacer$day_now"
# get names of the files of today_now
# ls -ltsha | grep "$month_now$spacer$day_now" | awk '{print $10}'
# get hh:mm of the files of today_now
# ls -ltsha | grep "$month_now$spacer$day_now" | awk '{print $9}'
# get hh of the files of today_now
# ls -ltsha | grep "$month_now$spacer$day_now" | awk '{print $9}' | cut -d ':' -f 1
# get mm of the files of today_now
# ls -ltsha | grep "$month_now$spacer$day_now" | awk '{print $9}' | cut -d ':' -f 2
# ***************************************
# * User msg *
# ***************************************
echo "Looking for files having been modified over the last $newerthan minutes between $hour_start:$minute_start and $hour_end:$minute_end ... pls wait"
# *******************************************
# * list of last 2 hours modified files *
# *******************************************
# Nota : add option -u to get last accessed time, instead of getting the file last modified time (by default)
echo "Building list ..."
ls -RltshaA 2>/dev/null | grep "$month_now$spacer$day_now $hour_start" > $today_nowsfiles
ls -RltshaA 2>/dev/null | grep "$month_now$spacer$day_now $hour_end" >> $today_nowsfiles
# debug
# cat $today_nowsfiles | more
# exit
# *******************************************************
# * Seek for files newer than $newerthan in the list *
# *******************************************************
let "count=1"
nb_of_files_today_now=`cat $today_nowsfiles | wc -l`
touch $found_files
while read file_line_i
do
# get the file informations
file_hour_i=`echo $file_line_i | cut -d ' ' -f 9 | cut -d ':' -f 1`
file_minute_i=`echo $file_line_i | cut -d ' ' -f 9 | cut -d ':' -f 2`
let "file_time_i=60*$file_hour_i+$file_minute_i"
file_name=`echo ${file_line_i##* }`
file_size=`echo $file_line_i | cut -d ' ' -f 6`
if [[ $file_time_i -ge $start_research_time ]] && [[ $file_time_i -le $end_research_time ]]
then
in_the_range=true
else
in_the_range=false
fi
# record files in the time range
if [[ $in_the_range ]]
then
if [ -f $file_name ] && [ ! -L $file_name ] && [ $file_name != "." ] && [ $file_name != ".." ] && [ $file_name != "0" ] && [ $file_name != $found_files ] && [ $file_name != $today_nowsfiles ] && [ $file_size != "0" ]
then
echo "found: $file_line_i"
echo -e "file $count: $file_line_i" >> $found_files
let "count=$count+1"
fi
fi
done < $today_nowsfiles
# *******************
# * Show results *
# *******************
nb_files_found=`cat $found_files | wc -l`
echo "cat $found_files | more"
echo "----------------------------------------------------------------------------------"
echo "$nb_files_found modified files in the last $newerthan minutes between $hour_start:$minute_start and $hour_end:$minute_end"
cat $found_files | more
echo "----------------------------------------------------------------------------------"
# *******************
# * clean & Get out *
# *******************
rm $found_files
rm $today_nowsfiles
exit |
et le bonus du Super Noob: afficher l'arborescence :
Citation:
ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/ * /' -e 's/-/|/'
shell - interpretation des caracteres speciaux
Nom du script: essai.ksh
Compatible avec: ksh
Descriptif: exporter des variables dans un fichier.
Le probleme est que le(s) caractere(s) * sont interpretes dans
la chaine OA_REMOVE_FILES_CRON alors que je souhaiterai voir
dans la premier for un affichage de la chaine
OA_REMOVE_FILES_CRON (cad: 0 1 * * *)
et dans le deuxieme for, un affichage de la chaine
(dans le fichier FILE_ENV)
export OA_REMOVE_FILES_CRON="0 1 * * *"
Merci de votre aide
Hervé
Pré-requis: aucun
Code:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| FILE_ENV=/tmp/OA.log
OAOPTIONLIST="OA_REMOVE_FILES_CRON"
if [ -z "$OA_REMOVE_FILES_CRON" ]; then
OA_REMOVE_FILES_CRON="0 1 * * *"
fi
echo "Loaded environment variables are:"
for var in $OAOPTIONLIST
do
if [ -n "$(eval echo \$$var)" ]; then
echo " $var=$(eval echo \$$var)"
fi
done
# Add to environment file the OA_ variables (without code markers)
for option in $OAOPTIONLIST
do
option_value=`eval echo '$'$option`
if [ "$option_value" != "" ];then
echo "export $option=\"$option_value\"" >> $FILE_ENV
fi
done |