Network setting

j99mac
Contributor

I am needing to set the DNS Server for all Macs on my network.
When I run the fallowing command network setup -listallnetworkservices I find that I have different network Interface names on my Macs. Is there a command that I can run to that would chose the Active Network Interface and use it with the command to set DNS Server?

5 REPLIES 5

daz_wallace
Contributor III

I don't think so, although you could possibly run a loop on the output of listallnetworkservices adding the DNS server to everyone it finds. That way you won't miss any?

mm2270
Legendary Contributor III

I think it would be best to loop through all interfaces and add the DNS server to any that you would like to, possibly excluding certain ones, as daz_dar suggests.
However, if you look under the Extension Attributes section in your JSS, you should see a template you can add for "Active Network Interface", which pulls that information via bash script. You could use the same code as in the EA script to get the active interface and include it in a script that would set the DNS server just for that interface.

HTH

j99mac
Contributor

I was trying to use this command

mainInt=$(networksetup -listnetworkserviceorder | awk -F') ' '/(1)/ {print $2}') networksetup -setdnsservers "$mainInt" 10.0.0.11 10.0.0.12

When I run it I get the Interface of Bluetooh DUN. When the Interface is "Ethernet 1" is there way I could write this command?

mm2270
Legendary Contributor III

The -listnetworkserviceorder has nothing to do with which interface is active. That determines which interface the Mac will attempt 1st (and 2nd, 3rd, etc) when establishing a network connection.
Again, I would take a look at the Extension Attribute JAMF includes with the JSS for information on how to grab that.

Here is a modified version of what the script in the JSS template is doing, although the one JAMF provides has some error checking and other details that would likely make the output more accurate-

networksetup -listallhardwareports 2>&1 | grep -B1 $(netstat -rn | awk '/default/{print $6}') | awk -F": " '/Port/{print $NF}'

The basics is it uses the netstat command to pull the 'default' network interface device id (for ex. "en0" on a MacBook Pro connected with Ethernet) and then use that to grep out the interface name from networksetup -listnetworkservice command and print only the Hardware Port name.

iJake
Valued Contributor

@mm2270 3.5 years later I just used this, thanks! I can't believe there is not a built-in that does this.