Skip to content

Instantly share code, notes, and snippets.

@Jbunsto
Created January 14, 2020 21:24
Show Gist options
  • Save Jbunsto/8f5b9e7d6cea643f5785e8595ce2ac30 to your computer and use it in GitHub Desktop.
Save Jbunsto/8f5b9e7d6cea643f5785e8595ce2ac30 to your computer and use it in GitHub Desktop.
echo on
#!/bin/sh
#!/usr/bin/env bash
# File Name: check_computer_name.sh
# Version: 1.5
# Author: Jason Bunston, City of Toronto
# Created: 06/02/2016
# Modified: 06/03/2016
if [ "$EUID" -ne 0 ]
then
printf "Please run as root\n"
exit
fi
clear
CURRENTMACHOST=$(sudo systemsetup -getcomputername | awk '/Computer Name/{print $NF}')
CURRENTIPHOST=$(sudo hostname)
SCUTILComputerName=$(sudo scutil --get ComputerName)
SCUTILHostName=$(sudo scutil --get HostName)
SCUTILLocalHostName=$(sudo scutil --get LocalHostName)
printf "Current Mac Host:\t\t$CURRENTMACHOST\n"
printf "Current IPhost:\t\t\t$CURRENTIPHOST\n"
printf "SC Util Computername:\t\t$SCUTILComputerName\n"
printf "SC Util HostName:\t\t$SCUTILHostName\n"
printf "SC Util LocalHostName:\t\t$SCUTILLocalHostName\n"
printf "\n"
sleep 1
funct_namecheck ()
{
printf "\n"
read -p "Enter new hostname:" FINALHOSTNAME
clear
}
# Check to see if the existing hostname matches the COT standard pattern for Mac Desktops and Notebooks
if [[ $CURRENTMACHOST == [A-Za-z][0-9][Xx][MOmoSs][0-9][0-9][0-9][0-9][0-9][0-9][0-9] ]];
then
printf "The existing hostname format is OK\n"
if [[ $CURRENTMACHOST = [a-z][0-9][x][mo][0-9][0-9][0-9][0-9][0-9][0-9][0-9] ]];
then
printf "Setting SystemName"
sudo systemsetup -setcomputername $(tr [A-Z] [a-z] <<< "$CURRENTMACHOST") > /dev/null 2>&1
sudo scutil --set ComputerName $(tr [A-Z] [a-z] <<< "$CURRENTMACHOST") > /dev/null 2>&1
sudo scutil --set HostName $(tr [A-Z] [a-z] <<< "$CURRENTMACHOST") > /dev/null 2>&1
sudo scutil --set LocalHostName $(tr [A-Z] [a-z] <<< "$CURRENTMACHOST") > /dev/null 2>&1
printf "Setting Hostname"
sudo hostname $(tr [A-Z] [a-z] <<< "$CURRENTMACHOST") >/dev/null 2>&1 > /dev/null 2>&1
fi
else
# Name format hasn't passed the check - we now ask for a new hostname to be entered, and validate it
printf "The current hostname does not adhere to the COT naming convention\n\n"
printf "Current System Name: $CURRENTMACHOST \n\n"
printf "This system must be renamed now\n"
while true;
do
funct_namecheck
if [[ $FINALHOSTNAME == [A-Za-z][0-9][Xx][MOmoSs][0-9][0-9][0-9][0-9][0-9][0-9][0-9] ]]
then
printf "The hostname you've entered passed the format test: $FINALHOSTNAME is OK\n"
break
else
Clear
printf "The hostname format your entered does not follow the COT Standard - please try again\n"
printf "NOTE: the format allowed must follow this convention:\n\n"
printf "(Divisional Prefix) + (X) + (Hardware type - M or O) + (Seven Digit Asset Tag #) \n\n"
printf "IE:\tJ2XM1234567 \n \tD1XO1234567 \n \tK4XM1234567 \n"
printf "\t...are all acceptable formats\n"
fi
done
# Check to see if the changes have been successuly applied.
if [[ $FINALHOSTNAME != $CURRENTMACHOST ]];
then
printf "Applying new hostname\n"
# IF current hostname doesn't match the target hostname make it so
sudo systemsetup -setcomputername $(tr [A-Z] [a-z] <<< "$FINALHOSTNAME") > /dev/null 2>&1
# Apply new computer name to Mac IP Hostname
sudo hostname $(tr [A-Z] [a-z] <<< "$FINALHOSTNAME")
sudo scutil --set ComputerName $(tr [A-Z] [a-z] <<< "$FINALHOSTNAME") > /dev/null 2>&1
sudo scutil --set HostName $(tr [A-Z] [a-z] <<< "$FINALHOSTNAME") > /dev/null 2>&1
sudo scutil --set LocalHostName $(tr [A-Z] [a-z] <<< "$FINALHOSTNAME") > /dev/null 2>&1
fi
fi
# Double Check to see if the changes have been successuly applied.
CURRENTMACHOST=$(sudo systemsetup -getcomputername | awk '/Computer Name/{print $NF}')
CURRENTIPHOST=$(sudo hostname)
SCUTILComputerName=$(sudo scutil --get ComputerName)
SCUTILHostName=$(sudo scutil --get HostName)
SCUTILLocalHostName=$(sudo scutil --get LocalHostName)
# If not, then reset those final name changes.
if [[ $CURRENTMACHOST != $CURRENTIPHOST ]];
then
printf "System name and IP Hostname weren't synced - enforcing"
sudo hostname $(tr [A-Z] [a-z] <<< "$FINALHOSTNAME") > /dev/null 2>&1
sudo scutil --set ComputerName $(tr [A-Z] [a-z] <<< "$FINALHOSTNAME") > /dev/null 2>&1
sudo scutil --set HostName $(tr [A-Z] [a-z] <<< "$FINALHOSTNAME") > /dev/null 2>&1
sudo scutil --set LocalHostName $(tr [A-Z] [a-z] <<< "$FINALHOSTNAME") > /dev/null 2>&1
fi
# Print out the final name changes for all to see.
printf "\n"
if [[ $FINALHOSTNAME != "" ]];
then
printf "Target Global Hostname:\t\t$FINALHOSTNAME\n"
printf "\n"
printf "Current System Name:\t\t$CURRENTMACHOST\n"
printf "Current IP Hostname:\t\t$CURRENTIPHOST\n"
printf "\n"
printf "Scutil Computername:\t\t$SCUTILComputerName\n"
printf "Scutil HostName:\t\t$SCUTILHostName\n"
printf "Scutil LocalHostName:\t\t$SCUTILLocalHostName\n"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment