Tuesday, January 6, 2009

Internode Scripts

Just finished some scripting work that allows me to mangle internode unmetered traffic for some of our office connections along with my home connection.

The file the IP's are grabbed from looks like this:



Case Studies:
1) Home - I have 2 ADSL Connections an 8mb (unlimited) AAPT link and a 12Mb (ADSL2+ 55gb) Internode link that I use to provide my family* I want to ensure all the free content always goes out over the internode link rather than the AAPT link as streaming abc iView etc loads faster over the Internode connection.I also use the internode linux mirror heavily to update my ubuntu boxes.

2) Work - We have a couple of seperate links along with a newly installed Internode ADSL2+ We also use a number of linux boxes here and would like to make best use of the free content.

I work with routers that run Mikrotik RouterOS. Have to say first up that despite a few small issues I've had with the software from time to time, it is in my opinion the best cost-to-value software I've ever used.

Compared to cisco products etc, I can purchase a mikrotik licence ($60AUD and up depending on the version) and choose my own hardware platfor. Eg: If I'm cheap i can use an old PC as a router, if I want reliability I can buy a RouterBoard system from $150AUD and up.

So anyway, onto the script itself.

Script name: Fetch-Node-IP-List

/tool fetch address=example.com host=example.com mode=http src-path=unmetered_ip_address_list.txt
:log info "Download Complete"
:delay 10
/system script run Add-Node-Unmetered-IPs


Due to the fetch command only supporting http, I've setup a cron job on one of the linux servers to download the internode list daily onto a server where I can access it via http. If people are interested in obtaining this list (via http) daily then I'll set it up on a box that is open to the general public.

The script above simply downloads the most recent list, waits 10 seconds for the download to be complete (just in case) and then triggers the list modification script. I also like to leave comments in the log so I can go back at any time and check the log to confirm the script completed each section sucessfully.

Script name: Add-Node-Unmetered-IPs

:log info "Begining Address List Modification"
:if ( [/file get [/file find name=unmetered_ip_address_list.txt] size] > 0 ) do={

/ip firewall address-list remove [/ip firewall address-list find list=Node-Unmetered]

:global content [/file get [/file find name=unmetered_ip_address_list.txt] contents] ;
:global contentLen [ :len $content ] ;

:global lineEnd 0;
:global line "";
:global lastEnd 0;

:do {
:set lineEnd [:find $content "\n" $lastEnd ] ;
:set line [:pick $content $lastEnd $lineEnd] ;
:set lastEnd ( $lineEnd + 1 ) ;

:if ( [:pick $line 0 1] != "\n" ) do={
:local entry [:pick $line 0 ($lineEnd) ]
:if ( [:len $entry ] > 0 ) do={
/ip firewall address-list add list=Node-Unmetered address=$entry
}
}
} while ($lineEnd < $contentLen) } :log info "IP List Modification Complete"


This script does the following:
1. Checks for the existance of a non-empty text file
2. If so (file with content exists) deletes the full current address list
1. Grab the txt file and places it's contents into a variable
2. Finds a full line and places it's contents into a second variable
3. Checks the line isn't blank
4. If so (the line isn't blank) adds the contents in as a new address list entry
5. If not(the line is blank) continue onto the next line
5. Repeat until the end of file

Future improvements:
In the short term this works fine for checking and updating a small list of addresses across a number of sites. In the real world I've done a couple of things a bit shabbily, such as removing the IP address list at 2am which means it will drop any open connections at this time (sure there may not be any people on now, but that isn't the case all the time).

I'll be looking at modifying this so the router checks current IP's vs the list and determines and modifies only additions or deletions from the active list. But not for now.

*By Family I mean, my parents house where the ADSL is connected, my house which is connected 4km away via wireless and my Uncle and Aunties place connected 1km via wireless. I pay for the Internode connection myself and don't charge any money for it. If I were making a living of this then a) I'd require a wireless carrier licence myself and b) I'd need to be paying for a SOHO or Business grade ADSL connection through internode.