Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 4 down vote accepted

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 = ""

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

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

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.

share|improve this answer
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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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