Home > python > Autoconnect to wifi in my hotel

Autoconnect to wifi in my hotel

Problem
In my hotel there is a fast wifi connection. However, some things trouble me. When I open the browser and I’m not logged in to the wifi, I’m automatically redirected to a page where I must provide my username and password. Unfortunately, the connection is terminated after a certain time so I have to log in again.

Could I use a script for automatically log in to the wifi? Furthermore, if the connection is lost, could it be restarted automatically?

Auto login
This solution is specific to my case but it can be customized to other networks too. Here is my login script:

#!/usr/bin/env python

# wifi.py

from splinter.browser import Browser
from time import sleep

URL = 'https://controller.mobile.lan/103/portal/'
NAME = 'login_name'
PASSWORD = 'login_password'

def main():
    br = Browser('chrome')
    br.visit(URL)
    sleep(3)
    if br.is_text_present('Connection', wait_time=7):
        br.fill('login', NAME)
        br.fill('password', PASSWORD)
        br.find_by_css('#logonForm_connect_button').first.click()

#############################################################################

if __name__ == "__main__":
    main()

I use splinter for browser automation. First, we open the wifi login page (see Figure 1). The login page uses AJAX so we wait some seconds until the page is completely loaded. As an added security, we also verify if the text “Connection” is present. If not, let’s wait some more time. If the page is OK, then fill the fields and click on the form button.


Figure 1: Login page

Figure 2 shows that the connection is established. The red warning message says that the browser must be left opened, otherwise the connection is lost. So from the script we won’t close the browser window. The Python script terminates but the browser window stays there, keeping the wifi connection alive.


Figure 2: Successful login, connection established

Monitoring the network connection
As mentioned above, my connection is terminated after some hours. Here is another script that monitors the network connection. If the network is down, it closes the previously opened browser window and calls the first script (wifi.py) in the background. The first script will establish the connection and the night mass downloading can go on without any problem :)

#!/usr/bin/env python

# monitor_wifi.py

import os
import psutil
from jabbapylib.network import network
from time import sleep
from datetime import datetime
from jabbapylib.process import process

PROCNAME = 'chromedriver'
SLEEP_TIME = 2 * 60

def kill_chromedriver():
#    print '# killing chromedriver'
    for proc in psutil.process_iter():
        if proc.name == PROCNAME:
            proc.kill()

def start_wifi():
    print '# restarting wifi ({now})'.format(now=datetime.now())
    os.chdir('/home/jabba/bin.python')
    process.execute_cmd_in_background("./wifi.py")

def main():
    while True:
        if not network.is_internet_on():
#            print '# network is down'
            kill_chromedriver()
            start_wifi()
        else:
#            print '# network is up'
            pass

#        print '# sleeping...'
        sleep(SLEEP_TIME)

#############################################################################

if __name__ == "__main__":
    main()

Here I use my jabbapylib library.

Usage
For browsing I use Firefox. For logging in I start an instance of Chromium, thus the two things are kept separate. Start wifi.py to create the connection. Then launch monitor_wifi.py. Normally that should be all. I only tested it under Linux.

Advertisements
  1. December 19, 2011 at 17:10

    Greetings! Jabba Laci. First I would like to thank you for sharing this codes which is very helpful to me if I could actually use it because I have exact situation as you stated in the problem section, but I am not onto programming side, so i don’t really nailed how to use the codes you had given. It would be very much appreciated if there is a simplified way or explanation with this or is it the the one? (makes me dumber). Thank you very much.

    With respect, Charles

    • December 19, 2011 at 21:07

      Dear Charles,

      You should give some more information. What operating system do you use? What’s your goal? Do you want to connect automatically? Do you want to reconnect too if the network connection is lost? What step is not clear? Are you familiar with Python a bit? Do you have Python on your machine? Every piece of info can be useful.

      Laszlo

  1. No trackbacks yet.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: