Skip to content

Instantly share code, notes, and snippets.

@dicer
Forked from vollkorn1982/wifionice
Last active February 28, 2024 09:57
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dicer/b21300fbbfdd8cf011756a8673b6f69c to your computer and use it in GitHub Desktop.
Save dicer/b21300fbbfdd8cf011756a8673b6f69c to your computer and use it in GitHub Desktop.
HowTo auto connect your Linux to the German Wifi on ICE trains
#!/bin/bash
# change the next line to match your wifi device and put this file in /etc/NetworkManager/dispatcher.d/ and make it executable
WIFI_DEVICE=wlp3s0
IF=$1
STATUS=$2
if [ "$IF" == "$WIFI_DEVICE" ] && [ "$STATUS" == "up" ] && [ $(iwconfig $WIFI_DEVICE | grep -c "ESSID:\"WIFIonICE\"") ]
then
curl 'http://www.wifionice.de/de/' -H 'Host: www.wifionice.de' -H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:58.0) Gecko/20100101 Firefox/58.0' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' -H 'Accept-Language: en-US,en;q=0.5' --compressed -H 'Referer: http://www.wifionice.de/' -H 'Content-Type: application/x-www-form-urlencoded' -H 'Cookie: csrf=42' -H 'Connection: keep-alive' -H 'Upgrade-Insecure-Requests: 1' --data 'login=true&CSRFToken=42&connect='
fi
@dicer
Copy link
Author

dicer commented Feb 22, 2018

First I wanted to get a valid CSRF token first by requesting the main page and extract it. Turns out this is not even necessary as long as the submitted token and the cookie are the same. Currently the captive portal server does not seem to remember the token it gives out.
In case that changes:

CSRF=$(curl -s 'http://www.wifionice.de/de/' | grep "CSRFToken" | sed -n '/CSRFToken/s/.*name="CSRFToken"\s\+value="\([^"]\+\).*/\1/p')
curl 'http://www.wifionice.de/de/' -H 'Host: www.wifionice.de' -H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:58.0) Gecko/20100101 Firefox/58.0' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' -H 'Accept-Language: en-US,en;q=0.5' --compressed -H 'Referer: http://www.wifionice.de/' -H 'Content-Type: application/x-www-form-urlencoded' -H "Cookie: csrf=$CSRF" -H 'Connection: keep-alive' -H 'Upgrade-Insecure-Requests: 1' --data "login=true&CSRFToken=$CSRF&connect="

@maltere
Copy link

maltere commented Oct 25, 2019

Works perfectly!

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