############## Content: linux fools # # all by scan@hypertext.at # no (c)! # # INDEX # ## BASH - ALIAS ## GREP - REGEXP ## watch increasing files ## Check Services ## tar example to copy whole direcrory structure ## remote copy with ssh ## GREP out lost & found from dir list ## substitution (sed) ## shell loop to build variable parameters together ## gzip & init 2 files ## rename files ## Find IP in files on the system - but do not grep proc or dev directory ## file and dir rights from apache conf ## grep out filenames of html code ## grep out Servernames / Serveraliases of more than one apache conf file ## zip logfiles from apache 1.3 and delete them ## delete mailfiles selective in an exim environment ## get 20 latest files of a directory tree ## grep values from commands ## scan your network for open ports ## make a cycled back up ## use find ## use perl to substitute regexp expression in all files ## make a web list of actual files sort by date ## wait for a user to message ## tar to splitted archive over ssh ################################################################### ## BASH - ALIAS ## ------------ # settings in ~/.bashrc # to grep the history for any items (e.g. shows all commands # including grep of the history) set: alias hg='history|grep $@' # to get a file with your history, set in ~./bashrc alias hst='history 2>\&1 >_hist_`date +''%Y%m%d%H%M%S''`' ## GREP - REGEXP ## ------------- # to find mysql strings type e.g. grep -ir -e "\(mysql_\(p\)\?connect\|->dbase_connect\|MySQLConn\)" ./D | \ grep -v -e ":\([[:space:]]\+\)\?\/\/" | \ sed -e "s/^M$//" ## watch increasing files ## ---------------------- # find files by datestamp findfiles.sh /opt/mysql_data -ds | sort -n # when findfiles.sh is: # #!/bin/sh # # File: /root/bin/find_files.sh # date: Mon Mar 3 09:16:19 CET 2003 # who: scan@hypertext.at # what: finds files and prints for sorted output if [ 'x'$2 = 'x' ] then echo "`basename $0` usage: `basename $0` {DIR} -{ds}" exit 1 fi if [ ! -d $1 ] then echo "`basename $0` usage: `basename $0` {DIR} -{ds}" echo "`basename $0` error: {DIR} not found" exit 1 fi # find / -type f -printf '%TY%Tm%Td %p\n' case x$2 in x-ds) string='%TY%Tm%Td%TH%TM%TS\t%s' ;; x-sd) string='%s\t%TY%Tm%Td%TH%TM%TS' ;; x-d) string='%TY%Tm%Td%TH%TM%TS' ;; x-s) string='%s\t' ;; x*) echo "`basename $0` usage: `basename $0` {DIR} -{ds|sd}" echo "`basename $0` error: -ds || -sd" exit 1 ;; esac # echo $string find $1 -type f -printf "$string %p\n" # ## Check Services # see running services to switch off chkconfig --list | grep -v "^[[:space:]]" | grep ":on" chkconfig --level 123456 lpd off ## tar example to copy ## ------------------- tar cf - ./myDir | ( cd /disk2home && tar xf - ) # make directory/file list cd anyDirectory ls > ~/x vi ~/x # ... tar czvf - -T ~/x | \ ssh -l USER 192.168.1.3 "( cd /targetDirectory && tar xzf - )" ## # or make a remote zip file splitted tar czvf - ./ | ssh -l USER 192.168.1.3 "( cd /targetDirectory && split -b690000000 - backup.date.tgz. )" # and when need date time of original file: h=/sourceDir/ cd /targetDir/ && tar czf - sourceFile | ( cd $h && tar xzf - --atime-preserve ) ## GREP out lost & found # ls /usr | sed -e "s/^lost+found//" ## substitution # search ServerName from conf files and del out unneeded # grep through more than one file grep -ie "^[[:space:]]*ServerName[[:space:]]\+" /etc/apache/*conf | \ sed -e "s/^\/etc\/apache\/.\+conf:[[:space:]]*ServerName[[:space:]]\+//" # search ServerAlias from conf file and del out unneeded # grep through one file grep -ie "^[[:space:]]*ServerAlias[[:space:]]\+" httpd.conf | \ sed -e "s/[[:space:]]*ServerAlias[[:space:]]\+//i" ## shell loop to build variable parameters together #!/bin/sh # assume, first parameter is fix, rest variable # set -x do_it() { par=$1 shift ports="" inloop=0 for i in $@ do pargot=$1 shift if [ 'x'$pargot != 'x' ]; then if [ $inloop == 0 ]; then ports="$pargot" inloop=1 else ports="$ports,$pargot" fi fi done echo "ports: $ports" } ## test: # do_it par1 10 12 13 14 # do_it par1 do_it par1 10 23 ## /shell loop to build variable parameters together ## gzip & init 2 files 1001 mkdir X 1002 cd X 1003 vi a b # fill text 1004 ls # ensure files 1005 a=a && ( cat $a | gzip --best -c >${a}.2.gz ) && cat $a ; echo $? # gzip first file 1006 a=b && ( cat $a | gzip --best -c >${a}.2.gz ) && cat $a ; echo $? # gzip second file 1007 ls -l ## /gzip & init 2 files ## rename files ## have t1.wav.mp3 t2.wav.mp3 and want t1.mp3 t2.mp3 ? do that: $ for i in `ls *mp3`; do echo "mv $i `echo $i | sed -e s/.wav.mp3$/.mp3/`"; done > x.sh ## Find IP in files on the system - but do not grep proc or dev directory #!/bin/bash ip='123.123.123.1' # search for for file in `find / -type f -print` do ( echo $file | grep -q -E "\/(dev|proc)\/" ) || \ grep -ln "$ip" $file done # was not working; so: #!/bin/bash grepfor="123\.123\.123\.1" c2d="/" if [ x$1 != "x" ]; then if [ ! -d $c2d ]; then echo "parameter is no directory" echo "usage: $0 [DIR]" exit fi c2d=$1 fi cd $c2d echo "MESSAGE: current dir is `pwd`" ( for i in `ls -d .[a-Z]*` do echo $i done 2>/dev/null && \ ls 2>/dev/null ) | \ while read dir do if [ -d $dir ] then if [ $dir = "proc" -o $dir = "dev" ] then echo "MESSAGE: omit directory $dir" else find $dir -type f -exec grep -HIsm 1 $grepfor {} \; fi else if [ -f $dir ] then grep -HIsm 1 $grepfor $dir else echo "WARNING: `pwd`/$dir is `file -b $dir` - not searching" fi fi done # eof # ## file and dir rights from apache conf (debian woody) # # grep -i DocumentRoot /etc/apache/httpd.conf | sed -e "s/DocumentRoot //i" | \ sed -e "s/^[[:space:]]\+//" | grep -v "^#" > ~user/x find `cat ~user/x` -exec chgrp www-data {} \; find `cat ~user/x` -type f -exec chmod 640 {} \; find `cat ~user/x` -type d -exec chmod 750 {} \; # ## grep out filenames of html code # # html code could be:
  • pic 2
  • pic 1
  • pic 3
  • --- shell command to grep out filenames: $ grep "\.jpg\"" index.html | \ sed -e "s/^.\+\"\(.\+\.jpg\)/\1/" | \ sed -e "s/\".\+$//" | \ sed -e "s/^\.\.\/jokes\//\.\//" | # ## grep out Servernames / Serveraliases of more than one apache conf file # or shows how to concatinate more than one command for piping to another command sequence # ( grep -i -E "(servername|serveralias)" /anydir/virtualhosts.conf && \ grep -i -E "(servername|serveralias)" /anyotherdir/virtualhosts.conf && \ grep -i -E "(servername|serveralias)" /anotherdirectory/virtualhosts.conf ) | \ sed -e "s/servername[[:space:]]\+//i" | \ sed -e "s/serveralias[[:space:]]\+//i" | \ sed -e "s/^[[:space:]]\+//g" # yes, i know - could give all files to one grep .... - so just for syntax example # ## zip logfiles from apache 1.3 and delete them # #!/bin/sh # # file /root/bin/ziplogs.sh # date Fri Oct 15 15:38:32 CEST 2004 # by me # what finds log files from apache 1.3 # # vars date=`date '+%Y%m%d'` tarfile=/WHEREVER/httpdLogsMASHINE${date}.tgz prtfile=/WHEREVER/httpdLogsMASHINE${date}.lst # zip them all together in a tar file ( parse out of http conf scripts ) grep -i -E "(CustomLog|Errorlog) " /etc/apache/*conf | \ grep -v -e "#" | \ sed -e "s/^.\+[[:space:]]\//\//g" | \ sed -e "s/[[:space:]]\+combined[[:space:]]\?$//" | \ while read logfile do ls ${logfile}* | grep -i "\.gz$" done | \ tar czf $tarfile -T /dev/stdin >/dev/null 2>&1 # check tar file tar tzvf ${tarfile} >${prtfile} 2>&1 || { ( echo "$0: tarfile ${tarfile} corrupt - abort" && cat ${prtfile} ) | \ mail -s "MASHINE $0: ERROR zipping http-logs" YOUREMAIL exit 1 } # delete all logs tar tzf ${tarfile} | while read logfile do rm -f /${logfile} done cat ${prtfile} | \ mail -s "MASHINE $0: Logs saved to ${tarfile} - logfiles deleted" YOUREMAIL # eof # ## delete mailfiles selective in an exim environment # for i in `ls *-H`; do cat $i; echo "del?(j/x):"; read a; if [ x$a = 'xj' ]; then exim -Mrm `echo $i | sed -e "s/-H$//"`; fi; done # ## get N latest files of a directory tree # #!/bin/sh # # DIR=/anywhere # root directory TMP=/tmp/itsme.sh.$$ # tmp file OUT=/anywhere/latest.html # output file PRE=/templateDir/itsme.pre # comes before output POST=/templateDir/itsme.post # comes after output h=`pwd` cd $DIR string='%TY-%Tm-%Td %TH:%TM:%TS' cat <$PRE >$TMP 2>/dev/null echo "seite generiert: `date`

    " >>${TMP} int=0 find . -type f \( -name "*.html" -o -name "*.jpg" -o -name "*.gif" -o -name "*.txt" \) -printf "$string %p\n" | \ grep -v "\/anyDirYouDoNotWantHaveInTheList\/" | \ grep -v " \./anyFileYouDoNotWantHaveInTheList.html" | \ sort -nr | \ sed -e "s/[[:space:]]\.\/\(\(.\+\/\)\+\(.\+\)\)$/ \3 in \2<\/a>/" | while read line do echo "$line
    " int=`expr $int + 1` if [ $int -gt 20 ]; then break fi done >>$TMP 2>/dev/null cat <$POST >>$TMP 2>/dev/null cat <$TMP >$OUT 2>/dev/null chown username.group $OUT && chmod 640 $OUT cd $h rm -f $TMP # eof # ## grep values from commands # watch mashines ressources beside procinfo # # calling script # #!/bin/sh iam=`basename $0` protfile="/anywhere/${iam}.log" if [ -f $protfile ] then /root/bin/sys_statistik.sh >>${protfile} 2>&1 else /root/bin/sys_statistik.sh -h >${protfile} 2>&1 fi # eof #!/bin/sh # # file: /root/bin/sys_statistik.sh # by hagals.mail@gmx.net # debian dist - ipchains # headerpar=$1 shift netstat=/bin/netstat grep=/bin/grep wc=/usr/bin/wc sed=/bin/sed uptime=/usr/bin/uptime ipchains=/sbin/ipchains top=/usr/bin/top date=`date '+%Y/%m/%d:%H:%m:%s'` established=`$netstat -na --listen | $grep ESTA | $wc | $sed -e 's/^[[:space:]]\+\([[:digit:]]\+\)[[:space:]]\+.\+/\1/'` timewait=`$netstat -na --listen | $grep TIME_W | $wc | $sed -e 's/^[[:space:]]\+\([[:digit:]]\+\)[[:space:]]\+.\+/\1/'` loadaverage=`$uptime | $sed -e 's/^.\+load average:[[:space:]]\(.\+\),[[:space:]]\(.\+\),[[:space:]]\(.\+\)/\1;\2;\3/'` traffico8=`$ipchains -L output -n -x -v | $grep '0.0.0.0/0 80 -. ' | $sed -e 's/^[[:space:]]*\([[:digit:]]\+\)[[:space:]]\+\([[:digit:]]\+\).\+$/\1;\2/'` trafficor=`$ipchains -L output -n -x -v | $grep '1024:65535 -. 1024:65535' | $sed -e 's/^[[:space:]]*\([[:digit:]]\+\)[[:space:]]\+\([[:digit:]]\+\).\+$/\1;\2/'` traffici8=`$ipchains -L output -n -x -v | $grep '0.0.0.0/0 0.0.0.0/0 80 -. ' | $sed -e 's/^[[:space:]]*\([[:digit:]]\+\)[[:space:]]\+\([[:digit:]]\+\).\+$/\1;\2/'` trafficir=`$ipchains -L output -n -x -v | $grep '1024:65535 -. 1024:65535' | $sed -e 's/^[[:space:]]*\([[:digit:]]\+\)[[:space:]]\+\([[:digit:]]\+\).\+$/\1;\2/'` mem=`$top b n1| $grep 'Mem:'| $sed -e 's/Mem:[[:space:]]\+[[:digit:]]\+K total,[[:space:]]\+\([[:digit:]]\+\)K used,[[:space:]]\+\([[:digit:]]\+\)K free.[[:space:]]\+\([[:digit:]]\+\)K buffers$/\1;\2;\3/'` swap=`$top b n1|$grep 'Swap:'| $sed -e 's/Swap:[[:space:]]\+[[:digit:]]\+K total,[[:space:]]\+\([[:digit:]]\+\)K used,[[:space:]]\+\([[:digit:]]\+\)K free.[[:space:]]\+\([[:digit:]]\+\)K cached$/\1;\2;\3/'` [ 'x'${headerpar} = 'x-h' ] && echo "date;established;timewait;la1;la2;la3;opack;obytes;orpack;orbytes;ipack;ibytes;irpack;irbytesmemuse;memfree;buffered;swapuse;swapfree;cached" echo "${date};${established};${timewait};${loadaverage};${traffico8};${trafficor};${traffici8};${trafficir};${mem};${swap}" # ## scan your network for open ports # #!/bin/sh # asuming you have 12.13.15.192/26 or 12.13.15.192/255.255.255.192 # means you have to scan from *.192 to *.254 incl. # # use nmap to do # ip=192 while true do wholeip="12.13.15.${ip}" echo "analysing $wholeip" nmap -P0 "$wholeip" ip=`expr $ip + 1` if [ $ip -gt 254 ]; then break fi done >~YourHome/protocol.txt # ## make a cycled back up # #-- script: #!/bin/bash # # file: bin/backupcycled.sh # date: Wed Jan 5 16:16:15 CET 2005 # by: scan@hypertext.at # what: back up system # use # ( cat setname* ) | tar xzvf - [pattern] # to restore # # config # setbase="MASHINENAME" cyclesare=4 stfile=/root/bin/backupcycled.st date=`date '+%Y%m%d-%H%M%S'` source="/server/dir1/ /home/ /etc/ /root/" target=/Directory/backup # # end config # calculate stamp st=0 [ -f $stfile ] && st=`cat $stfile` st=`expr $st + 1` [ $st -gt ${cyclesare} ] && st=1 # set vars setname=${setbase}.${st}.${date}.tgz # echo "setname=${setname}=" protfile=${target}/${setbase}.${st}.${date}.log # echo "protfile=$protfile" splitto=${target}/${setname}. # echo "splitto=$splitto" # delete existing set rm -f ${target}/${setbase}.${st}.* >/dev/null 2>&1 # backup ( tar czvf - $source --ignore-failed-read | \ split -b690000000 - ${splitto} ) >${protfile} 2>&1 # save stamp echo $st >$stfile exit 0 # eof bin/backupcycled.sh # ## and add a cronjob to execute every sunday 2 o'clock in the morning: # 0 2 * * 0 nohup /root/bin/backupcycled.sh >/dev/null 2>&1 & # ## use find # # find dirs: $ find . -name "AMUS*" -type d # find pix inside found dirs: $ find `!!` \( -iname "*.jpg" -o -iname "*.gif \) -type f # and output list to file: $ !! >found.txt 2>&1 # ## use perl to substitute regexp expression in all files # $ perl -p -i -e "s;FROM;TO;g" * # ## make a web list of actual files sort by date # #!/bin/bash outfile=/usr/local/www/htdocs/jokes/lastlist.html string='%TY%Tm%Td
    %TY-%Tm-%Td %p
    \n' ( echo '' find /usr/local/www/htdocs/jokes/ -type f -printf "$string" \ | grep -v -e "index\.html<\/a" \ | grep -v -e "lastlist\.txt<\/a" \ | grep -v -e "lastlist\.html<\/a" \ | grep -v -e "\.bak<\/a" \ | sort -rn \ | sed -e "s/^[[:digit:]]\+//" \ | sed -e "s/\/var\/lib\/www\/htdocs//g" echo '' ) >$outfile chmod 640 $outfile chown apache.apache $outfile # and we are # ## wait for a user to message # #!/bin/bash # # if here, write yes man and exit # if not make a dot # to=robert echo -e "waiting for $to\c" while true do ( who | grep -q $to ) && { write $to <<__x_ yes man -- __x_ break } || echo -e ".\c" sleep 5 done # ## tar to splitted archive over ssh # date=`date '+%Y%m%d-%H%M%S'` ls *.gz | tar czvf - -T /dev/stdin | ssh scan@aladin "( cd LOGBACKUP/floh && mkdir EXIM && cd EXIM && split -b690000000 - floh_exim_logs-${date}.tgz. )" # # eof