Renaming network services

martin
Contributor III

Hi,

I want to share a script with you I've just created.

You probably all are familiar when creating an base image on a Mac Pro and
image a MacBook Pro with it that the network service names are like
Ethernet 1, Ethernet 2 and FireWire. Ethernet 2 is actually the AirPort
(or Wi-Fi). The script I created renames the service to the hardware port
name if they do not match. After running the script on a MacBook Pro (with
10.6) the result will be: Ethernet, AirPort and FireWire.

#!/bin/bash

# renameNetworkServices.sh

# Script to correct the network services names after imaging a new machine

# Detect new network hardware
networksetup -detectnewhardware

# List all network services and read one by one
networksetup -listallnetworkservices | tail -n +2 | while read service
do # Check if network service is disabled (note the asterisk) if [[ ${service} == ** ]] then # Remove asterisk from string for renaming service service=echo ${service} | sed -e 's/.(.*)/1/' fi

# Use filter to select next line which has the harware port defined filter=false

# Display network services networksetup -listnetworkserviceorder | while read serviceorder do if [[ ${filter} == true ]] then # Grab hardware port hardwareport=`echo ${serviceorder} | sed -e 's/(Hardware Port:
//;s/, Device:.*//'` # Check if service name if different if [[ ${service} != ${hardwareport} ]] then # Rename the network service networksetup -renamenetworkservice "${service}"
"${hardwareport}" echo -e "Renamed network server "${service}" to
"${hardwareport}"" fi fi

if [[ ${serviceorder} == *${service} ]] then # Got the line with the service. Set the filter to true to
grab the next line which contains the hardware port filter=true else filter=false fi done
done

Have fun with it!

Kind Regards,

Martin van Diemen

4 REPLIES 4

nick_caro
New Contributor

is this Lion compatible?

rmanly
Contributor III

Edit this and re-paste in the script tags PRETTY PLEASE!

:)

fritz_schlapbac
Contributor

I've played around with Martin's script and it didn't work like expected on Lion. (Especially the part to remove the asterix (*) when a network service is disabled)

After some changes in the code it's working now for me on Lion and Mountain Lion. I'm not sure if it's perfect what I've changed. I recommend to test it first. ;)

Thanks to Martin for the script.

#!/bin/bash

# renameNetworkServices.sh

# Script to correct the network services names after imaging a new machine

# Detect new network hardware
networksetup -detectnewhardware

# List all network services and read one by one
networksetup -listallnetworkservices | tail -n +2 | while read service
do

# Remove asterisk from string for renaming disabled services
service=${service#**}

# Use filter to select next line which has the hardware port defined
filter=false

# Display network services
networksetup -listnetworkserviceorder | while read serviceorder
do
if [[ ${filter} == true ]]
then
# Grab hardware port
hardwareport=`echo ${serviceorder} | sed -e 's/(Hardware Port: //;s/, Device:.*//'`
# Check if service name if different
if [[ ${service} != ${hardwareport} ]]
then
# Rename the network service
networksetup -renamenetworkservice "${service}" "${hardwareport}"
echo -e "Renamed network service "${service}" to "${hardwareport}""
fi
fi

if [[ ${serviceorder} == *${service} ]]
then

# Got the line with the service. Set the filter to true to grab the next line which contains the hardware port
filter=true
else
filter=false
fi
done
done

franton
Valued Contributor III

Thank you guys! This has been a major headache for me over this past week.