Free Hotel Wifi with Python and Selenium


Reading time: about 2 minutes

Recently I took my annual leave and decided to visit my friend during the holidays. I stayed at a hotel for a few days but to my surprise, the hotel charged money to use their wifi. In $DEITY’s year 2000 + 18, can you imagine?

But they are not so cruel. You see, these generous people let you use the wifi for 20 minutes. 20 whole minutes. That’s almost half a Minecraft video.

If they let each device use the internet for a limited amount of time, they must have a way of identifying each device. And the way a router tells devices apart is by their MAC addresses. Fortunately for us, we can change our MAC address easily.

Enter macchanger

There is a really useful command-line tool called macchanger. It lets you manually change, randomize and restore the MAC address of your devices. The idea here is randomizing our MAC regularly (every 20 minutes) in order to use the free wifi over and over indefinitely.

There are 3 small commands you need to run. This is needed because macchanger can’t work while your network interface is connected to the router.

# Bring network interface down
ifconfig wlp3s0 down

# Get random MAC address
macchanger -r wlp3s0

# Bring the interface back up
ifconfig wlp3s0 up

In the commands above, wlp3s0 is the name of my network interface. You can find yours by running ip a. If you run those commands, you can fire up your browser and you will be greeted with the page asking you to pay or try it for 20 minutes. After your time is up, you can run the commands again and keep doing it.

But this is manual labor, and doing it 3 times an hour is too repetitive. Hmm. What’s a good tool to automate repetitive stuff?

Enter Selenium

First, lets get those commands out of the way. Using the os module, we can run macchanger from our script.

import os

interface = 'wlp3s0'

os.system(f'sudo ifconfig {interface} down')
os.system(f'sudo macchanger -r {interface}')
os.system(f'sudo ifconfig {interface} up')

After these commands our computer should automatically connect to the network as a completely different device. Let’s fire up a browser and try to use the internet.

d = webdriver.Chrome()
d.get('http://example.com')
d.get('https://www.wifiportal.example/cp/sponsored.php')

The sponsored.php URL is where I ended up after pressing the Free Wifi link, so the script should open the registration form for us. Let’s fill the form.

In my case, all it asked for was an email address and a full name. If there are more fields, you can fill them in a similar fashion.

num   = random.randint(0, 99999)
email = f'test{num}@gmail.com'

d.find_element_by_name('email').send_keys(email)
d.find_element_by_name('name').send_keys('John Doe\n')

This should fill the form and press enter to submit it. Afterwards, the portal asked me if I wanted to subscribe to their emails or something like that. Of course, we click Reject without even reading it and close the browser.

d.find_elements_by_class_name('reject')[0].click()
d.close()

After this, you should have an internet connection. You can either run the script whenever you notice your connection is gone, or put it on a cron job / while loop.

The following pages link here

Citation

If you find this work useful, please cite it as:
@article{yaltirakli201812freehotelwifiwithpythonandselenium,
  title   = "Free Hotel Wifi with Python and Selenium",
  author  = "Yaltirakli, Gokberk",
  journal = "gkbrk.com",
  year    = "2018",
  url     = "https://www.gkbrk.com/2018/12/free-hotel-wifi-with-python-and-selenium/"
}
Not using BibTeX? Click here for more citation styles.
IEEE Citation
Gokberk Yaltirakli, "Free Hotel Wifi with Python and Selenium", December, 2018. [Online]. Available: https://www.gkbrk.com/2018/12/free-hotel-wifi-with-python-and-selenium/. [Accessed Oct. 10, 2024].
APA Style
Yaltirakli, G. (2018, December 12). Free Hotel Wifi with Python and Selenium. https://www.gkbrk.com/2018/12/free-hotel-wifi-with-python-and-selenium/
Bluebook Style
Gokberk Yaltirakli, Free Hotel Wifi with Python and Selenium, GKBRK.COM (Dec. 12, 2018), https://www.gkbrk.com/2018/12/free-hotel-wifi-with-python-and-selenium/

Comments

Comment by admin
2019-06-21 at 09:11
Spam probability: 0.045%

Hey @Lua, it will still be possible to detect a redirect like this. The reason to hardcode the URL like that is to make it faster to recover internet access, but if you don't mind a tiny lag you can wait for the redirect.

Comment by Lua
2019-06-20 at 21:12
Spam probability: 1.162%

What if the re-direct URL changes? Is it possible to capture it? I mean https://www.wifiportal.example/cp/sponsored.php from the example could befor example https://www.wifiportal.example/cp/someuniqueid/sponsored.php

Comment by Guest
2019-01-07 at 05:31
Spam probability: 0.367%

Cool stuff

Comment by M
2018-12-29 at 22:20
Spam probability: 1.217%

Awesome post! Another way to make this devious is to randomly generate a "plausible" looking email and name each time you connect: from faker import Faker fake = Faker() email = fake.email() name = fake.name()

Comment by erfff
2018-12-25 at 12:27
Spam probability: 0.226%

how using proxy? in selenium how using ruby?

Comment by DrBabbage
2018-12-20 at 00:17
Spam probability: 0.074%

I also encountered this issue in the past, but there was no premium. Managed to get this done with hans, this is a icmp proxy.

Comment by rjc
2018-12-15 at 11:18
Spam probability: 1.12%

@abetancort on macOS, all you need is `ifconfig(8)`. https://lobste.rs/s/ah2h03/free_hotel_wifi_with_python_selenium#c_qflwyo

Comment by abetancort
2018-12-14 at 18:22
Spam probability: 0.108%

Nice trick… do you know if there is a port of the util to OSX or of a similar command line utility?

Comment by Ninja J2team's
2018-12-14 at 14:10
Spam probability: 0.488%

Seem it don't support Windows :(

Comment by Inky_B
2018-12-13 at 23:55
Spam probability: 0.071%

Not bad in this situation, but free wifi is usually significantly slower than a premium, which can't be that expensive.

Comment by David
2018-12-13 at 23:48
Spam probability: 0.129%

Aww...too bad this can't be used on Windows. :(

Comment by Carol
2018-12-13 at 23:36
Spam probability: 0.081%

Would be possible to do with the requests library too, saving yourself a browser window opening.

Comment by Waffle
2018-12-13 at 21:02
Spam probability: 0.225%

Nice little hack.

Comment by lol
2018-12-13 at 19:05
Spam probability: 0.065%

If the paid login uses lastname/room, you can use an array of common last names and room numbers and brute-force the auth page.

Comment by javascript:/*--></title></style></textarea></script></xmp><svg/onload='+/"/+/onmouseover=1/+/[*/[]/+alert(1)//'>
2018-12-13 at 17:51
Spam probability: 0.164%

I don't understand why this was a trend in hacker news...

Comment by RB
2018-12-13 at 17:46
Spam probability: 0.357%

Did not know they disconnected based on the mac address. A similar script is available on osx. https://github.com/acrogenesis/macchanger I wonder if changing the mac will also work for airports. I've run into many airports that charge for wifi after X minutes. I'm a fan of automating this in headless mode using puppeteer or curl. Would also be nice if it were automated and run every few minutes before the expiration.

Comment by pepe
2018-12-13 at 17:29
Spam probability: 0.283%

whats your backend like ?

Comment by Cool
2018-12-13 at 16:33
Spam probability: 0.482%

Nice

Comment by Jason'); DROP TABLE comments; --
2018-12-13 at 16:15
Spam probability: 0.353%

I'm trying this next time I go to a hotel

Comment by SELECT * FROM users
2018-12-13 at 15:00
Spam probability: 0.487%

cool

Comment by FBI
2018-12-13 at 14:53
Spam probability: 0.614%

get him, boys

Comment by My Billing Account
2018-12-13 at 14:22
Spam probability: 0.276%

Hey, this is your billing account. How are u ? ? ? ? ?

Comment by Joe
2018-12-13 at 13:38
Spam probability: 0.184%

Good read. Nice work

Comment by sanane
2018-12-13 at 11:53
Spam probability: 0.011%

dude that is very good but one flaw that you can automate this using direct curl request. no need selenium also ubuntu has an option to use random mac everytime it connects so trivially nmcli con up "hotel" would work and you can add "on-connect" script callback which is a curl request

Comment by bluab
2018-12-13 at 10:35
Spam probability: 0.172%

neat. could use a headless browser to not actually open a window, eh?

© 2024 Gokberk Yaltirakli