1832647 Members
2694 Online
110043 Solutions
New Discussion

Script assistance

 
Nick D'Angelo
Super Advisor

Script assistance

All,

I have a script that runs every hour:

userse=`who -u | grep 10.10. | wc -l `
who -u|grep 10.10|sort -k6

This shows me the users idle time sorted.

I then need to look in a /database/dbname.log file to see if they are still logged in. I do this manually now and this is what I want to automate.

In the log file, it shows the following:

08:55:28 Usr 9: Login by userid on /dev/ttyqa. (452)

I then run a manual script that emails a warning to the idle users to please logoff if not in use. This script emails users that have been idle for greater than 1 hour:

get_user()
{
USER=$(echo $LINE|awk '{print $1}')
IDLE=$(echo $LINE|awk '{print $6}')
if [ "$IDLE" = "old" ]
then
echo $MESSAGE |mailx -s $SUBJECT nickd
else
IDLE_HOURS=$(echo $IDLE|awk '{FS=":";print $1}')
if [ "$IDLE_HOURS" -ge 1 ]
then
#echo $MESSAGE |mailx -s $ SUBJECT $USER
#echo "Subject: Idle user Report" >>/tmp/idlemail.$$
#cat /usr/local/bin/scripts/idlemail >>/tmp/idlemail.$$
#sendmail nickd sendmail $USER fi
fi
}
who -u|while read LINE
do
get_user $LINE
done

I would like to enhance my script so it looks for idle users > than 1 hour, looks in the log and if the users does not have the word logout in the entry, then it emails them and it also copies the userid to a log file somewhere ie /home1/nickd/idleuser.log.

This will allow me to keep track of users and log them as they are not abiding by our policies.

Your assistance is appreciated.

Script newbie.
Nickd
Always learning
5 REPLIES 5
Thom Cornwell
Frequent Advisor

Re: Script assistance

Have you investigated the SHELL variable TMOUT? It allows for logging out users after idle time has reached the value set in TMOUT. Hope this helps
--
Thom
Steven E. Protter
Exalted Contributor

Re: Script assistance

TMEOUT variable works great here for users on a $ prompt.

Most of our users are in telnet app and TMEOUT doesn't help with that.

I find your script pretty innovative already.

If I were to let the sendmail go to the users' they'd get pretty mad. They violently defend their right to log in and not log out here.

Why do you think the word logout is going to be anywhere in a log?

Anyway, I'm taking your script and running a few tests on it to see if I can help you with it.

P
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Steven E. Protter
Exalted Contributor

Re: Script assistance

Is the logout command in that file
/database/dbname.log ?

If so, you can pull the username out of the output from your first line and then

grep $USERNAME | grep -v logout

This will get the username line if it doesn't have logout in it.

At this point you'd be well on your way to making this really slick script work.

When I ran your command line on my user system the value I get in the $userse variable is 0

The screen ouput shows all users, idle or not, sorted in ascending order, which is what you want. Am I getting a valid value in $userse?


It would be nice to see what dbname.log has in it so I can play with this. Just a sample example, I can play with.

P
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Nick D'Angelo
Super Advisor

Re: Script assistance

Thanks Steven and all.

here are my two scripts that I run - zipped with winzip

the first one is ewusers
it shows me all the users in our NJ building, based on IP address sorted by most idle time

The second one is the idle.sh script that checks for all users that are idle for more than 1 hour, regardless of whether they are in our db app or not and sends them a nice email to please logoff when not in use.

This is what our log file looks like:

5:06:30 Usr 15: Logout by petrecca on /dev/ttypd. (453)
15:09:58 Usr 15: Login by rarcoman on /dev/ttyr9. (452)

This means taht petrecca has logged off the db application and is cool, but the user rarcoman is logged on but not necessarily off.

I know this is a tricky script/process and I do appreciate your help.

By the way, you were right. The TMOUT is dangerous when it comes to db's. If the users are logged into the db application and sitting idle in the middle of editing a record, just think of the trouble that can be caused by a disconnect by the Operating system.

Cheers,


Always learning
Nick D'Angelo
Super Advisor

Re: Script assistance

Steve, are you still there?
Always learning