Skip to content

Instantly share code, notes, and snippets.

@oxguy3
Last active April 29, 2019 18:58
Show Gist options
  • Save oxguy3/eb24c2ae2fd2ee1ef26e35e037ad1b02 to your computer and use it in GitHub Desktop.
Save oxguy3/eb24c2ae2fd2ee1ef26e35e037ad1b02 to your computer and use it in GitHub Desktop.
Installation script for a launchd daemon that automatically logs into my workplace's wi-fi
#!/bin/bash
function run8451eir
{
if [ -a ~/Library/LaunchAgents/local.8451login.plist ]; then
promptun="$(osascript -e 'Tell application "System Events" to display dialog "Looks like 84.51 EiR Utility is already installed; do you want to uninstall it?" default button 1 buttons {"Yes", "No"} with icon caution' 2>/dev/null)"
if [ "$promptun" = "button returned:Yes" ]; then
rm ~/Library/Application\ Support/8451login.sh
launchctl unload ~/Library/LaunchAgents/local.8451login.plist
rm ~/Library/LaunchAgents/local.8451login.plist
osascript -e 'Tell application "System Events" to display alert "Uninstalled!"'
exit 0
fi
fi
promptin="$(osascript -e 'Tell application "System Events" to display dialog "Do you want to install 84.51 EiR Utility?" default button 1 buttons {"Yes", "No"} with icon note' 2>/dev/null)"
if [ "$promptin" = "button returned:Yes" ]; then
:
else
exit 1
fi
eiruser="$(osascript -e 'Tell application "System Events" to display dialog "Enter your 8451-EiR username:" default answer "" with icon note' -e 'text returned of result' 2>/dev/null)"
if [ $? -ne 0 ]; then
exit 1
elif [ -z "$eiruser" ]; then
osascript -e 'Tell application "System Events" to display alert "Username is required; try again." as warning'
exit 1
fi
eirpass="$(osascript -e 'Tell application "System Events" to display dialog "Enter your 8451-EiR password:" default answer "" hidden answer true with icon note' -e 'text returned of result' 2>/dev/null)"
if [ $? -ne 0 ]; then
exit 1
elif [ -z "$eirpass" ]; then
osascript -e 'Tell application "System Events" to display alert "Password is required; try again." as warning'
exit 1
fi
cat << EOF > ~/Library/Application\ Support/8451login.sh
#!/bin/bash
ssid=\`/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -I | awk '/ SSID/ {print substr(\$0, index(\$0, \$2))}'\`
if [ "\$ssid" == "8451-EiR" ]; then
/usr/bin/curl -k -H "Host: 1.1.1.1" --data "buttonClicked=4&redirect_url=&err_flag=0&username=$eiruser&password=$eirpass" --compressed https://1.1.1.1/login.html > /dev/null
fi
EOF
chmod +x ~/Library/Application\ Support/8451login.sh
cat << EOF > ~/Library/LaunchAgents/local.8451login.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" \
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>local.8451login</string>
<key>LowPriorityIO</key>
<true/>
<key>Program</key>
<string>/Users/`whoami`/Library/Application Support/8451login.sh</string>
<key>WatchPaths</key>
<array>
<string>/etc/resolv.conf</string>
<string>/Library/Preferences/SystemConfiguration/NetworkInterfaces.plist</string>
<string>/Library/Preferences/SystemConfiguration/com.apple.airport.preferences.plist</string>
</array>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
EOF
launchctl load ~/Library/LaunchAgents/local.8451login.plist
~/Library/Application\ Support/8451login.sh &> /dev/null
osascript -e 'Tell application "System Events" to display alert "84.51 EiR Utility is now installed!" message "Your computer will now automatically attempt to login to 84.51 EiR wi-fi every time you connect to it (you might have to wait a moment for it to happen). If you ever want to uninstall this utility, just open this app again."'
}
run8451eir

84.51 EiR Utility

This script will automatically log-in to my workplace's captive portal every time I connect to the wi-fi. It's currently only designed to work with my workplace's wi-fi, but it is easily adaptable. Simply change the SSID on line 43 from "8451-EiR" to whatever your network's SSID is. Then change the curl command on line 44 to be a curl command that logs into your workplace's wi-fi. You can find out this command using your browser's inspector tools to examine the captive portal's login form, or using a proxy tool like Charles Proxy, mitmproxy, or Fiddler.

This script is limited to macOS (aka Mac OS X), and that's not very easily changed; sorry.

If you want to distribute this to non-developers, I recommend bundling it into an app using appify; here's a nice tutorial (it's super easy).

How It Works

Once you choose to install and enter your username and password, the installer first creates a bash script at ~/Library/Application Support/8451login.sh. This script doesn't do much; it simply checks that the current SSID is "8451-EiR" and, if it is, uses curl to POST the username and password to the login page. The installer then makes the bash script executable.

Next, the installer creates an XML file at ~/Library/LaunchAgents/local.8451login.plist. This is a config file for launchd, and it basically says that, whenever any of the computer's network config files get changed (which happens whenever connecting/disconnecting/etc from a network), the ~/Library/Application Support/8451login.sh script will get run.

Lastly, the installer tells launchd to load the new plist file, and then runs the script once to make sure it takes effect right away. When the computer reboots in the future, launchd will automatically load the plist file.

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