Skip to content

Instantly share code, notes, and snippets.

@Kronuz
Created October 3, 2012 22:46
Show Gist options
  • Save Kronuz/3830369 to your computer and use it in GitHub Desktop.
Save Kronuz/3830369 to your computer and use it in GitHub Desktop.
#!/bin/sh
#
# This takes the first rule and makes it randomly active across the day in several rules
#
# For tomato router
# http://infinilogix.com/wordpress/category/network-programming
#
# 1|540|1140|62|||block-site.com$|0|New Rule 1
# Let us take a closer look at what each of these nine fields separated by pipe (|) means.
# The first field shows whether the rule is currently enabled or disabled – 1 means enabled, 0 means disabled.
# The second field gives the start time, i.e. the time to start applying this rule, in minutes elapsed since midnight. In the above example start time is 540 meaning the router should enforce this rule starting at 9am.
# The third field is the end time, i.e. the time to stop applying this rule, again coded the same way as the start time. Both the second and third fields will be -1 if you select the option ‘All Day’ in the control panel.
# The fourth field is the days of week on which the rule should be applied and is coded in binary – 1 for Sunday, 2 for Monday, 4 for Tuesday and so on. For multiple days, add the corresponding numbers for each day. In the above example the fourth field is 62 which is equal to 2+4+8+16+32 – meaning the rule should be active on Mon, Tue, Wed, Thu, and Fri i.e. only on week days. If you had checked the option Everyday this value would be 127.
# The fifth field shows the ip or mac address range in your network for which the rule should be applied – in case you don’t want all the computers on the network to be affected by this rule.
# The sixth field has the Port/Application information coded in it i.e. which ports numbers, protocols, layer 7 and p2p applications should be blocked by this rule.
# The seventh field contains the domains or URLs you want to block and it partially supports regular expressions. In the above example, domain names ending in block-site.com are blocked.
# The eighth field stores as a binary coded value if ActiveX, Flash or Java need to be blocked – 1 for ActiveX, 2 for Flash and 4 for Java.
# And finally the ninth field stores the name that you gave to this rule.
#Wait if any service is currently being restarted
nvstat=`nvram get action_service`
while [ "$nvstat" != "" ]; do
echo
done
#Rule number is passed as the first parameter on the command line.
rule=$rule
start=600 # starts at 10 am (600 minutes past midnight)
#Get the current setting of the rule.
rr=`nvram get rrule$rule`
while test "$end" -le "1200"; do
start=`awk "BEGIN{ print int(rand() * 20) + $start + 1;}"`
end=`awk "BEGIN{ print int(rand() * 10) + $start + 1;}"`
#Set the first field to the value in variable $enable
rrr=$(echo $rr|sed "s/^[0-1]|[0-9]*|[0-9]*|/1|$start|$end|/")
echo $rrr
#Replace the old rule with the new value
# nvram set rrule$i="$rr"
done
#Prepare to restart the service by killing the init process
# nvram set action_service=restrict-restart
#kill the init process
# kill -USR1 1
#Wait for the service to restart
while [ "`nvram get action_service`" == "restrict-restart" ]; do
echo
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment