Detect and adding external network adapters.

jhalvorson
Valued Contributor

Sometimes after the first reboot after using Casper Imaging, the network adapter isn't detected and some scripts and binding that require a working network connection will fail.

Our imaging process still relies on Netbooting and using Casper Imaging. We are "thin" imaging them by not including a AutoDMG based OS. The factory macOS is left in place.

I've updated a script that has been around for years to support both the a Apple "Belkin USB-C" and a StarTech USB-C adapter.

You may need to modify if you are using different brand/model USB-C Ethernet adapters. Use the following terminal command to display the adapter's name:

networksetup -listallhardwareports

Here's the script:

#!/bin/bash

# Checks to see if the Mac is either a MacBook, MacBook Pro Retina or MacBook Air.
# If it's any of these machines, the script will then check for external USB
# or Thunderbolt network adapters. If an adapter is present, it will add the 
# adapter to network services.
#
# Original script by Allen Golbig:
# https://github.com/golbiga/Scripts/tree/master/enable_external_network_adapter
# REF https://github.com/rtrouton/rtrouton_scripts/tree/master/rtrouton_scripts/enable_external_network_adapter
# Modified: 2017-01-27 to included USB-C adapters from Belkin and Startech - Jhalvorson


##########################################################################################
# Create time date stamped log to record actions taken by this script.
##########################################################################################
logFile="/private/var/log/companynamehereInfoTech.log"

log () {
    /bin/echo $1
    /bin/echo $(date "+%Y-%m-%d %H:%M:%S: ") $1 >> $logFile 
}

log "-----"
log "Begin 010_Network_enable_external_adapters.sh"

macbook_check=`/usr/sbin/system_profiler SPHardwareDataType | awk '/Model Name/' | awk -F': ' '{print substr($2,1,7)}'`

usbcstartechus1fc30a=`/usr/sbin/networksetup -listallhardwareports | grep "Hardware Port: USB 10/100/1000 LAN"`
usbcbelkinf2cu040=`/usr/sbin/networksetup -listallhardwareports | grep "Hardware Port: Belkin USB-C LAN"`

usbGigAdapter=`/usr/sbin/networksetup -listallhardwareports | grep "Hardware Port: USB Gigabit Ethernet"`
usbAdapter=`/usr/sbin/networksetup -listallhardwareports | grep "Hardware Port: USB Ethernet"`
usbAppleAdapter=`/usr/sbin/networksetup -listallhardwareports | grep "Hardware Port: Apple USB Ethernet Adapter"`

tbAdapter=`/usr/sbin/networksetup -listallhardwareports | grep "Hardware Port: Thunderbolt Ethernet"`
tbDisplay=`/usr/sbin/networksetup -listallhardwareports | grep "Hardware Port: Display Ethernet"`

/usr/sbin/networksetup -detectnewhardware

if [ "$macbook_check" = "MacBook" ]; then
    if [ "$usbcstartechus1fc30a" != "" ]; then
        /usr/sbin/networksetup -createnetworkservice USB 10/100/1000 LAN 'USB 10/100/1000 LAN'
        log "-"
        log "USB-C StarTech us1fc30a added to Network Services"
        log "-"
    else
        log "No USB-C Startech connected"
    fi
        if [ "$usbcbelkinf2cu040" != "" ]; then
        /usr/sbin/networksetup -createnetworkservice Belkin USB-C LAN 'Belkin USB-C LAN'
        log "-"
        log "USB-C Belkin 2cu040 added to Network Services"
        log "-"
    else
        log "No USB-C Belkin connected"
    fi
    if [ "$usbAdapter" != "" ]; then
        /usr/sbin/networksetup -createnetworkservice USB Ethernet 'USB Ethernet'
        log "-"
        log "USB Ethernet added to Network Services"
        log "-"
    else
        log "No USB Adapter connected"
    fi
    if [ "$usbAdapter" != "" ]; then
        /usr/sbin/networksetup -createnetworkservice Apple USB Ethernet Adapter 'Apple USB Ethernet Adapter'
        log "-"
        log "Apple USB Ethernet Adapter added to Network Services"
        log "-"
    else
        log "No Apple USB Ethernet Adapter connected"
    fi
    if [ "$usbGigAdapter" != "" ]; then
        /usr/sbin/networksetup -createnetworkservice USB Gigabit Ethernet 'USB Gigabit Ethernet'
        log "-"
        log "USB Gigabit Ethernet Adapter added to Network Services"
        log "-"
    else
        log "No USB Gigabit Ethernet Adapter connected"
    fi
    if [ "$tbAdapter" != "" ]; then
        /usr/sbin/networksetup -createnetworkservice Thunderbolt Ethernet 'Thunderbolt Ethernet'
        log "-"
        log "Thunderbolt Ethernet added to Network Services"
        log "-"
    else
        log "No Thunderbolt Adapter connected"
    fi
    if [ "$tbDisplay" != "" ]; then
        /usr/sbin/networksetup -createnetworkservice Display Ethernet 'Display Ethernet'
        log "-"
        log "Display Ethernet added to Network Services"
        log "-"
    else
        log "No Display Ethernet connected"
    fi
else
    log "-----"
    log "This machine does not use external network adapters"
    log "-----"
fi

log "Completed 010_Network_enable_external_adapters.sh"

exit 0

The script is useful for us in two ways.
1. It's one of the first packages that runs after reboot. (I've used @rtrouton Payload-Free Package Creator.app) to "convert" the script into a package.
2. I've added it to self server so that our non-admin users can add their own adapters if it happens to be different than what the configuration centers used. The SS policy also includes the

jamf log

command to update the IP in the JSS. (User's need to connect to wifi to be able to get the Self Service app to work.)

0 REPLIES 0