Skip to content

Instantly share code, notes, and snippets.

@DosAmp
Created January 7, 2018 20:13
Show Gist options
  • Save DosAmp/7dfac4c947c6cf6d90c4e4c5ced33696 to your computer and use it in GitHub Desktop.
Save DosAmp/7dfac4c947c6cf6d90c4e4c5ced33696 to your computer and use it in GitHub Desktop.
Automatic login to Deutsche Bahn Wi-Fi
#!/usr/bin/env python3
import re
import requests
LOGIN_URL = "http://www.wifionice.de/de/"
CSRF_REGEX = re.compile(b'"CSRFToken" value="([0-9a-f]+)"')
print("Einloggen...")
with requests.Session() as s:
resp = s.get(LOGIN_URL)
for line in resp.iter_lines():
match = CSRF_REGEX.search(line)
if match:
token = match.group(1).decode(resp.encoding)
login_data = {"login": "true", "CSRFToken": token, "connect": ""}
resp = s.post(LOGIN_URL, data=login_data)
for line in resp.iter_lines():
if b'class="user-online' in line:
print("Login erfolgreich!")
break
@EckhardM
Copy link

EckhardM commented Feb 5, 2024

I just trying to adopt this on a Raspberry Pico W with Micropython but it does not work. Did somebody try this?

@DosAmp
Copy link
Author

DosAmp commented Feb 5, 2024

I have not used this script in a long time, but I'm currently riding an ICE train and found following small differences:

  • The login URL is now https://login.wifionice.de/de/. Scraping the CSRF token should still work the same way.
  • POST data now only consists of the login and CSRFToken parameters.
  • Either send the request with allow_redirects=False and check whether the Location header points to https://iceportal.de, or do another GET request and search for "Sie sind online".

The script should also work for DB station Wi-Fi (WIFI@DB) and DB Systel (dbs4public) hotspots, but the hostnames may vary.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment