1656258048cf

~/.. archív

ksh script for manual internet access.

60 minutes of use allowed.

in plain text

#!/bin/ksh

# this creates a file that will keep track of your daily usage
# should you kill the script before it finishes. It checks the
# date and remaining time of the 60 minutes.

# Do note that the idea is to have the script running the whole time
# and isn't really intended to be killed until your 3600 seconds are
# up. But this should help if you kill it with for example Ctrl - c
touch $HOME/.occ-date
DATE=$(date +%d-%m)

# this trap makes use of Ctrl - C to pause the counter and disable
# your connection.
trap NETOFF 2

# these two variables make sure you start from 0 (with 3600 seconds)
# x pauses and resumes
TIMEFORNET=0
x=1

# this test checks what day it is. If it is the same day, it checks
# how much time you have left.
# if it's the next day (or any other day), restart the counter
if [[ $(cat $HOME/.occ-date | head -n1) != $DATE ]]; then
print $DATE > $HOME/.occ-date
print 0 >> $HOME/.occ-date

elif [[ $(cat $HOME/.occ-date | tail -n1) != 3601 ]]; then
TIMEFORNET=$(cat $HOME/.occ-date | tail -n1)
TIMELEFT=$(((TIMELEFT=3600-TIMEFORNET)))
print "You still have some time left! $TIMELEFT seconds to be exact."

else
NOMORENET
fi

#Function to exit the scripts once time is up
NOMORENET() {
print "Your time is up for today! Start me up tomorrow!"
print "See you tomorrow!"
exit
}

NETOFF() {
# command to stop networking goes here 
# depending on your setup
# you might have to run this script as a super user.


# This would work on OpenBSD, FreeBSD, and Linuxes that make
# use of ifconfig.
# EXAMPLE: (uncomment this and replace 'interface'
# with a valid interface name. if it makes sense for your OS)

# ifconfig interface down 
print "Shutting off internet connection."
x=0
}

NETON(){
#command to resume networking goes here:

#ifconfig interface up
print "Resuming connection"
}

# log current time left and day
WRITESTATE() {
print "$(date +%d-%m)" > $HOME/.occ-date
print "$TIMEFORNET" >> $HOME/.occ-date

}

NETOFF #start the script with connection off

while (( TIMEFORNET < 3601 )); do
print "Hit enter when you begin using the internet."
print "Ctrl-C when you stop"
read
x=1
NETON
while [[ $x != 0 ]]; do #i'm using the internet
        # count time in increments of 10 (thanks, solene)
        # no need to waste power every second
         ((TIMEFORNET=TIMEFORNET+10))
         WRITESTATE
         if (( TIMEFORNET == 3600 )); then
                 NOMORENET
                 NETOFF
         fi
         clear
         sleep 10
         print "$TIMEFORNET out of 3600 seconds."

done
done