LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 11-27-2003, 07:11 PM   #1
NIkss
LQ Newbie
 
Registered: Nov 2003
Posts: 4

Rep: Reputation: 0
Shell Script to control logins


Hello there this is my first post!!

I need a shell script that checks the time that every user is logged to the machine and when the time expires it would send him a message and logged him out...
If anyone could help me............


Sorry for my poor english....
 
Old 11-28-2003, 02:30 PM   #2
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 64
You could look at the output from:
who -u

and use either the idle time or login time to choose who to log off.
 
Old 11-30-2003, 05:40 AM   #3
hiteshmaisheri
Member
 
Registered: Nov 2003
Location: Kerala, India
Distribution: Red Hat, Knoppix, Mandrake, FreeBSD
Posts: 231

Rep: Reputation: 30
You can check the output with the w command to get the details regarding what each user is doing right now
 
Old 12-09-2003, 11:13 AM   #4
NIkss
LQ Newbie
 
Registered: Nov 2003
Posts: 4

Original Poster
Rep: Reputation: 0
My problem is that the w or who tells me the time tha each user logged in, not the minutes the user is connected to the machine.
I don't know how to caclulate the time each user is connected.
for example i want to disconnect any user when 30 minutes expires.
 
Old 12-09-2003, 12:46 PM   #5
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 64
Just compare it to the output of "date". Do you want it to be 30 minutes idle time or 30 minutes total time?
 
Old 12-09-2003, 02:49 PM   #6
NIkss
LQ Newbie
 
Registered: Nov 2003
Posts: 4

Original Poster
Rep: Reputation: 0
I want it to be 30 min total time.
I know how to take the logins from who and put them to a file but i don't know how to compare the time that he logged in with current time.
I think i must do it with awk but i search the net i see examples but i can't understand how awk works....
 
Old 12-09-2003, 04:26 PM   #7
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 64
I just wrote this of the top of my head but it should work. I suggest you test it first though:
Code:
#!/bin/bash
nmin=`date +%M`
nhour=`date +%H`
nmin=$((nmin-30))
if [ $nmin -lt 0 ];then
nmin=$((nmin+60))
hour=$((nhour-1))
fi
if [ $nhour -lt 0 ];then
nhour=$((nhour+24))
fi
IFS="
"
for line in `who -u`;do
echo "     $line"
un=`echo $line|awk {'print $1'}`
pid=`echo $line|awk {'print $7'}`
time=`echo $line|awk {'print $5'}`
min=`echo $time|cut -d: -f2`
hour=`echo $time|cut -d: -f1`

echo $hour:$min - $nhour:$nmin
if [ $min -lt $nmin ];then
echo Killing $un - $pid
kill $pid
fi
done;
 
Old 12-10-2003, 01:10 AM   #8
paonethestar
Member
 
Registered: Oct 2003
Posts: 47

Rep: Reputation: 15
Hey,
Why don't we use the command "finger" that shows ideal time and login time .By that we can solve our prob through small shell script!.Bye
 
Old 12-17-2003, 11:00 AM   #9
NIkss
LQ Newbie
 
Registered: Nov 2003
Posts: 4

Original Poster
Rep: Reputation: 0
Thanks for the script!!!
I have done a few changes and now it is ok.
Code:
#!/bin/bash
nmin=`date +%M`
nhour=`date +%H`
nmin=$((nmin-30))
if [ $nmin -lt 0 ];then
nmin=$((nmin+60))
nhour=$((nhour-1))
fi
if [ $nhour -lt 0 ];then
nhour=$((nhour+24))
fi
IFS="\n"
for line in `who -u`;do
echo "     $line"
un=`echo $line|awk {'print $1'}`
pid=`echo $line|awk {'print $7'}`
time=`echo $line|awk {'print $5'}`
min=`echo $time|cut -d: -f2`
hour=`echo $time|cut -d: -f1`

echo $hour:$min - $nhour:$nmin
if [ $hour -lt $nhour ];then
    echo Killing $un - $pid
    kill $pid
elif [ $nhour -lt $hour ];then
     :
else
     if [ $min -lt $nmin ];then
        echo Killing $un - $pid
        kill $pid
     fi
fi
done;
but the output is something like that :
: - 06:3
test_2:line 22:[ : -lt : unary operator expected
test_2:line 28:[ : -lt : unary operator expected
Killing ikos -3037
............
line 22,28 are the 2 if but it runs just fine!
It should be Killing Nikos -3037
I lose "N" the first character why that???

Last edited by NIkss; 12-17-2003 at 06:27 PM.
 
Old 12-17-2003, 12:53 PM   #10
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 64
For some reason that "IFS" newline has never worked for me try it back with:
IFS="
"
 
  


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
Shell script inside shell script treotan Linux - General 4 02-19-2009 06:34 AM
Shell Scripting: Getting a pid and killing it via a shell script topcat Programming 15 10-28-2007 02:14 AM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 05:32 PM
control / monitor dhcp logins pasta_fetuccini Linux - General 1 04-17-2004 12:22 AM
[SHELL SCRIPT] Write at the right of the shell window Creak Linux - General 2 04-02-2004 03:00 PM


All times are GMT -5. The time now is 05:20 PM.

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