I often connect to a network, that is open, but requires a key-pass authentication before used in a webpage. Is it possible to automate the authentication with a script?


What a fun problem.

While I'm sure ændrük's answer would probably work, I'm slightly adverse to letting something dependant on desktop software manage the network availability.

I would suggest you play around with Python's Mechanize library. You can install it from the repositories via:

sudo apt-get install python-mechanize

Then get scripting. You need to start by finding out the stucture of the page you're submitting to. You need to find where you form comes and what fields need to be entered to submit. Once you know that, it's really as simple as just writing a quick little form parser and submitter. Mechanize does all the heavy lifting. Here's a quick example adapted from their docs on forms:

import sys
from mechanize import ParseResponse, urlopen, urljoin

# replace this with the actual login page
# you'll need to do some research
uri = "http://theloginpage.com/"

response = urlopen(uri)
forms = ParseResponse(response, backwards_compat=False)
form = forms[0]
print form
form["username"] = "your username"
form["username"] = "password"

# form.click() returns a mechanize.Request object
# (see HTMLForm.click.__doc__ if you want to use only the forms support, and
# not the rest of mechanize)
print urlopen(form.click()).read()

Depending on how their system works, this might be enough. You might have to tie in some cookie jarring to keep a session alive while they turn the connection on, but technically speaking, there's no argument why they should require that.

Then you just need to tie this into the networking system so it gets called. Loading it from /etc/network/if-up.d should work but I suggest you prefix your code with a check to make sure you're really on the hotspot that your login works for.

  • Wonderful, I didn't know about mechanize. I'm excited to try it out. – ændrük Feb 17 '11 at 22:41

Depending on the type of captive portal in place, you might be able to authenticate via a POST request sent using Wget (use Tamper Data to learn what data needs to be sent) or by using an iMacros script in Firefox.

You can add a script to /etc/network/if-up.d to make it run every time a network connection is established. Have the script verify what network you're on with iwconfig before doing anything, and be sure to start commands using sudo -u yourusername so you don't run your web browser as root.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.