Tuesday, 3 February 2009

Find files older than 3 days

This script finds all files with a modify time of 3 days ago or more.
It then uses the exec command to run a command on every match found,
it could easily be rm to remove files instead of listing them.


find /home/ -type f -mtime +3 -exec ls -l {} \;

Set date and time script

This script allows easy setting of the date and time.
Run script,and specify the following parameters after the script name.
Month Day Hour Minute Seconds Year


#Need to add read lines for $1 - $6 user input

date_proc () # MM dd hh mm ss yy ¦#
# dialogue param order # 2 3 4 5 6 1 ¦#
{
yy=$1
MM=$2
dd=$3
hh=$4
mm=$5
ss=$6
yy=${yy##*(0)}
yy=${yy##*(0)}
MM=${MM##*(0)}
dd=${dd##*(0)}
hh=${hh##*(0)}
mm=${mm##*(0)}
ss=${ss##*(0)}
DATE_STR=`/usr/bin/printf \"%02d%02d%02d%02d.%02d%02d\" $(($MM)) $(($dd)
) $(($hh)) $(($mm)) $(($ss)) $(($yy))`
/usr/bin/date \"$DATE_STR\"
}
date_proc \'08\' \'11\' \'05\' \'14\' \'01\' \'00\'

Tripwire Prune Report Script


#!/bin/bash

DATE=`date +%H%M-%d%m%y`
TWPRINT="/opt/tripwire/sbin/twprint -m r -r"
TWREPORT=/opt/tripwire/reports
TWHOSTNAME=`hostname`.twr
TWASCII=/tmp/${TWHOSTNAME}.txt
COMP_DIR=${TWREPORT}/COMPRESSED
TWLOG=/tmp/tripwire.tmp
MAILSUB="${HOSTNAME} Report ${DATE}"


get_ascii_report(){
${TWPRINT} ${TWREPORT}/${TWHOSTNAME} > ${TWASCII}
}

section(){
cat ${TWASCII} | sed -n "/$1/,/$2/p" >> ${TWLOG}
echo " " >> ${TWLOG}
}

cleanup(){
if [ -f ${TWLOG} ]
then
rm -f ${TWLOG}
fi
# Remove fies older than 1 month
find ${COMP_DIR} -type f -mtime +31 -exec rm {} \; 2>/dev/null
}

compress_logs(){
testdir ${COMP_DIR}
gzip -c ${TWASCII} > ${COMP_DIR}/${TWHOSTNAME}.${DATE}.gzip 2>/dev/null
}

testdir(){
if [ ! -d ${1}/ ]
then
mkdir ${COMP_DIR}
fi
}

header(){
echo "Tripwire Report for ${DATE}." >> ${TWLOG}
echo " " >> ${TWLOG}
echo "Please ensure you have checked the report for Errors." >> ${TWLOG}
echo "Also ensure the report is checked for any Severity levels higher
than 0." >> ${TWLOG}
echo "The Full Report can be found at" ${TWASCII} >> ${TWLOG}
echo "Use # zcat ${COMP_DIR}/${TWHOSTNAME}.${DATE}.gzip to view log."
>> ${TWLOG}
echo " " >> ${TWLOG}
}

mailto(){

MAILSUB="Tripwire Report for ${HOSTNAME} ${DATE}"
RECP=paul.ward@datacom.co.nz
mail -s "${MAILSUB}" ${RECP} < ${TWLOG}
}

# Script begins
cleanup

get_ascii_report

header
echo "Rule Name Severity Level Added
Removed Modified" >> ${TWLOG}
section "Tripwire Data Files" "Total violations found:"
section "Error Report:" "End of report"
compress_logs
mailto

Monday, 2 February 2009

How to remove last line of the file - Shell Programming and Scripting - The UNIX and Linux Forums

The first piece of code allows you to cut a whole block of text from one file to the screen or another file.
eg, you may wish cut a block of text from a log file or even web page using the links -dump command instead of cat.

The script works by matching the text in the first sed statement and then the last text and outputs it to screen.
The last sed removes the last line of text from the output.


cat filename.txt | sed -n "/^first line to to cut/,/last lne to cut/p"| sed '$d'

Shell Script or perl help. to write sections of a log to a tmp file for mailing - LinuxQuestions.org

I came across an issue recently where I needed to copy a whole section of lines from a log file into a daily report.
I devised some script to get line numbers and work what lines my section starts and finishes on then uses tail and head to get this text.

Then some bright spark said try this sed code, and its works great.

Thanks blackhole64!


sed -n \"/^Rule Summary:/,/^Total violations found:/p\"

Sunday, 1 February 2009

Array in Shell Script - Shell Beginner

Some of the important operations with array in BASH.
Here is some code for using arrays in bash.


#!/bin/sh

array=(jerry gen glan rahim)
len=${#array[*]} #Number of elements of the array

echo \"The array has $len members. They are:\"

i=0
while [ $i -lt $len ]; do
echo \"$i: ${array[$i]}\"
let i++
done

#Some operations
echo \"Adding \"jack\" to the end of the array\"
array=( \"${array[@]}\" \"jack\" )
echo \"Listng all the elements in the array\"
echo \"${array[@]}\"
echo \"Listng all the elements in the array\"
echo \"${array[*]}\" #One more way
echo \"Deleting \\\"gen\\\"\"
unset array[1] #Same as array[1]=
echo \"Now the array is\"
echo \"${array[@]}\"
echo \"length of 3rd element in the array\"
len1=${#array[2]}; echo $len
echo \"Deleting the whole array\"
unset array
echo \"${array[@]}\" #Array is empty


Otput should look like the following.

$ ./array.sh
The array has 4 members. They are:
0: jerry
1: gen
2: glan
3: rahim
Adding jack to the end of the array
Listng all the elements in the array
jerry gen glan rahim jack
Listng all the elements in the array
jerry gen glan rahim jack
Deleting \"gen\"
Now the array is
jerry glan rahim jack
length of 3rd element in the array
4
Deleting the whole array