Last active
July 26, 2024 18:37
-
-
Save DanielVoogsgerd/4ff8b5395d65d978e8bce96347754cf5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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')" | |
# shellcheck disable=2064 | |
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