Archive

Posts Tagged ‘monitor network’

Autoconnect to wifi in my hotel

November 30, 2011 2 comments

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.