Home > Mac administration, Mac OS X, Scripting > Setting preferred wireless networks from the command line.

Setting preferred wireless networks from the command line.

If you need to set a wireless network in Mac OS X from the command line or with a script, you can do so with the networksetup command. Networksetup has a few options that are specifically geared to working with wireless networks. For me, one of the more useful is the ability to set wireless networks, either manually via the command line or automating it with a script. You can set this using the -addpreferredwirelessnetworkatindex option, with the network port, wireless network name, the index number you want to assign it to (set it to 0 if you don’t know what to set it to, as that’ll bump it to the top of the list), and the security type of the wireless network (OPEN, WEP, WPA, WPA2, WPAE or WPA2E). You can also substitute NONE in place of OPEN in the security type.

For example, you may have a wireless network named HomeNetwork that uses WPA2 encryption with the password of donthackme. It’s the only one in your environment and you want to set your Mac(s) to prefer this wireless network. To set your preferred wireless network, log into your Mac(s) with an admin account, open Terminal and run the following command:

sudo networksetup -addpreferredwirelessnetworkatindex AirPort HomeNetwork 0 WPA2 donthackme

Running this command will add it to the list of your preferred wireless networks in System Preferences: Network: Airport. It will also add a password item to the System keychain for the HomeNetwork wireless network, which will store the donthackme password.

For greater flexibility, you can also script this so that all of the various values after -addpreferredwirelessnetworkatindex are variables which can be set as needed. See below the jump for a script that shows how to do this.

Hat tip to Charles Edge for describing this method in this entry on his blog: http://krypted.com/mac-os-x/pushing-wireless-networks-out/

#!/bin/sh

# Enable the wireless method you need and
# add the correct variables as needed. The
# wireless network name should not contain spaces.

# Set the WIRELESS variable to the wireless
# network port you want to use. (leave it as
# "AirPort" if you do not know what port to use.)

WIRELESS=AirPort

# 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.
SSID=

# 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.)
INDEX=0

# 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.
SECURITY=

# If you've set the SECURITY variable to something other than NONE,
# 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.)
PASSWORD=

sudo networksetup -addpreferredwirelessnetworkatindex $WIRELESS $SSID $INDEX $SECURITY $PASSWORD

  1. gilris
    October 14, 2011 at 5:29 pm

    The networksetup -addpreferredwirelessnetworkatindex command is only available in OSX 10.6.x, how do you accomplish the same task in 10.5.x?

  2. kc
    December 30, 2012 at 12:14 am

    networksetupup -setairportnetwork ssid key

  3. felixfx2
    February 16, 2017 at 10:36 am

    how do i reorder SSID which are already joined then?

    • March 31, 2017 at 7:57 pm

      Pudquick has created a nice python script for reordering already existing networks on his git


      #!/usr/bin/python
      # As written, this requires the following:
      # – OS X 10.6+ (may not work in 10.10, haven't tested)
      # – python 2.6 or 2.7 (for collections.namedtuple usage, should be fine as default python in 10.6 is 2.6)
      # – pyObjC (as such, recommended to be used with native OS X python install)
      # Only tested and confirmed to work against 10.9.5
      # Run with root
      import objc, ctypes.util, os.path, collections
      from Foundation import NSOrderedSet
      preferred_SSID = 'This SSID Should Be First'
      next_to_last_SSID = 'This SSID Should Be Next To Last'
      last_SSID = 'This SSID Should be Last'
      def load_objc_framework(framework_name):
      # Utility function that loads a Framework bundle and creates a namedtuple where the attributes are the loaded classes from the Framework bundle
      loaded_classes = dict()
      framework_bundle = objc.loadBundle(framework_name, bundle_path=os.path.dirname(ctypes.util.find_library(framework_name)), module_globals=loaded_classes)
      return collections.namedtuple('AttributedFramework', loaded_classes.keys())(**loaded_classes)
      # Load the CoreWLAN.framework (10.6+)
      CoreWLAN = load_objc_framework('CoreWLAN')
      # Load all available wifi interfaces
      interfaces = dict()
      for i in CoreWLAN.CWInterface.interfaceNames():
      interfaces[i] = CoreWLAN.CWInterface.interfaceWithName_(i)
      # Repeat the configuration with every wifi interface
      for i in interfaces.keys():
      # Grab a mutable copy of this interface's configuration
      configuration_copy = CoreWLAN.CWMutableConfiguration.alloc().initWithConfiguration_(interfaces[i].configuration())
      # Find all the preferred/remembered network profiles
      profiles = list(configuration_copy.networkProfiles())
      # Grab all the SSIDs, in order
      SSIDs = [x.ssid() for x in profiles]
      # Check to see if our preferred SSID is in the list
      if (preferred_SSID in SSIDs):
      # Apparently it is, so let's adjust the order
      # Profiles with matching SSIDs will move to the front, the rest will remain at the end
      # Order is preserved, example where 'ssid3' is preferred:
      # Original: [ssid1, ssid2, ssid3, ssid4]
      # New order: [ssid3, ssid1, ssid2, ssid4]
      profiles.sort(key=lambda x: x.ssid() == preferred_SSID, reverse=True)
      # Now we move next_to_last_SSID to the end
      profiles.sort(key=lambda x: x.ssid() == next_to_last_SSID, reverse=False)
      # Now we move last_SSID to the end (bumping next_to_last_SSID)
      profiles.sort(key=lambda x: x.ssid() == last_SSID, reverse=False)
      # Now we have to update the mutable configuration
      # First convert it back to a NSOrderedSet
      profile_set = NSOrderedSet.orderedSetWithArray_(profiles)
      # Then set/overwrite the configuration copy's networkProfiles
      configuration_copy.setNetworkProfiles_(profile_set)
      # Then update the network interface configuration
      result = interfaces[i].commitConfiguration_authorization_error_(configuration_copy, None, None)

  1. No trackbacks yet.

Leave a comment