Script Help (Networksetup MediaOpt) Case or If Statement

Matt
Valued Contributor

Hey guys and gals we have a 100basetx Full-Duplex setup here and cannot leave the adapters on automatic. I concocted a pretty amateur script full of if statements; it works but I am sure someone can help me clean it up with a case statement or something like that.

#!/bin/sh
# Declaring Variables

en=`/usr/sbin/networksetup -listallnetworkservices | grep -x "Ethernet"`
en1=`/usr/sbin/networksetup -listallnetworkservices | grep -x "Ethernet 1"`
en2=`/usr/sbin/networksetup -listallnetworkservices | grep -x "Ethernet 2"`
thunderbolt=`/usr/sbin/networksetup -listallnetworkservices | grep -x "Thunderbolt Ethernet"`
usb=`/usr/sbin/networksetup -listallnetworkservices | grep -x "USB Ethernet"`
display=`/usr/sbin/networksetup -listallnetworkservices | grep -x "Display Ethernet"`

# Setting Devices

# Setting Built-in Ethernet

if [ "$en" = "Ethernet" ]; then
    /usr/sbin/networksetup -setMedia "Ethernet" 100baseTX full-duplex
    echo "Built in Ethernet Completed"

elif [ "$en" = "" ]; then
    echo "No Devices Found"

fi

# Setting Built-in Ethernet 1

if [ "$en1" = "Ethernet" ]; then
    /usr/sbin/networksetup -setMedia "Ethernet 1" 100baseTX full-duplex
    echo "Built in Ethernet 1 Completed"

elif [ "$en1" = "" ]; then
    echo "No Devices Found"

fi

# Setting Built-in Ethernet 2

if [ "$en2" = "Ethernet" ]; then
    /usr/sbin/networksetup -setMedia "Ethernet 2" 100baseTX full-duplex
    echo "Built in Ethernet 2 Completed"

elif [ "$en2" = "" ]; then
    echo "No Devices Found"

fi


# Setting Thunderbolt Ethernet

if [ "$thunderbolt" = "Thunderbolt Ethernet" ]; then
    /usr/sbin/networksetup -setMedia "Thunderbolt Ethernet" 100baseTX full-duplex
    echo "Thunderbolt Ethernet Completed"

elif [ "$thunderbolt" = "" ]; then
    echo "No Devices Found"

fi

# Setting USB Ethernet

if [ "$usb" = "USB Ethernet" ]; then
    /usr/sbin/networksetup -setMedia "USB Ethernet" 100baseTX full-duplex
    echo "USB Ethernet Completed"

elif [ "$usb" = "" ]; then
    echo "No Devices Found"

fi

# Setting Display Ethernet

if [ "$display" = "Display Ethernet" ]; then
    /usr/sbin/networksetup -setMedia "Display Ethernet" 100baseTX full-duplex
    echo "Display Ethernet Completed"

elif [ "$display" = "" ]; then
    echo "No Devices Found"

fi

Basically what I am trying to do is set all the adapters to 100basetx Full-Duplex. What would be even cooler would be a way to detect which adapter is currently active that way if someone takes their laptop home they can be at auto and when they get back to the office and login it could reset back to 100basetx Full-Duplex.

Any opinions welcomed!

1 ACCEPTED SOLUTION

tron_jones
Release Candidate Programs Tester

This worked with just ethernet. Haven't tested with all the other devices.

#!/bin/bash

SERVICE_GUID=`printf "open
get State:/Network/Global/IPv4
d.show" | scutil | grep "PrimaryService" | awk '{print $3}'`

SERVICE_NAME=`printf "open
get Setup:/Network/Service/$SERVICE_GUID
d.show" | scutil | grep "UserDefinedName" | awk -F': ' '{print $2}'`

if [ "$SERVICE_NAME" = "Wi-Fi" ]; then
    echo "User on Wi-Fi"
    exit 0
elif [ "$SERVICE_NAME" != "Wi-Fi" ]; then
/usr/sbin/networksetup -setMedia "$SERVICE_NAME" 100baseTX full-duplex
    echo "$SERVICE_NAME Completed"
fi

The first part to find the current active device comes from
https://jamfnation.jamfsoftware.com/discussion.html?id=9887

View solution in original post

4 REPLIES 4

tron_jones
Release Candidate Programs Tester

This worked with just ethernet. Haven't tested with all the other devices.

#!/bin/bash

SERVICE_GUID=`printf "open
get State:/Network/Global/IPv4
d.show" | scutil | grep "PrimaryService" | awk '{print $3}'`

SERVICE_NAME=`printf "open
get Setup:/Network/Service/$SERVICE_GUID
d.show" | scutil | grep "UserDefinedName" | awk -F': ' '{print $2}'`

if [ "$SERVICE_NAME" = "Wi-Fi" ]; then
    echo "User on Wi-Fi"
    exit 0
elif [ "$SERVICE_NAME" != "Wi-Fi" ]; then
/usr/sbin/networksetup -setMedia "$SERVICE_NAME" 100baseTX full-duplex
    echo "$SERVICE_NAME Completed"
fi

The first part to find the current active device comes from
https://jamfnation.jamfsoftware.com/discussion.html?id=9887

Josh_S
Contributor III

I have not tested this at all, so make sure you do before deploying it. Also, as written, it makes the user unable to ever set their network service to anything other than "100baseTX full-duplex" while on the network, and unable to ever set it to "100baseTX full-duplex" while off network. Keep that in mind.

/Library/LaunchDaemons/com.company.automaticmedia.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>com.company.automaticmedia</string>
    <key>OnDemand</key>
    <true/>
    <key>ProgramArguments</key>
    <array>
        <string>/path/to/script.sh</string>
    </array>
    <key>WatchPaths</key>
    <array>
        <string>/Library/Preferences/SystemConfiguration</string>
    </array>
    <key>UserName</key>
    <string>root</string>
</dict>
</plist>

/path/to/script.sh - Set 'serverAddr' to a server that is only accessible while on your network.

# Set to a server name only accessible on your network.
serverAddr='servername'

SERVICE_GUID=`printf "open
get State:/Network/Global/IPv4
d.show" | scutil | grep "PrimaryService" | awk '{print $3}'`
SERVICE_NAME=`printf "open
get Setup:/Network/Service/$SERVICE_GUID
d.show" | scutil | grep "UserDefinedName" | awk -F': ' '{print $2}'`

# Check if serverAddr is Reachable
nc -z "$serverAddr" 80
if [ "$?" -eq "0" ]; then
    if [ "$(grep -c 'Ethernet' <<< "${SERVICE_NAME}")" -gt '0' ]; then
        /usr/sbin/networksetup -setMedia "${SERVICE_NAME}" 100baseTX full-duplex
    fi
else
    if [ "$(grep -c 'Ethernet' <<< "${SERVICE_NAME}")" -gt '0' ] && [ "$(/usr/sbin/networksetup -getMedia "${SERVICE_NAME}" | grep -c '100baseTX <full-duplex>')" -gt '0' ]; then
        /usr/sbin/networksetup -setMTUAndMediaAutomatically "${SERVICE_NAME}"
    fi
fi

Edit: Copied/pasted some parts from an old script. Removed some bits that didn't apply to this one.

Matt
Valued Contributor

Very nice!!!! Worked when plugged in to Ethernet. Unplugged and plugged in TB Ethernet and viola!

luke_jaeger
Contributor

Here's my modification, which sets Ethernet to autoselect and disables all other network interfaces. This is handy if you have assorted Mac models with different interface names. I didn't include 'Firewire' since I don't have any, but you could add that.

#!/bin/sh

# Setting Devices

# Set Built-in Ethernet to autoselect

if [[ `/usr/sbin/networksetup -listallnetworkservices | grep -x "Ethernet"` = "Ethernet" ]]; then
/usr/sbin/networksetup -setMedia "Ethernet" autoselect echo "Built in Ethernet Completed"
fi

# disable Thunderbolt Bridge

if [[ `/usr/sbin/networksetup -listallnetworkservices | grep -x "Thunderbolt Bridge"` ]]; then
/usr/sbin/networksetup -setnetworkserviceenabled 'Thunderbolt Bridge' off echo "Thunderbolt Bridge Completed"
fi

# disable Bluetooth DUN

if [[ `/usr/sbin/networksetup -listallnetworkservices | grep -x "Bluetooth DUN"` ]]; then
/usr/sbin/networksetup -setnetworkserviceenabled 'Bluetooth DUN' off echo "Bluetooth DUN Completed"
fi

# disable Bluetooth PAN

if [[ `/usr/sbin/networksetup -listallnetworkservices | grep -x "Bluetooth PAN"` ]]; then
/usr/sbin/networksetup -setnetworkserviceenabled 'Bluetooth PAN' off echo "Bluetooth PAN Completed"
fi

# disable Wi-fi

if [[ `/usr/sbin/networksetup -listallnetworkservices | grep -x "Wi-Fi"` ]]; then
/usr/sbin/networksetup -setnetworkserviceenabled 'Wi-Fi' off echo "Wi-Fi Completed"
fi
/usr/sbin/networksetup -listallnetworkservices