Thursday, December 18, 2008

Customizing BCM96338 (3)

Eventually I found some time to complete it.

We now know

1. How to enter the modem
2. What settings need to be made to forward a port

Now, we will see how to automate this.

Automating this is very simple. I used expect to do this job.

I can directly post all the scripts here. But they are too long and the same expect scripts might not work for someone else.

First you need to manually make the settings at the modem.
Then, just follow the steps below

1. autoexpect telnet 192.168.1.1
1.1 When you get the login prompt, login by giving the user-name and password.
1.2 At the prompt type "sh" and get a shell.
1.3 At the shell type "iptables -L -t nat"
1.4 Once you see the o/p, press "Ctrl-D", , 14
1.5 This will take you out of the modem
1.6 Auto expect would have now created a script to do this (script.exp)

2. Edit the script, set force_conservative 1, rename it to something else (poke.exp in my case)

3. Repeat step 1. At 1.3, instead of listing the table, enter the DNAT command (Eg. iptables -t nat -I PREROUTING -p tcp --dport 80 -j DNAT --to-destination 192.168.1.10)

4. Repeat step 2, save it in some other name (ipt.exp in my case)

5. Now extract the rule from the poke.exp and save it in a file. It will look like something like this,
DNAT tcp -- anywhere anywhere tcp dpt:www to:192.168.1.10
Save it to a file (grep)

6. It is time to create a script (monitor.sh). RUNDIR is where all your files will be kept.

#!/bin/bash

RUNDIR=/root/bin/bsnl
${RUNDIR}/poke.exp > ${RUNDIR}/op
grep -q -- "`cat ${RUNDIR}/grep`" ${RUNDIR}/op || ${RUNDIR}/ipt.exp

7. Now you can add this to your crontab using the command "crontab -e"
*/5 * * * * /root/bin/monitor.sh
(Save and quit)

It won't be complete if I don't mention about ddns. Get an account there. This modem has a ddns client. Once you enable this, you can access your machine using the name that you got from dyndns. (I have actually done this also using a script since I maintain my dns records at zoneedit.com. More about it in another post)

No comments: