Automatically enable and disable a router's DMZ

Oct 19, '05 06:20:00AM

Contributed by: freemacware.com

If you connect to the internet through a router, you may occasionally want to expose your computer directly to the internet for things like BitTorrent, serving up web pages, or logging into your computer from work. Most consumer-grade routers have a "DMZ" feature that allows one internal user to be exposed to the internet. If you have a Linksys router, the following code will automatically enable the DMZ feature for your IP address:

  1. Open Terminal and type pico activatedmz to open a new document for editing.

  2. Paste in the following two lines of code. This code assumes the Linksys default IP address 192.168.1.1 and default password admin -- hopefully you've changed that! So make any changes if necessary. Remove the line wraps shown in each line, replacing them with a space to make two long lines of code:
    Line #1: lastdigit=`ifconfig | grep netmask | grep -v 127.0.0.1 
             | awk {'print $2'} | sed 's/192.168.1.//'`
    Line #2: curl http://192.168.1.1/apply.cgi
             -d "submit_button=DMZ&change_action=&action=Apply
             &dmz_enable=1&dmz_ipaddr=$lastdigit"
             -u admin:admin -s > /dev/null
    
  3. Hit Ctrl-X, then Y to save the document and close pico.

  4. Type chmod u+x activatedmz to make the script executable.

  5. Type ./activatedmz to run the script and expose your computer to the internet.
To automatically disable DMZ and again protect your computer from the internet, create another file called deactivatedmz and paste in the following single line of code -- again, remove the line breaks and replace them with a space:
Line #1: curl http://192.168.1.1/apply.cgi
         -d "submit_button=DMZ&change_action=&action=Apply&dmz_enable=0&dmz_ipaddr=0"
         -u admin:admin -s > /dev/null
Again, type chmod u+x deactivatedmz to make it executable, then type ./deactivatedmz to run it. I found it helpful to use Platypus to turn these UNIX scripts into GUI apps. I now have an "Activate DMZ" and a "Deactivate DMZ" icon in my Dock -- easy as a light switch. If you want to know how this works, or do this with a different brand of router, keep reading for a behind-the-scenes look.

Here's how I figured this out...

  1. Install the Web Developer Extension for Firefox.
  2. Open your router configuration page (at http://192.168.1.1 or whatever) in Firefox. Go to the DMZ page.
  3. On the Web Developer Extension toolbar, click Forms, then click Convert POSTs to GETs
  4. Fill out the DMZ page (to turn it on) and hit submit.
  5. You should see a long URL in the address bar -- copy it somewhere. Everything before the question mark (?) is the address of the form. Everything after the question mark is the list of variables to submit. For example, this was my URL (with line breaks for a narrower display):
    http://192.168.1.1/apply.cgi?submit_button=DMZ
    &change_action=&action=Apply&dmz_enable=1&dmz_ipaddr=3
    You'll notice the variables dmz_enable=1 and dmz_ipaddr=3, which activate the DMZ for my IP address 192.168.1.3. To deactivate the DMZ, I have to change the variables to dmz_enable=0 and dmz_ipaddr=0.
Because the DMZ form asks me to enter the last digit of my computer's IP address, I needed a way to get that from the Terminal. Here's the breakdown:
  1. ifconfig | grep netmask | grep -v 127.0.0.1 | awk {'print $2'} - returns the local IP address 192.168.1.3
  2. sed 's/192.168.1.//' - strips out the first three numbers and returns just the 3
  3. lastdigit=`` - saves the 3 to a variable called $lastdigit
And a breakdown of the curl statement:
  1. curl http://192.168.1.1/apply.cgi - the address of the form (everything before the ? from above)
  2. -d "submit_button=DMZ&change_action=&action=Apply&dmz_enable=1&dmz_ipaddr=$lastdigit" - the variables (everything after the ?)
  3. -u admin:admin - the username and password of your router
  4. -s > /dev/null - silences the output
Hope that makes thing somewhat clearer...

Comments (20)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20051015142349372