Enviado em 17/05/2015 - 18:43h
olá pessoal, tudo bem?#!/bin/bash
WEEKDAYS=("mon" "tue" "wed" "thu" "fri" "sat" "sun")
block_data=\
"block, mon tue wed thu fri, 00:00-05:59;"\
"block, sat sun, 01:00-05:59;"
updatetime=10 #secs
started=false
execute() {
name="$1"
day="$2"
from_time="$2"
to_time="$3"
if [ "$name" == "block" ]; then
echo "conexão com internet desativada!!!"
#iptables -I INPUT -j REJECT
#iptables -I FORWARD -j REJECT
#iptables -I OUTPUT -j REJECT
elif [ "$name" == "none" ]; then
echo "conexão com internet ativada!!!"
#iptables -I INPUT -j ACCEPT
#iptables -I FORWARD -j ACCEPT
#iptables -I OUTPUT -j ACCEPT
fi
}
scheduleCalendar() {
count=0
OIFS=$IFS; IFS=";"
for line in $block_data; do
Ok=false
OIFS=$IFS; IFS=","
range_value=($line)
name=${range_value[0]#* }
days=(${range_value[1]#* })
IFS=$OIFS
from_hour=""
from_minute=""
to_hour=""
to_minute=""
OIFS=$IFS; IFS="-"
arr=(${range_value[2]#* })
for s in ${arr[*]}; do
if [ -z "$from_hour" ]; then
from_hour=${s%%:*}
from_minute=${s##*:}
else
to_hour=${s%%:*}
to_minute=${s##*:}
fi
done
IFS=$OIFS
h=`date +%H`
m=`date +%M`
u=$((`date +%u`-1))
OIFS=$IFS; IFS=" "
for day in ${days[*]}; do
if [ "${WEEKDAYS[$u]}" == "$day" ]\
&& [ $h$m -ge $from_hour$from_minute ]\
&& [ $h$m -le $to_hour$to_minute ]; then
Ok=true
execute $name $day\
$from_hour:$from_minute\
$to_hour:$to_minute
fi
done
IFS=$OIFS
count=$(($count + 1))
done
IFS=$OIFS
if [ $Ok == false ]; then
execute "none" ${WEEKDAYS[$u]} $h:$m ""
fi
}
start() {
started=true
while $started; do
scheduleCalendar
sleep $updatetime
done
}
stop() {
started=false
}
case $1 in
start)
start;;
stop)
stop;;
restart)
stop; start;;
esac