LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Shell Script to control logins (https://www.linuxquestions.org/questions/programming-9/shell-script-to-control-logins-120574/)

NIkss 11-27-2003 07:11 PM

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....:(

david_ross 11-28-2003 02:30 PM

You could look at the output from:
who -u

and use either the idle time or login time to choose who to log off.

hiteshmaisheri 11-30-2003 05:40 AM

You can check the output with the w command to get the details regarding what each user is doing right now

NIkss 12-09-2003 11:13 AM

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.

david_ross 12-09-2003 12:46 PM

Just compare it to the output of "date". Do you want it to be 30 minutes idle time or 30 minutes total time?

NIkss 12-09-2003 02:49 PM

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....:(

david_ross 12-09-2003 04:26 PM

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;


paonethestar 12-10-2003 01:10 AM

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

NIkss 12-17-2003 11:00 AM

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???

david_ross 12-17-2003 12:53 PM

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


All times are GMT -5. The time now is 04:23 AM.