Skip to content

Instantly share code, notes, and snippets.

@ayharano

ayharano/captive.sh

Last active Oct 30, 2017
Embed
What would you like to do?
Small script used to log a device into a captive portal using mac os' Wi-Fi interface.
#!/bin/bash
set -eu
# URL of a website without https
HTTP_URL='pudim.com.br'
SLEEP_SECONDS_AFTER_RESETING_WIFI='30'
# Binaries' full path
WHICH="$(which which)"
CURL="$("${WHICH}" curl)"
ECHO="$("${WHICH}" echo)"
GREP="$("${WHICH}" grep)"
IFCONFIG="$("${WHICH}" ifconfig)"
LS="$("${WHICH}" ls)"
NETWORKSETUP="$("${WHICH}" networksetup)"
READ="$("${WHICH}" read)"
SED="$("${WHICH}" sed)"
SLEEP="$("${WHICH}" sleep)"
SUDO="$("${WHICH}" sudo)"
usage() {
"${ECHO}" "usage: ${0} wifi_interface_name other_mac_address"
exit 1
}
if [ "${#}" -ne '2' ]; then
usage
fi
WIFI_INTERFACE_NAME="${1}"
OTHER_MAC_ADDRESS="${2}"
if [ "$("${NETWORKSETUP}" -getairportnetwork "${WIFI_INTERFACE_NAME}" > /dev/null 2> /dev/null; "${ECHO}" ${?})" -ne '0' ]; then
"${ECHO}" "ERROR: '${WIFI_INTERFACE_NAME}' is not a Wi-Fi interface." >&2
usage
elif [ "$("${ECHO}" "${OTHER_MAC_ADDRESS}" | "${SED}" 's,^[[:space:]]*,,' | "${SED}" 's,[[:space:]]*$,,' | "${GREP}" -c '^\([0-9A-Fa-f]\{1,2\}:\)\{5\}[0-9A-Fa-f]\{1,2\}$')" -ne '1' ]; then
"${ECHO}" "ERROR: '${OTHER_MAC_ADDRESS}' should a MAC address in format xx:xx:xx:xx:xx:xx." >&2
usage
fi
ORIGINAL_MAC_ADDRESS="$("${IFCONFIG}" "${WIFI_INTERFACE_NAME}" ether | "${GREP}" -o '\([0-9A-Fa-f]\{1,2\}:\)\{5\}[0-9A-Fa-f]\{1,2\}')"
"${ECHO}" -n 'Type your sudo password IF REQUESTED. Otherwise, wait for next message. '
"${SUDO}" "${LS}" > /dev/null 2> /dev/null
"${ECHO}"
# Applying MAC address change
"${SUDO}" "${NETWORKSETUP}" -setairportpower "${WIFI_INTERFACE_NAME}" off
"${SUDO}" "${NETWORKSETUP}" -setairportpower "${WIFI_INTERFACE_NAME}" on
if [ "$("${SUDO}" "${IFCONFIG}" "${WIFI_INTERFACE_NAME}" ether "${OTHER_MAC_ADDRESS}"; "${ECHO}" ${?})" -eq '0' ]; then
"${ECHO}" "${WIFI_INTERFACE_NAME}'s MAC address changed to ${OTHER_MAC_ADDRESS}! Wait for next message..."
else
"${ECHO}" "${WIFI_INTERFACE_NAME}'s MAC address change failed. Aborting..." >&2
exit 1
fi
"${SLEEP}" "${SLEEP_SECONDS_AFTER_RESETING_WIFI}" && "${CURL}" "${HTTP_URL}" > /dev/null 2> /dev/null
# Waiting for user to logging into captive portal using requested MAC address
"${ECHO}" -n 'Press ENTER after logging into captive portal using requested MAC address.' && "${READ}" -r
# Applying MAC address restore
"${SUDO}" "${NETWORKSETUP}" -setairportpower "${WIFI_INTERFACE_NAME}" off
"${SUDO}" "${NETWORKSETUP}" -setairportpower "${WIFI_INTERFACE_NAME}" on
if [ "$("${SUDO}" "${IFCONFIG}" "${WIFI_INTERFACE_NAME}" ether "${ORIGINAL_MAC_ADDRESS}"; "${ECHO}" ${?})" -eq '0' ]; then
"${ECHO}" "${WIFI_INTERFACE_NAME}'s MAC address original value restored! Wait for next message..."
else
"${ECHO}" "${WIFI_INTERFACE_NAME}'s MAC address restore failed. Aborting..." >&2
exit 1
fi
"${SUDO}" "${NETWORKSETUP}" -setairportpower "${WIFI_INTERFACE_NAME}" off
"${SUDO}" "${NETWORKSETUP}" -setairportpower "${WIFI_INTERFACE_NAME}" on
"${SLEEP}" "${SLEEP_SECONDS_AFTER_RESETING_WIFI}" && "${CURL}" "${HTTP_URL}" > /dev/null 2> /dev/null
"${ECHO}" 'Done!'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment