Skip to content

Instantly share code, notes, and snippets.

@dicer

dicer/wifionice

Forked from vollkorn1982/wifionice
Last active Apr 12, 2020
Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

@dicer 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

This comment has been minimized.

Copy link

@maltere 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
You can’t perform that action at this time.