Skip to content

Instantly share code, notes, and snippets.

@mid-kid
Last active June 18, 2024 14:18
Show Gist options
  • Save mid-kid/b60563059a393f90522f5e57aabfc493 to your computer and use it in GitHub Desktop.
Save mid-kid/b60563059a393f90522f5e57aabfc493 to your computer and use it in GitHub Desktop.
WiFi in de trein auto login script
#!/bin/sh
# Logs in automatically to the "WiFi in de trein" public hotspots, located in
# trains in the Netherlands.
# Requires: curl, sed
set -e
tmp="$(mktemp -d -p '' 'hslogin.XXXXXXXXXX')"
trap "rm -r '$tmp'" EXIT
curl="curl --dns-servers 8.8.8.8,8.8.4.4 -s -v -m 30"
$curl -c "$tmp/cookies" 'http://portal.nstrein.ns.nl/' > "$tmp/page"
token="$(sed -n -e 's/.* id="csrfToken" value="\([^"]*\)" .*/\1/p' "$tmp/page")"
test -n "$token"
$curl -b "$tmp/cookies" -X POST -H 'Content-Length: 0' -H 'Origin: http://portal.nstrein.ns.nl' -H 'Referer: http://portal.nstrein.ns.nl/' -H 'X-Requested-With: XMLHttpRequest' -H 'Connection: close' "http://portal.nstrein.ns.nl/nstrein:main/internet?csrfToken=$token" > /dev/null
@mid-kid
Copy link
Author

mid-kid commented Jun 17, 2024

It's a shell script. You run it with a shell.

Depending on what wireless networking system you use, you can bind it to execute after connecting to specific networks. For example, in my /etc/dhcpcd.exit-hook file, I have the following:

case "$reason" in
BOUND|BOUND6)
    if $if_up; then
    case "$ifssid" in
    "WiFi in de trein"|"WiFi in de Trein"|"Wifi in de trein")
        setsid sh -c 'sleep 5 && exec /usr/local/bin/hslogin-nstrein' 2>&1 > /dev/null &
        ;;
    esac
    fi
    ;;
esac

But if you're using NetworkManager without dhcpcd (which I think is the most common configuration for Linux these days), then you might want to look for examples on how to make a dispatcher.d script for it. Exact documentation is for example here: https://manpages.debian.org/bookworm/network-manager/NetworkManager-dispatcher.8.en.html

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