Skip to content

Instantly share code, notes, and snippets.

@DanielVoogsgerd
Created February 7, 2024 10:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DanielVoogsgerd/4ff8b5395d65d978e8bce96347754cf5 to your computer and use it in GitHub Desktop.
Save DanielVoogsgerd/4ff8b5395d65d978e8bce96347754cf5 to your computer and use it in GitHub Desktop.
#!/bin/sh
# Logs in automatically to the "WiFi in de trein" public hotspots, located in
# trains in the Netherlands.
# Requires: curl, sed
#
# Based on: https://gist.github.com/mid-kid/b60563059a393f90522f5e57aabfc493
set -e
ssid="Wifi in de trein"
# Check if we are online
if ping -W 0.5 -c 1 8.8.8.8 > /dev/null; then
echo "Already online"
exit 1
fi
current_ssid="$(nmcli -t -f active,ssid dev wifi list --rescan no | awk -F: '/^yes/{ print $2 }')"
if [[ "$current_ssid" != "$ssid" ]]; then
echo "Not connected to \"$ssid\""
exit 1
fi
tmp="$(mktemp -d -p '' 'hslogin.XXXXXXXXXX')"
trap "rm -r '$tmp'" EXIT
curl="curl -s -v -m 30"
$curl -c "$tmp/cookies" \
'http://portal.nstrein.ns.nl/' \
> "$tmp/page" \
2> /dev/null
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 \
2> /dev/null
echo "Connected"
if ping -W 0.5 -c 1 8.8.8.8 > /dev/null; then
echo "Internet available"
else
echo "Still not online"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment