Operating System - HP-UX
1834827 Members
2221 Online
110070 Solutions
New Discussion

Script to get data from wtmp

 
SOLVED
Go to solution
Bob_165
Frequent Advisor

Script to get data from wtmp

OK script wizards---Here’s what I’m trying to do:

On the first of every month (via cron) append a report called ‘secure’ with anyone who logged in etc via console or accessed a root command the previous month. I need to also make sure it’s the current year.

I know how to get the year from wtmp you need to convert it:
/usr/sbin/acct/fwtmp /var/adm/tmp2

cat /var/adm/tmp2 root console | egrep '$previous_month|$current year' | egrep ‘root|console’ >> /var/adm/secure

How do I sent the variables for previous_month and current_year?

This should be easy..
I tried current_year=’date “+Y%”’ but it doesn’t work and I don't have a clue on pervious_month.
…HELP!

Thanks!!!
8 REPLIES 8
A. Clay Stephenson
Acclaimed Contributor

Re: Script to get data from wtmp

#!/usr/bin/sh

typeset -i10 MO=1
typeset -i10 YR=0

date '+%m %Y' | read MO YR
((MO -= 1))
if [[ ${MO} -lt 1 ]]
then
MO=1
((YR -= 1))
fi
echo "Previous Month: ${MO} Year: ${YR}"

If it ain't broke, I can fix that.
James R. Ferguson
Acclaimed Contributor

Re: Script to get data from wtmp

Hi Bob:

# current_year=`date +%Y`

For the previous month name, constuct an array of names and index by the value of the current month less one. Wrap-around if the current month is the last of a year.

# previous_month=`perl -le '@name=qw{Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec c};$m=(localtime(time))[4];print $name[$m>0?$m-1:11]'`

...I chose to use Perl since I can derive the current month (zero-relative) and perform the lookup in one easy swoop.

Regards!

...JRF...
Bob_165
Frequent Advisor

Re: Script to get data from wtmp

Clay-

Thanks but it comes back with:
Previous Month: 4 Year: 2007
Which yes 4 is previous to 5.

What Iâ m really looking for in the variable is the three char month i.e May, Aug since thatâ s what I get out of wtmp converted to asci.

So close!


James--The date works perfect..

Can you do the previous month in a shell (not perl) in the three char format.

Thanks again..
James R. Ferguson
Acclaimed Contributor

Re: Script to get data from wtmp

Hi (again) Bob:

> Can you do the previous month in a shell (not perl) in the three char format.

OK, but Perl is more fun :-)

# cat ./lastmonth
#!/usr/bin/sh
typeset -i M=`date +%m`
set -A name Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
(( M -= 2 ))
if [ ${M} -lt 0 ]; then
M=11
fi
echo ${name[M]}
exit 0

...simply run:

# ./lastmonth
Apr

Regards!

...JRF...
Bob_165
Frequent Advisor

Re: Script to get data from wtmp

I've tried a few more things; but sadly shell programming isn't my forte:
previous_month=$(expr`date +%m` -1)
sh: expr05: not found.
/ # previous_month=`date +%m -1`
/ # echo $previous_month
05

Sigh!

It now occurs to me that I'll run into a problem when I try and get the data Jan for Dec data. I'll have previous month..but then I'll need previous year.

Double Sigh!!

Thanks again for any help
James R. Ferguson
Acclaimed Contributor

Re: Script to get data from wtmp

Hi Bob:

Well, one way to compute the previous year is:

# typeset -i Y=`date +%Y`;(( Y -= 1 ));echo $Y

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor
Solution

Re: Script to get data from wtmp

Hi Bob:

...or if you like using 'expr' (which is slow and which causes the shell to invoke a new process):

# Y=`expr $(date +%Y) - 1`
# echo ${Y}

Regards!

...JRF...
Bob_165
Frequent Advisor

Re: Script to get data from wtmp

Thanks James & Clay..

Perfect!!!!