Last active
March 4, 2026 07:46
-
-
Save morgan9e/4daf76948f3a342571ce4224920c1a0d to your computer and use it in GitHub Desktop.
nm dispatcher for captive portal (snu_guest, KT_Starbucks, ...)
This file contains hidden or 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/bash | |
| # | |
| # /etc/NetworkManager/dispatcher.d/99-wifi-hook | |
| INTERFACE="$1" | |
| ACTION="$2" | |
| CONN="$CONNECTION_ID" | |
| LOCKDIR="/tmp/nm-wifi-hook-$CONN.lock" | |
| run_hook() { | |
| local script="$1" | |
| if ! mkdir "$LOCKDIR" 2>/dev/null; then | |
| exit 0 | |
| fi | |
| trap "rmdir '$LOCKDIR'" EXIT | |
| logger "Running $1" | |
| "$script" | |
| } | |
| if [ "$INTERFACE" != "$IFACE" ]; then | |
| exit 0; # Not wifi | |
| fi | |
| SSID=$(iw dev $INTERFACE info | grep ssid | awk '{print $2}') | |
| if [ "$ACTION" = "up" ]; then | |
| case "$SSID" in | |
| "snu_guest") | |
| run_hook "/home/$USER/.local/bin/scripts/wifi-hook-snu_guest" | |
| ;; | |
| esac | |
| fi | |
| exit 0 |
This file contains hidden or 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
| #!/usr/bin/env python3 | |
| # | |
| # $HOME/.local/bin/scripts/wifi-hook-snu_guest | |
| import requests | |
| import re | |
| import sys | |
| sess = requests.Session() | |
| init_req = sess.get("http://1.0.0.1") | |
| if "1.1.1.1" in init_req.text: | |
| print("Already authenticated") | |
| sys.exit(0) | |
| if (next_case := re.findall("<meta http-equiv='refresh' content='1; url=(.*)'>", init_req.text)): | |
| next_url = next_case[0] | |
| else: | |
| next_url = "http://172.19.1.20/view/external/guest" | |
| logn_req = sess.get(next_url) | |
| apc_data = { | |
| "apc_type": re.findall('var APCType = "(.*)";', logn_req.text)[0], | |
| "apc_ip": re.findall('var APCConnectionIp = "(.*)";', logn_req.text)[0], | |
| "apc_port": re.findall('var APCConnectionPort = "(.*)";', logn_req.text)[0], | |
| "apc_cb": re.findall('var redirectURL = "(.*)";', logn_req.text)[0], | |
| "apc_fwd": re.findall('var forward = "(.*)";', logn_req.text)[0] | |
| } | |
| auth_req = sess.post("http://172.19.1.20/login/guest", headers={'Content-Type': 'application/json; charset=utf-8','isPad': 'false'}, json = | |
| {"userId":"","userType":"","guestCompany":"","guestPhone":"","purpose":"","guestEmail":"","guestName":""} | |
| ) | |
| print(auth_req.text) | |
| if (apc_data["apc_type"] == "SAMSUNG"): | |
| import dns.resolver | |
| resolver = dns.resolver.Resolver(configure=False) | |
| resolver.nameservers = ["1.1.1.1"] | |
| fwd_host = requests.utils.parse_url(apc_data["apc_fwd"]).host | |
| fwd_ip = resolver.resolve(fwd_host, 'A') | |
| url = apc_data["apc_fwd"].replace(fwd_host, fwd_ip[0].to_text()) if apc_data["apc_fwd"] else f"https://{apc_data["apc_ip"]}/client.php" | |
| data = {"id": "guest", "passwd": "guest", "redirect_url": apc_data["apc_cb"], "agree": "0", "type": "3"} | |
| elif (apc_data["apc_type"] == "ARUBA"): | |
| url = f"http://{apc_data["apc_ip"]}/auth/index.html/u" | |
| data = {"user": "guest", "password": "guest"} | |
| else: | |
| print(logn_req.text) | |
| print(apc_data) | |
| finl_req = sess.post(url, headers={'Content-Type': 'application/x-www-form-urlencoded'}, data=data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment