Scripturi BASH
From linux360
Contents
- 1 Exemple scripturi BASH
- 1.1 Scripturi care permit schimbarea rapida a MAC+IP
- 1.2 Script care lanseaza in fundal o anumita comanda data ca parametru de intrare
- 1.3 Script ce elimina zona asociata unui domeniu dat ca parametru de intrare
- 1.4 Cautarea unui fisier ce contine un anumit string
- 1.5 Stergerea fisierelor backup
- 1.6 Probleme cu spatiul pe disc?
- 1.7 Gasirea fisierelor duplicate intr-un director
- 1.8 Calcularea recordului de uptime
Exemple scripturi BASH
Scripturi care permit schimbarea rapida a MAC+IP
Raptor360 12:33, 8 June 2006 (EEST)
getmac
Primeste ca parametru un IP si returneaza MAC-ul acestuia sau "" daca nu exista in retea nodul cu pricina.
/usr/local/bin/getmac<sh/>#!/bin/sh
if ! $IFDEVICE ; then
IFDEVICE="eth0";
fi
mac=`arping -I $IFDEVICE $1 -c 3 | grep reply | cut -f2 -d\[ | cut -f1 -d\] | uniq` echo $mac
be
Primeste ca parametru un IP si schimba MAC-ul si IP-ul pentru un device.
/usr/local/bin/be<sh/>#!/bin/bash
if ! $IFDEVICE ; then
IFDEVICE="eth0"
fi
gip=`echo -e "$1\t"` echo -e " IP is $1." mac=`cat /etc/stored | grep "$gip" | cut -f2` echo -e "MAC is $mac." if $mac != "" ; then
ifconfig $IFDEVICE down ifconfig $IFDEVICE hw ether $mac ifconfig $IFDEVICE $1 netmask 255.255.248.0 broadcast 10.10.17.255 route add default gw 10.10.17.1 # echo "nameserver 10.10.17.1" > /etc/resolv.conf
else
echo "$1 has not an associated MAC."
fi
switchuser
Schimba MAC-ul si IP-ul aleator conform unuia stocat in /etc/stored folosind scripturile anterioare.
/usr/local/bin/switchuser<sh/>#!/bin/bash
notfound=1 while $notfound -eq 1 ; do
rip=`echo "$RANDOM % 252 + 3" | bc` for i in `seq $rip 254`; do randip="10.10.17.$i" gip=`echo -e "$randip\t"` if grep "$gip" | cut -f2` != "" ; then if `getmac $randip` == "" ; then be $randip exit 0 notfound=1 else echo "IP $randip is already active." fi else echo "$randip has no MAC associated." fi done
done
Scripturile folosesc fisierul de configurare /etc/stored in care se adauga perechi de forma: IP\tMAC, e.g. 10.10.17.241 00:20:ED:95:2A:A1
10.10.17.242 4C:00:10:53:BF:A5
precum si variabila din enviroment IFDEVICE daca este definita. e.g. daca vrem sa schimbam ip-ul doar pentru device-ul eth1 atunci apelam la:
# IFDEVICE=eth1 switchuser
Script care lanseaza in fundal o anumita comanda data ca parametru de intrare
Puteti lasa o comanda sa se execute in fundal fara ca ea sa fie intrerupta la iesirea dumneavoastra din sistem folosind urmatorul script pe care l-am numit silent. (output-ul nu este salvat, pentru aceasta puteti folosi nohup)
<sh/>#!/bin/bash
echo "Executing \"$*\"" setsid bash -c $* 2>>/dev/null 1>>/dev/null < /dev/null &
Scriptul il puteti folosi de exemplu:
$ silent wget -c -t 0 www.bigfiles.com/ubuntu.iso
Script ce elimina zona asociata unui domeniu dat ca parametru de intrare
Puteţi elimina porţiunea din named.conf de la zone "nume.domeniu.dat.ca.parametru.de.intrare" { până la acolada închisă asociată acoladei deschise de mai sus.
Scriptul este:<sh/>#!/bin/bash
- Bind zone remove script
- Copyright (C) 2005 Silvian Cretu <silvian86@yahoo.com>
- 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 2
- 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, write to the Free Software
- Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
if [ ! $# = 1 ]; then
echo "Usage: $0 domain"; echo "The path to named.conf is defined inside the script"; else
pathToNamedDotConf=/etc/named.conf
x=$(grep -n "zone \"$1\" {" $pathToNamedDotConf | cut -f1 -d:) sed $x,/\}\;/d $pathToNamedDotConf > temp c=$(sed -n $x'p' < temp) if [ "x`echo $c | grep "zone"`" = "x" ] then sed $x' d' temp > $pathToNamedDotConf else cat temp > $pathToNamedDotConf fi rm -f temp exit fi
Cautarea unui fisier ce contine un anumit string
<sh/>find /path -name "*" -exec grep -H "<string>" '{}' \;
sau
<sh/>grep -Hr "<string>" /path/
Fireste, <string> poate fi atat un sir de caractere cat si orice fel de expresie regulata suportata de versiunea particulara de grep disponibila pe masina in cauza.
Deasemenea, pentru a cauta fara a face diferenta intre majuscule si minuscule, adaugati parametrul -i la grep.
Daca doriti ca in output-ul comenzii grep expresia cautata sa fie cautata sa fie evidentiata puteti folosi parametrul --color astfel:
<sh/>grep --color -Hr "<string>" /path/
Stergerea fisierelor backup
Asa cum stiti, prin traditie in *NIX, editoarele text fac o copie de siguranta fisierelor editate, copie de siguranta ce este denumita identic cu fisierul initial plus caracterul tilda (~). Ei bine... in cazul aplicatiilor web, acest backup mai mult dauneaza decat sa ajute. Nu ar fi prea placut sa aveti un index.php~ in DocumentRoot
<sh/>find /path -name "*~" -exec rm -f '{}' \;
Probleme cu spatiul pe disc?
Nu stiti unde "vi s-a dus" spatiul de pe disc si e cam greu sa verificati fiecare director in parte?
Linia urmatoare va ajuta, facand totodata si o sortare.
<sh/>du --max-depth=1 /path | sort -rn
Gasirea fisierelor duplicate intr-un director
Aveti prea multe mp3-uri si majoritatea sunt duplicate dar cu alt nume sau orice de genul folositi scriptul de mai jos:D Eventual adaugati si un -maxdepth 1 sa nu fie recursiv.
- !/bin/sh
if [ ! -d "$1" ]; then
echo "Usage $0 <dir>" exit 1
fi find "$1" -type f -print0 | xargs -0 -n1 md5sum | sort --key=1,32 | uniq -w 32 -d --all-repeated=prepend \
|cut -f3- -d' '|sed 's/^$/Fisiere identice:/'
Calcularea recordului de uptime
Scriptul urmator va trebui plasat in crontab astfel incat sa ruleze periodic, de exemplu, din ora in ora. El va afisa in fisierul $output (initial /var/log/uptimeRecord.log) output-ul comenzilor uptime si date (adica uptime-ul record si data la care a fost inregistrat).
<sh/>#!/bin/bash
- Copyright (C) 2005 Silvian Cretu <silvian86@yahoo.com>
- 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 2
- 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, write to the Free Software
- Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
output='/var/log/uptimeRecord.log'
function compareHrs {
uptimeCurentOre=`uptime | awk '{print $5}'` uptimeRecordOre=`cat $output | head -1 | awk '{print $5}'` if [ `expr length $uptimeCurentOre` \> `expr length $uptimeRecordOre` ]; then
- echo Nou record - Uptime curent mai mare cu cateva ore decat uptime-ul record 1
uptime > $output date >> $output else if [ `expr length $uptimeCurentOre` == `expr length $uptimeRecordOre` ]; then uptimeCurentOra=`echo $uptimeCurentOre | cut -d":" -f1` uptimeRecordOra=`echo $uptimeRecordOre | cut -d":" -f1` if [ $uptimeCurentOra -gt $uptimeRecordOra ]; then
- echo Nou record - Uptime curent mai mare cu cateva ore decat uptime-ul record 2
uptime > $output date >> $output fi fi fi
}
if [ -a $output ]; then
uptimeCurent=`uptime` uptimeRecord=`cat $output | head -1` if [ `echo $uptimeCurent | grep -c day` == '0' ]; then
- echo Uptime mai mic de o zi
if [ `echo $uptimeRecord | grep -c day` == '0' ]; then
- echo Uptime record mai mic de o zi, comparam orele
compareHrs
- else
- echo Uptime record mai mare ca uptime curent
fi else
- echo Uptime mai mare de o zi
if [ `echo $uptimeRecord | grep -c day` == '0' ]; then
- echo Uptime record mai mic de o zi
uptime > $output date >> $output else uptimeCurentZile=`uptime | awk '{print $3}'` uptimeRecordZile=`cat $output | head -1 | awk '{print $3}'` if [ $uptimeCurentZile -eq $uptimeRecordZile ]; then
- echo Uptime in zile egal
compareHrs else if [ $uptimeCurentZile -gt $uptimeRecordZile ]; then
- echo Record nou
uptime > $output date >> $output fi fi fi fi
else
- echo Fisierul nu exista, deci trebuie creat. Recordul e uptime-ul curent
uptime > $output date >> $output
fi
exit 0