Created
December 13, 2019 16:55
-
-
Save grahampugh/75d0629fea18829f4bfbff1d3cab3dc9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
#################################################################################################### | |
# | |
# Change host name | |
# | |
#################################################################################################### | |
# | |
# HISTORY | |
# | |
# Version 1.0, 27-Mar-2018, Graham Pugh. | |
# Version 2.0, 15-Jun-2018, Graham Pugh. Added wait for network. Added verbosity to output. Set hostname to FQDN. | |
# Version 3.0, 28-Aug-2018, Graham Pugh. Grab DNS name from A record using `host` command. | |
#################################################################################################### | |
# Domain name | |
domain="company.com" | |
# Docking network string | |
docking="docking" | |
# Max time to wait for a DNS name (seconds) | |
countermax=60 | |
# DON'T EDIT BELOW HERE | |
# Get current ComputerName | |
computerName=$( scutil --get ComputerName ) | |
localHostName=$( scutil --get LocalHostName ) | |
hostName=$( scutil --get HostName ) | |
# Get current hostname from DNS | |
IP=$(ifconfig | grep "inet " | grep -m 1 -v 127.0.0.1 | cut -d ' ' -f2) | |
hostNamefromARecord=$(host $IP | rev | cut -d ' ' -f1 | rev | sed -e 's|\.$||') | |
# Check for FQDN, wait for network if necessary up to countermax limit | |
counter=0 | |
while [[ $counter -lt $countermax && $hostNamefromARecord != *$domain* ]]; do | |
IP=$(ifconfig | grep "inet " | grep -m 1 -v 127.0.0.1 | cut -d ' ' -f2) | |
hostNamefromARecord=$(host $IP | rev | cut -d ' ' -f1 | rev | sed -e 's|\.$||') | |
[[ $hostNamefromARecord == *$domain* ]] && break | |
echo "[$( date )] DNS name not found. Waiting for a connection..." | |
counter=$[${counter}+10] | |
echo "[$( date )] Waiting for a valid DNS name... ${counter} seconds" | |
sleep 10 | |
done | |
# Valid FQDN name returned | |
if [[ ${hostNamefromARecord} == *$domain* && ${hostNamefromARecord} != *$docking* ]]; then | |
echo "[$( date )] FQDN found from $domain domain." | |
if [[ "${hostName}" == "${hostNamefromARecord}" ]]; then | |
echo "[$( date )] hostName already set to '${hostName}'" | |
else | |
echo "[$( date )] HostName changing from '${hostName}' to '${hostNamefromARecord}'" | |
scutil --set HostName "${hostNamefromARecord}" | |
fi | |
hostName=$hostNamefromARecord | |
# Invalid name returned | |
elif [[ ${hostNamefromARecord} == *$domain* && ${hostNamefromARecord} == *$docking* ]]; then | |
echo "[$( date )] Computer connected to docking network. Using $hostName." | |
elif [[ $hostName ]]; then | |
echo "[$( date )] No FQDN found from $domain domain. Using $hostName." | |
# No HostName at all so need to quit | |
else | |
echo "[$( date )] No FQDN found from $domain domain, and no HostName set. Cannot proceed to change local hostnames." | |
exit 1 | |
fi | |
# We have a HostName, so check and repair ComputerName and LocalHostName as necessary | |
if [[ "${computerName}" == "${hostName%%.*}" ]]; then | |
echo "[$( date )] ComputerName already set to '${computerName}'" | |
else | |
echo "[$( date )] ComputerName changing from '${computerName}' to '${hostName%%.*}'" | |
scutil --set ComputerName "${hostName%%.*}" | |
fi | |
if [[ "${localHostName}" == "${hostName%%.*}" ]]; then | |
echo "[$( date )] LocalHostName already set to '${localHostName}'" | |
else | |
echo "[$( date )] LocalHostName changing from '${localHostName}' to '${hostName%%.*}'" | |
scutil --set LocalHostName "${hostName%%.*}" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment