parsing a file + cron email job
I've got an assignment i'm bummed on. It involves parsing a certain log file, appending new records on that log file to an email that crontab sends out at midnight everynight to three people.
This log files keeps track of when someone su's to root, but I'm not quite sure where this file is. Also, can someone give me or point me to a website with a good example crontab explaination or something that shows me how crontab works? I've never used it before =/
Oh, and I also don't know how to parse a file for new entries, and then copying and pasting those entries into a new document/email.
Any help would be appreciated! I'm really suck =[
edit: Also, a few other things...
1. no email should be sent out if there haven't been su - root 's in the past 24 hours.
2. logs should be kept for 4 weeks, although emails should not include duplicates.
3. A direct quote from my assignment - "As root you will not run this from an installed crontab but
instead you will have it run in the typical way that root does." I haven't the slightest inkling what this means -____-
I'm going to give you a few things to read, which will help you do this assignment
The log file is probably /var/log/secure, but your lecturer should have told you where it is or at least what its called.
See also /var/log/messages.
You can use the find cmd http://linux.die.net/man/1/find to find a file on a system.
You're going to need to learn bash (shell scripting lang) so
I agree that last remark is meaningless as stated; I assume(!) it was in the context of the lecture.
He may mean that as well as the system crontabs, root (like all users) also has his own personal crontab.
Try looking under /var/spool/cron and/or see the crontab link I gave you.
Heres an update on what I've done:
I've done "crontab -e" to edit the crontab list, what I have right now is "05 00 * * * ./foo1.sh"
I'm thinking I'll be running some script at 12:05am every day, regardless of date month or year or anything like that.
I learned how to find my home directory (lol i had no idea how to figure that out before) with echo $HOME,
I went to my home directory and did "touch foo1.sh"
Here's something I'd like clarified... I read that crontab sends out an email to root everytime a job executes properly. Since I need to send an email to my instructor smauney, me jhong23, and root, would the other two emails to smauney and jhong23 be sent out via my bash script? Also, the confirmation email being sent by crontab isn't even the email that's supposed to be sent actually, the email that's supposed to be sent should have a list of times someone su-ed root - which I'm guessing my bash script should help me do. Could you confirm this?
Sorry for all these questions, but I really appreciate any help!
So, I've taken a look at the bash links you gave me Chris, and I'm having a bit of a problem narrowing down which sections I need to read.
Here's some pseudocode (I've taken C), if you or someone else could kindly help me figure out the exact syntax that would be great!
What I know so far is that there are no headers in bash, or formal declaration of variables or their types. some nuances here and there, and the end of the code isn't a return 0 or end or something.
#no headers, so we're skipping the .h's
#skipping the int main() as well
#checks to see if anyone has su-ed root
y = grep root <su root log file>;
#yes, someone has su-ed to root
mail -s "users su-ed to root" user1@hostname1, user2@hostname2, user3@hostname3;
#no, noone has su-ed to root OR just finished the yes part
end (whatever end is in bash)
Well, you should have noticed that (ideally) a shell script starts by specifying which shell (there are several) to use. The default in Linux is normal bash thus
To see what the parser is doing, make the next line
Don't try to do it all in one go; get some basic simple stuff working first.
When you've done that, have a go (in stages) at the assignment and come back when you've got some code to show.
Incidentally, cron does not login to your home dir, so
|All times are GMT -5. The time now is 07:51 PM.|