Bash script or Jamf Policy to move Wi-FI to top of Network Services list

Does anyone know how to do this? I have spent much time and have not gotten it to work. I want to move WI-FI to the top of the network services list using script or Jamf policy.

Posted on Apr 19, 2023 12:06 PM

Reply
Question marked as Top-ranking reply

Posted on Apr 20, 2023 11:29 AM

This is pretty easy to do.


Network Preferences has a whole command-line suite available for confirming the network, with a high degree of control.


In this case, you want to use networksetup with the -ordernetworkservices switch:


networksetup -ordernetworkservices "WiFi" "Built-in Ethernet 1" "USB 10/100/100 LAN"


The above would configure the system to prefer WiFi, then the built-in ethernet, then a USB adapter.


You can get a list of available devices on the current system via:


networksetup --listallnetworkservices


and you can review the current priority order via:


networksetup -listnetworkserviceorder


3 replies
Question marked as Top-ranking reply

Apr 20, 2023 11:29 AM in response to madaudit

This is pretty easy to do.


Network Preferences has a whole command-line suite available for confirming the network, with a high degree of control.


In this case, you want to use networksetup with the -ordernetworkservices switch:


networksetup -ordernetworkservices "WiFi" "Built-in Ethernet 1" "USB 10/100/100 LAN"


The above would configure the system to prefer WiFi, then the built-in ethernet, then a USB adapter.


You can get a list of available devices on the current system via:


networksetup --listallnetworkservices


and you can review the current priority order via:


networksetup -listnetworkserviceorder


Apr 20, 2023 12:27 PM in response to madaudit

Ultimately, the -listallnetworkservices and -listnetworkserviceorder switches are going to be the key.


However, you're going to have to work out some rule for identifying the WiFi network adapter if users have unfettered access to naming their devices.


Looking at the -listallnetworkservices output, there is an indicator as to the interface type there:


(4) Wi-Fi
(Hardware Port: Wi-Fi, Device: en0)


so you could parse the 'Hardware Port: Wi-Fi' string to identify the network.


Alternatively, a couple of things come to mind.


First, I think you only need to specify the WiFi network in the -ordernetworkservices command - whatever you specify comes to the top, with the remaining interfaces listed afterwards.


As for identifying the WiFi network, you could trial-and-error using the networksetup -getairportnetwork command, which will indicate if the network is a Wi-Fi or wired connection:


networksetup -getairportnetwork en1
en1 is not a Wi-Fi interface.
networksetup -getairportnetwork en2
Current Wi-Fi Network: MyAirPortNetwork


This would help you identify the hardware port that is your wifi network, which can then track to the service name.


Alternatively the -listnetworkserviceorder indicates the Hardware Port, which you can parse out via grep:


networksetup -listnetworkserviceorder | grep -B1 "Hardware Port: Wi-Fi"
(4) Wi-Fi
(Hardware Port: Wi-Fi, Device: en0)


So now you know the service name (just need to nix the '(4) ' from the first line).


Ultimately, these are the kind of games you're going to have to play to get a universal solution that works on all systems.

If that's too much logic processing in shell scripts (which I have a hard time with, personally), the commands can be invoked via a number of different systems, I'm not familiar enough with Jamf to know what other options it has, though.

Apr 20, 2023 11:43 AM in response to Camelot

The issue is I want to run the script on any Mac and do not know all the devices listed in Network Services. I want the script to move Wi-fi to the top of the Network services list regardless of what Network services are on the mac. I don't want to hard code the Network service names into the script because all users have diffrent Network services listed. I can read the Network Services into array with Double quotes like it says but for some reason can't get it to work.




This is what I have in the Network Services list.



~ % networksetup

-listnetworkserviceorder


An asterisk (*) denotes that a network service is

disabled.


(1) Wi-Fi


(Hardware Port: Wi-Fi, Device: en0)




(2) USB 10/100/1000 LAN


(Hardware Port: USB 10/100/1000 LAN, Device: en8)




(3) ThinkPad Lan


(Hardware Port: ThinkPad Lan, Device: en14)




(4) Lenovo USB-C to LAN


(Hardware Port: Lenovo USB-C to LAN, Device: en15)




(5) USB-C Dock Ethernet


(Hardware Port: USB-C Dock Ethernet, Device: en13)






This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Bash script or Jamf Policy to move Wi-FI to top of Network Services list

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.