Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
[HOWTO] Limit Kids Computing Time (bash script)
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks
View previous topic :: View next topic  
Author Message
RinkyDinksRJ
n00b
n00b


Joined: 19 Aug 2007
Posts: 13

PostPosted: Thu Apr 07, 2016 6:07 am    Post subject: [HOWTO] Limit Kids Computing Time (bash script) Reply with quote

With one computer in the household (mine) and my girlfriends 3 kids constantly fighting over it I've been getting massive headaches. I did explore various PAM modules but found them too time consuming to set up or outdated. I put together this bash script that is executed when their KDE session starts and once they utilize their time for the day they won't be getting back on. It's particularly useful for when we're working on the weekend and they are at home trying to run up my electric bill.

Feedback is welcome. If you want support for another desktop point me in the right direction as far as commands go (I only use KDE). I would need a command to force user log off and a command to handle notifications.

Code:

[b]Usage[/b]
This script should be pasted into /usr/bin/lockout.sh and marked executable.

Set the script to be run locally (user level) at login:
(KDE) System Settings -> Workspace -> Startup and Shutdown -> Autostart -> Add Script...

Set the script to be run globally at login:
Copy the below desktop entry into /usr/share/autostart/lockout.desktop. chmod 0644


Code:

[Desktop Entry]
Exec=/usr/bin/lockout.sh
Type=Application
Name=Session Time Limiter
X-KDE-StartupNotify=false
X-KDE-autostart-phase=1
NoDisplay=true


Code:

#!/bin/bash
#/usr/bin/lockout.sh
#
#======================================================
#Traits:
#Only runs the script if user is in the "child" group
#Works against `last` command (/var/log/wtmp) for info
#Has a variable session time limit based on day of week
#Only allows sessions to be started at certain times of the days
#Will cut the session off if it becomes too late in evening
#Sends a text message (configurable) when user attempts bad login
#
#Agenda:
#Add password dialog to allow time to be extended
#Send email message when user attempts bad login
#Add support for other desktops (need help on this)
#
#======================================================
#Configuration
#======================================================

#How much time is allowed on schoolnights versus weekends
schoolnighttime=30
weekendnighttime=60

#Define your schoolnights here
declare -a schoolnights=("Sun" "Mon" "Tue" "Wed" "Thu")

#Latest and earliest time allowed to start session (military time)
earlytime=07:30
latetime=21:30
schoollatenighttime=19:00

#Support for text notification of unauthorized session attempts
#Requires curl support and uses textbelt.com api
#Options: text (USA), intl, canada, off
texting=off
textnumber=1112229999

#Support for email notification of unauthorized session attempts
#Requires net-mail/email  --- not finished
#Options: on, off
emailing=off

#======================================================
#Functions
#======================================================

array_contains () {
    local array="$1[@]"
    local seeking=$2
    local in=1
    for element in "${!array}"; do
        if [[ $element == $seeking ]]; then in=0; break; fi
    done
    return $in
}

killsession () {
    #Force log out
    qdbus org.kde.ksmserver /KSMServer logout 0 0 0
    exit 0;
}

startsession () {
    #Let user know how long there session will last
    kdialog --title "$currentuser's Game Time" --msgbox "You have $lockouttime minutes of game time."

    sessiontime=$(($sessiontime-3))
    sleep $sessiontime; kdialog --title "$currentuser's Game Time" --passivepopup "3 minutes remaining" 5
    sleep 60; kdialog --title "$currentuser's Game Time" --passivepopup "2 minutes remaining" 5
    sleep 60; kdialog --title "$currentuser's Game Time" --passivepopup "1 minute remaining" 5
    sleep 60; killsession;
}

notifytext () {
    if [[ $texting == "off" ]]; then return;
    elif [[ $texting == "text" ]]; then
        curl http://textbelt.com/text -d number=$textnumber -d "message=$1";
    elif [[ $texting == "intl" ]]; then
        curl http://textbelt.com/intl -d number=$textnumber -d "message=$1";
    elif [[ $texting == "canada" ]]; then
        curl http://textbelt.com/canada -d number=$textnumber -d "message=$1";
    fi
}

#notifyemail () {
#    if [[ $emailing == "off" ]]; then return;
#    else
       #-from-addr
       #-from-name   
       #-subject subject
       #-smtp-server server
       #-smtp-port port
       #-smtp-auth type

       #Bad idea
       #-smtp-user username
#      #-smtp-pass password
#    fi
#}

#======================================================
#Script
#======================================================

currentuser=`whoami`

#If user is not a member of child group then exit script
if groups $currentuser | grep -v &>/dev/null '\bchild\b'
then
    exit 0;
fi


#Determine the last login time for this user
#then convert to epoch seconds
lastlogin=`last $currentuser --time-format iso -n1|head -n1|cut -c40-64`;
lastlogin=$((`date -d"$lastlogin" +%s`));

#Odd cornercase: if user has never logged in (i.e. new user)
#set variable manually to something non-empty
if [ -z "$lastlogin" ]; then
    lastlogin=1;
fi

#Get information about current day/time
currentdayofweek=`date +%a`; #(Mon Tue Wed) Format
currenttime=`date +%s`;

#24hrs*60min*60sec=86400
#Get previous midnight in epoch seconds
prevmidnight=$(bc <<< "scale=0; $currenttime/86400"); #in days
prevmidnight=$(bc <<< "$prevmidnight*86400"); #in seconds

#If user already logged in today then kill
if (( "$lastlogin" > "$prevmidnight" )); then
    kdialog --title "Warning:" --passivepopup "You already used your time today. Session will end." 5;
    text="$currentuser attempted another session at $currenttime"; notifytext $text;
    sleep 60; killsession;
fi

$earlytime=`echo $earlytime | awk -F: '{ print ($1 * 3600) + ($2 * 60)}'`;
$latetime=`echo $latetime | awk -F: '{ print ($1 * 3600) + ($2 * 60)}'`;
$schoollatenighttime=`echo $schoollatenighttime | awk -F: '{ print ($1 * 3600) + ($2 * 60)}'`;

#Assign session time based on day of week
if array_contains schoolnights "$currentdayofweek"; then
    sessiontime=$schoolnighttime;
    latetime=$schoollatenighttime; else
    sessiontime=$weekendnighttime;
fi

#If session is started too late in the evening then kill
if (( "$currenttime" > "$latetime" )); then
    kdialog --title "Warning:" --passivepopup "Too late in evening. Session will end." 5;
    text="$currentuser tried to login at $currenttime."; notifytext $text;
    sleep 60; killsession;
#If session is started too early in the morning then kill
elif (( "$currenttime" < "$earlytime" )); then
    kdialog --title "Warning:" --passivepopup "Too early in morning. Session will end." 5;
    text="$currentuser tried to login at $currenttime."; notifytext $text;
    sleep 60; killsession;
#If session is started too late in evening reduce session time
elif (( "$currenttime" + "$sessiontime" > "$latetime" )); then
    sessiontime=$(($latetime-$sessiontime));
fi
   
startsession;


Last edited by RinkyDinksRJ on Sun Apr 10, 2016 10:20 pm; edited 1 time in total
Back to top
View user's profile Send private message
Syl20
l33t
l33t


Joined: 04 Aug 2005
Posts: 619
Location: France

PostPosted: Thu Apr 07, 2016 1:55 pm    Post subject: Re: [HOWTO] Limit Kids Computing Time (bash script) Reply with quote

RinkyDinksRJ wrote:
I did explore various PAM modules but found them too time consuming to set up or outdated (snip)
No, it's not very secure and is fairly easy to bypass if they knew how *nix works and could manage to find this file.

You can avoid setting a file (easy to modify, because the user owns it) by using the "last" and "date" commands. Iast reads /var/log/wtmp, which is read-only for everyone but root and the wtmp group.

To restrict the allowed logon hours, the pam_time module will help you.
Back to top
View user's profile Send private message
MarioCorleone
Guru
Guru


Joined: 29 Jun 2003
Posts: 336

PostPosted: Thu Apr 07, 2016 6:08 pm    Post subject: Reply with quote

What a great idea! Do you mind if I implement that on my PC's? My 4-1/2 year old daughter is getting too smart, way too fast!
_________________
-Mario
Back to top
View user's profile Send private message
Cyker
Veteran
Veteran


Joined: 15 Jun 2006
Posts: 1746

PostPosted: Thu Apr 07, 2016 7:43 pm    Post subject: Reply with quote

Aww, this is neat ^_____^
Back to top
View user's profile Send private message
khayyam
Watchman
Watchman


Joined: 07 Jun 2012
Posts: 6227
Location: Room 101

PostPosted: Thu Apr 07, 2016 9:18 pm    Post subject: Reply with quote

mario18 wrote:
What a great idea! Do you mind if I implement that on my PC's? My 4-1/2 year old daughter is getting too smart, way too fast!

mario18 ... and by Moore's Law she'll have figured a way around the block in about one or two weeks ;)

best ... khay
Back to top
View user's profile Send private message
NeddySeagoon
Administrator
Administrator


Joined: 05 Jul 2003
Posts: 54096
Location: 56N 3W

PostPosted: Thu Apr 07, 2016 9:23 pm    Post subject: Reply with quote

khayyam,

:) I was going to give her until she was 5.
_________________
Regards,

NeddySeagoon

Computer users fall into two groups:-
those that do backups
those that have never had a hard drive fail.
Back to top
View user's profile Send private message
khayyam
Watchman
Watchman


Joined: 07 Jun 2012
Posts: 6227
Location: Room 101

PostPosted: Thu Apr 07, 2016 9:45 pm    Post subject: Reply with quote

NeddySeagoon wrote:
I was going to give her until she was 5.

Neddy ... always the pessimist, never the bride ;)

best ... khay
Back to top
View user's profile Send private message
MarioCorleone
Guru
Guru


Joined: 29 Jun 2003
Posts: 336

PostPosted: Thu Apr 07, 2016 10:46 pm    Post subject: Reply with quote

khayyam wrote:
mario18 wrote:
What a great idea! Do you mind if I implement that on my PC's? My 4-1/2 year old daughter is getting too smart, way too fast!

mario18 ... and by Moore's Law she'll have figured a way around the block in about one or two weeks ;)

best ... khay


No doubt! If she is anything like me, then I'm in for a world of trouble :)
_________________
-Mario
Back to top
View user's profile Send private message
RinkyDinksRJ
n00b
n00b


Joined: 19 Aug 2007
Posts: 13

PostPosted: Sun Apr 10, 2016 5:03 am    Post subject: Reply with quote

Well I personally feel if they figure out a way around it they can have as much computer time as they want.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum