Skip to content

Instantly share code, notes, and snippets.

What would you like to do? - joins a wifi network at the login window.
# Set paths to our utilities
# Determines which OS the script is running on
osvers=$(sw_vers -productVersion | awk -F. '{print $2}')
# On 10.7 and higher, the Wi-Fi interface needs to be identified.
# On 10.5 and 10.6, the Wi-Fi interface should be named as "AirPort"
if [[ ${osvers} -ge 7 ]]; then
wifiDevice=`/usr/sbin/networksetup -listallhardwareports | awk '/^Hardware Port: Wi-Fi/,/^Ethernet Address/' | head -2 | tail -1 | cut -c 9-`
/usr/sbin/networksetup -setnetworkserviceenabled Wi-Fi on
wifiDevice=`/usr/sbin/networksetup -listallhardwareports | awk '/^Hardware Port: AirPort/,/^Ethernet Address/' | head -2 | tail -1 | cut -c 9-`
/usr/sbin/networksetup -setnetworkserviceenabled AirPort on
# Set the SSID variable to your wireless network name
# to set the network name you want to connect to.
# Note: Wireless network name cannot contain spaces.
# Set the INDEX variable to the index number you'd like
# it to be assigned to (leave it as "0" if you do not know
# what index number to use.)
# Set the SECURITY variable to the security type of the
# wireless network (NONE, WEP, WPA, WPA2, WPAE or
# WPA2E) Setting it to NONE means that it's an open
# network with no encryption.
# Set the password here. For example, if you are using WPA
# encryption with a password of "thedrisin", set the PASSWORD
# variable to "thedrisin" (no quotes.)
# Once the running OS is determined, the settings for the specified
# wireless network are created and set as the first preferred network listed
if [[ $wifiDevice == "" ]]; then
echo "No Wi-Fi device found!"
exit 0;
# Run our tests first
airportScan=`/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -s`
preferredList=`/usr/sbin/networksetup -listpreferredwirelessnetworks $wifiDevice`
# First: turn on wifi
/usr/sbin/networksetup -setairportpower $wifiDevice on
# Does the wifi even exist?
if [[ -n "$airportScan | grep $SSID" ]]; then
# Yes, the wifi exists. Is it already on the preferred list?
if [[ -z `echo $preferredList | grep $SSID` ]]; then
# No, it isn't in the preferred list, so we set the connection right now.
/usr/sbin/networksetup -setairportnetwork $wifiDevice $SSID $PASSWORD
echo "Setting network now."
exit 0;
# If it gets here, it is on the preferred list and the wifi exists, so it should join automatically (or have another SSID to join).
# If it gets here, the wifi doesn't exist, so we shouldn't try to join it right now. Add it to the preferred list.
echo "Setting network later."
/usr/sbin/networksetup -addpreferredwirelessnetworkatindex $wifiDevice $SSID $INDEX $SECURITY

This comment has been minimized.

Copy link

commented Mar 28, 2014

This is exactly what I have been trying to do ... thanks for posting! I am going to try it this weekend.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.