Skip to content

Instantly share code, notes, and snippets.

@menasheh
Last active March 9, 2022 11:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save menasheh/66a7b824e53d0ca75e94d95a844f8400 to your computer and use it in GitHub Desktop.
Save menasheh/66a7b824e53d0ca75e94d95a844f8400 to your computer and use it in GitHub Desktop.
Automatically log in to your school/work/coffeeshop/bus Captive Portal Wifi
Put the following file in /etc/network/if-up.d/ under any name. Make sure to chmod+x.
#!/bin/bash
# Set the name of an SSID with a captive portal:
SSID="examplewifi"
# Don't run on the loopback device
[ "$IFACE" != "lo" ] || exit 0
# Don't run on ethernet
# Check your ethernet interface name(s) - Run `iwconfig` and find options other than "lo" with "no wireless connections." listed. Copy this line for each if there are more than one, replacing "eth0" as appropriate.
# This following line is an attempt at a generic alternative for the next. If your wifi isn't on eth0, switch it as appropriate on line 13.
# [ ${#`iwconfig $IFACE | grep $IFACE | grep "no wireless extensions."`} = 0 ] || exit 0
[ "$IFACE" != "eth0" ] || exit 0
# Only run on matching SSID
ESSID=$(iwconfig $IFACE | grep ESSID | cut -d":" -f2 | sed 's/^[^"]*"\|"[^"]*$//g')
[ "$ESSID" = "$SSID" ] || { echo "Not running script. SSID: $ESSID Expected: $SSID" && exit 0; }
# Only run if can't connect to the internet:
[[ $(curl https://peromsik.com/scripts/nettest/connection.php) != "ok" ]] || { echo "Not running script; already logged in to $SSID wifi." && exit 0; }
echo "Running login script for \$SSID=$SSID"
# Run your script that logs into the captive portal here. I've done it with lynx:
exec lynx -cmd_script="/etc/network/if-up.d/lynx" -cmd_log="/var/log/lynx.log"
case "${1}" in
resume|thaw)
find /etc/network/if-up.d/ -maxdepth 1 -type f -name 'cp-*' | sort | bash
;;
esac
@menasheh
Copy link
Author

Do it manually once and use -cmd_log to get keypresses for the script. See this blog post, which is targeted for Windows.

@menasheh
Copy link
Author

Tweaked today, works as of Aug 29, 17

@mighty-phoenix
Copy link

@menasheh Can you please tell me if such a script can be used to automate login on lan after timeouts?

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