Skip to main content
Solved

Script to match hostname to localhostname or computername...

  • September 28, 2016
  • 5 replies
  • 0 views

Forum|alt.badge.img+2

I have a seemingly common issue.

When checking the hostnames with scutil --get ComputerName and it's LocalHostName and HostName commands, the HostName is not always set. As such, the hostname tends to be set to some random name.

Is there a command I can use that will automatically match the HostName to a ComputerName or LocalHostName variable? I'd like to push this out remotely in bulk over ARD.

Best answer by mm2270

Hi. You have half the answer already in your post. Since you can grab the ComputerName from the Mac in a script with:

scutil --get ComputerName

You can use that as a variable to set the HostName string to the same with a scutil --set command. For example:

#!/bin/sh

## This creates a variable string with the result of the Computer Name
CompName=$(scutil --get ComputerName)

## Now use that to set the HostName to match the ComputerName string
scutil --set HostName "$CompName"

Keep in mind the above is very simplistic. It does no error checking or making sure the ComputerName is even set to something valid (though I don't know if such a check would be needed), but it is the basic framework of how you would do this.

You could also simplify this further with a one-liner if needed:

scutil --set HostName "$(scutil --get ComputerName)"
View original
Did this topic help you find an answer to your question?

5 replies

mm2270
Forum|alt.badge.img+24
  • Legendary Contributor
  • 7881 replies
  • Answer
  • September 28, 2016

Hi. You have half the answer already in your post. Since you can grab the ComputerName from the Mac in a script with:

scutil --get ComputerName

You can use that as a variable to set the HostName string to the same with a scutil --set command. For example:

#!/bin/sh

## This creates a variable string with the result of the Computer Name
CompName=$(scutil --get ComputerName)

## Now use that to set the HostName to match the ComputerName string
scutil --set HostName "$CompName"

Keep in mind the above is very simplistic. It does no error checking or making sure the ComputerName is even set to something valid (though I don't know if such a check would be needed), but it is the basic framework of how you would do this.

You could also simplify this further with a one-liner if needed:

scutil --set HostName "$(scutil --get ComputerName)"

Forum|alt.badge.img+4

Here is the basic script we use. It seems to have fixed a few random issues we were having.

#!/bin/sh
#!/bin/bash



# Get the Computer name using scutil

setName=`scutil --get ComputerName`



scutil --set LocalHostName ${setName}
scutil --set HostName ${setName}

echo HostName and LocalHostName set to ${setName}

exit 0

Forum|alt.badge.img+2
  • Author
  • New Contributor
  • 1 reply
  • October 6, 2016

thanks that was exactly what I needed (scutil --set HostName "$(scutil --get ComputerName)") The simpler the better.

any thought on matching the DNS Name in ARD to the computername or hostname via a script? Or is this something that has to be fixed on the dhcp/dns server side? Or after making the hostname change, does it just take a while for the changes to reflect in DNS? Such as a DHCP Renewal?


mm2270
Forum|alt.badge.img+24
  • Legendary Contributor
  • 7881 replies
  • October 6, 2016

What you're describing is a DHCP issue. Hostnames, once picked up, often get retained within DHCP and will get assigned to the machine that picks up that IP the next time. Its a pain no doubt. Ask anyone that has used ARD and they will say they've seen the same thing.
A DHCP renewal may help, but I don't know for sure to be honest.


donmontalvo
Forum|alt.badge.img+36
  • Legendary Contributor
  • 4293 replies
  • October 8, 2016

Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings