Results 1 to 7 of 7
First of all, yes, this is a homework assignment. I'm not looking for anyone to do my work for me, but as I don't have a Linux system on which ...
- 11-27-2007 #1Just Joined!
- Join Date
- Nov 2007
- Posts
- 4
Looking for script assistance
First of all, yes, this is a homework assignment. I'm not looking for anyone to do my work for me, but as I don't have a Linux system on which to test this script, I would greatly appreciate it if someone could assist me with my errors. As my Linux experience is very limited, I'm sure errors are abound.
I'm writing a script which will simply email a list of all log-ins, good and bad, to the admin on a daily basis. I've looked at many sample scripts online, and hope I've at least gotten a good portion of this correct. One question I have is how to load this script as 'myscript,' but I believe I at least have the correct directory. Here it is:
#!/bin/bash
#This will need to be changed to whatever email address is required
NOTIFY_ADDRESS=admin@localhost
0 4 * * * /var/spool/cron/myscript
tmp=$(mktemp)
last > "$tmp"
lastb >> "$tmp"
mail -s "daily login report" date "$tmp" | NOTIFY_ADDRESS
rm -f "$tmp"
%
My intention is that this script will record all log-in attempts everyday at 4am in a temp directory, email that list to the admin, and delete the temp directory afterward. My fear, other than I've completely screwed up, is that my script is too simple and that I've forgotten something crucial. I'm *not* looking for someone to hand my assignment to me, but instead ask that if you could point me to a source where I can research further and figure it out for myself, I would be most grateful. If, however, I've come *really* close and I'm just missing a small thing, I guess I won't mind if you tell me
- 11-28-2007 #2Just Joined!
- Join Date
- Nov 2007
- Location
- Camp Pendleton
- Posts
- 55
Well, you're really close. Just make sure to put the subject line in quotes so it's evaluated as one argument to /bin/mail.
(On that note, it's good to always use full paths to programs.)
Also, take another look at /bin/mail's arguments. It doesn't directly read a file. You have to pipe the message body to it.
I'm trying not to give everything away. But you should probably grab a Linux live CD for testing. You'd be able to test scripts and not blow out your hard drive...
- 11-28-2007 #3Just Joined!
- Join Date
- Nov 2007
- Posts
- 4
first of all, thanks for the quick response! that's awesome.

I've made one change which I think addresses what you mentioned regarding piping the message to /bin/mail. I replaced
mail -s "daily login report" date "$tmp" | NOTIFY_ADDRESS
with
mail NOTIFY_ADDRESS -s "Daily Login Report" <"$tmp"
also, after testing various commands on Cygwin, it appears that I don't need to type ' "$tmp" ' but only ' tmp ' and it will work fine (without 's, of course). still working on it, but again, thanks a lot for the help mrjohnson
- 11-28-2007 #4Just Joined!
- Join Date
- Nov 2007
- Location
- Camp Pendleton
- Posts
- 55
Hi,
Don't forget NOTIFY_ADDRESS is a variable... Run like you have it there, /bin/mail would send email to "NOTIFY_ADDRESS".
- 11-28-2007 #5Just Joined!
- Join Date
- Nov 2007
- Posts
- 4
yes, but in my original script it states that NOTIFY_ADDRESS would be recognized as admin@localhost, or whatever the appropriate address would be. (wouldn't it?)
also, you mentioned full paths are better, so I changed
last > "$tmp" to /var/log/wtmp > "$tmp"
and lastb > "$tmp" to /var/log/btmp > "$tmp"
so now the script appears as:
#!/bin/bash
#This will need to be changed to whatever email address is required
NOTIFY_ADDRESS=admin@localhost
0 4 * * * /var/spool/cron/myscript
tmp=$(mktemp)
date > "$tmp"
/var/log/wtmp >> "$tmp"
/var/log/btmp >> "$tmp"
mail NOTIFY_ADDRESS -s "Daily Login Report" <"$tmp"
rm -f "$tmp"
%
oh, and I changed it as well to show the date in the email itself and not in the subject line, as you can see. Again, the assignment is on the written proposal of the script with a detailed explanation for each line, not just the script itself, but I wanted to be sure that my script would work as designed (although I'm not sure how much of my grade depends upon that). If there's anything else you see that I should take another look at, please let me know. You've been a great help.
- 11-28-2007 #6Just Joined!
- Join Date
- Nov 2007
- Location
- Camp Pendleton
- Posts
- 55
No, I meant you need to use $NOTIFY_ADDRESS (forgot the $).
Also, I didn't mean paths to the files. The contents of /var/log/wtmp isn't going to be very useful. Keep last and lastb like you had it...
- 11-29-2007 #7Just Joined!
- Join Date
- Nov 2007
- Posts
- 4
oh, heh

changed back to last and last b, and added $ to the mail argument... thanks for that, it's the little details that get you
here's what I'll be using for my proposal then:
#!/bin/bash
#This will need to be changed to whatever email address is required
NOTIFY_ADDRESS=admin@localhost
0 4 * * * /var/spool/cron/myscript
tmp=$(mktemp)
date > "$tmp"
last >> "$tmp"
lastb >> "$tmp"
mail $NOTIFY_ADDRESS -s "Daily Login Report" <"$tmp"
rm -f "$tmp"
%
again, many many thanks. i'd gladly return the favor if i could


Reply With Quote