LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-03-2003, 03:01 PM   #1
xscousr
Member
 
Registered: Jul 2003
Location: Toronto
Distribution: Redhat
Posts: 89

Rep: Reputation: 15
bash script question


I have written a script to compare a file containing the md5sums of all files in a directory. I would like to compare this file with a master file of the same type to monitor that these files are not changed.
There will be many authorised changes made via scripting and a new master file will be created after each authorised change. Using tripwire it just goes batty - hence the need for a script (before anyone asks :-)

Anyway - it's bailing at the if statement, what it's supposed to do is check whether if a is = to b then quit else run ...

Here's the script - thanks for any help.

#!/bin/bash
# create md5 list and compare to master list
# list files that have been changed (if any) and mail admins
#
##########################
# Set variables
#########################1#
master="/tmp/master"
compare="/tmp/compare"
changed="/tmp/changed"
notify="/tmp/notify"
EMAILLIST="user@domain"
#############################
#Path to programs
#############################
diff="/usr/bin/diff"
mail="/bin/mail"
cat="/bin/cat"
############################################################
#create compare file
find /home/test/list/ -type f -exec md5sum \{\} \; >/tmp/compare
#compare the two md5 files
if [ "a${master}" != "a${compare}" ]; then
exit 1
else #diff the files and create changed file
diff $master $compare > /tmp/changed
#
echo "^MFiles that have been changed:" >$notify
echo "^M" >>$notify
cat $changed |awk ' { print $3 }' >> $notify
mail -s "Unauthorized Website Change" ${EMAILLIST} < $notify
#clean up files
#rm $compare $changed $notify
fi

Last edited by xscousr; 07-03-2003 at 03:03 PM.
 
Old 07-03-2003, 03:31 PM   #2
xscousr
Member
 
Registered: Jul 2003
Location: Toronto
Distribution: Redhat
Posts: 89

Original Poster
Rep: Reputation: 15
fixed it - here is the working script for anyone who is curious
(btw - the ^M is a line break created by ctrl-v ctrl-M)

#!/bin/bash
# create md5 list and compare to master list
# list files that have been changed (if any) and mail admins
#
#########################
# Set variables
#########################
master="/tmp/master"
compare="/tmp/compare"
changed="/tmp/changed"
notify="/tmp/notify"
EMAILLIST="user@domain.com"
#########################
#create compare file
#
find /home/user/test/ -type f -exec md5sum \{\} \; >/tmp/compare
#compare the two md5 files
if [ "a${master}" = "a${compare}" ]; then
echo "Files Are the same."
exit 0
fi
#diff the files and create changed file
diff $master $compare > $changed
#
echo "^MFiles that have been changed:" >$notify
echo "^M" >>$notify
cat $changed |awk ' { print $3 }' >> $notify #strips md5sum and lists only the files
mail -s "Unauthorized Change" ${EMAILLIST} < $notify
#clean up files
rm $compare $changed $notify
 
Old 07-03-2003, 03:36 PM   #3
stickman
Senior Member
 
Registered: Sep 2002
Location: Nashville, TN
Posts: 1,552

Rep: Reputation: 53
Re: bash script question

hmmm... never mind. you fixed it while i was responding....

Last edited by stickman; 07-03-2003 at 03:41 PM.
 
Old 07-03-2003, 03:45 PM   #4
LogicG8
Member
 
Registered: Jun 2003
Location: Long Island, NY
Distribution: Gentoo Unstable (what a misnomer)
Posts: 380

Rep: Reputation: 30
I don't believe you can compare files that way. You should
probably diff them then check the return value of diff to
see if they are different and mail yourself the results if
they are.

diff $master $compare > /tmp/changed
if [ ! $? -eq 0 ]; then
#mail yourself the diffs
fi

Also you probably want to change the /tmp/changed
file to a randomly generated name otherwise you'll
be very vulnerable to a link attack. Initialize it with
something like

compare=changed`dd if=/dev/random bs=1 count=10 2>/dev/null | md5sum | cut -f1 -d" '`

That'll give you a nice unpredictable name, that's just off
the top of my head there's probably a
simpler way to do it but you probably get the idea.
 
Old 07-03-2003, 03:52 PM   #5
xscousr
Member
 
Registered: Jul 2003
Location: Toronto
Distribution: Redhat
Posts: 89

Original Poster
Rep: Reputation: 15
good points LogicG8

thanks.
 
Old 07-03-2003, 05:04 PM   #6
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,279
Blog Entries: 54

Rep: Reputation: 2852Reputation: 2852Reputation: 2852Reputation: 2852Reputation: 2852Reputation: 2852Reputation: 2852Reputation: 2852Reputation: 2852Reputation: 2852Reputation: 2852
//moderator.note: moved to Linux - Programming, not a security issue.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
BASH script question drj000 Programming 11 03-07-2005 10:25 PM
Bash script question deiussum Programming 6 08-14-2004 11:43 AM
bash script question djgerbavore Linux - Newbie 3 07-08-2004 03:39 PM
Bash script question J_Szucs Linux - General 4 05-29-2003 08:48 AM
A bash script question J_Szucs Programming 2 05-13-2003 02:13 AM


All times are GMT -5. The time now is 05:31 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration