The macosxhints Forums

The macosxhints Forums (http://hintsforums.macworld.com/index.php)
-   UNIX - Newcomers (http://hintsforums.macworld.com/forumdisplay.php?f=15)
-   -   Command to retrieve computer name? (not hostname) (http://hintsforums.macworld.com/showthread.php?t=101493)

brycesutherland 05-11-2009 07:49 PM

Command to retrieve computer name? (not hostname)
 
Hi,

What unix command should I use to retrieve the computer name of my machine? We're connected via Active Directory, so whenever I type `hostname` in Terminal (or a shell script) I get a long string with my IP address and network location.

However, I need to get the name of the workstation that's visible in the Sharing pref pane. Something human-readable like Media-Studio-Mac-4.

Is there a unix command for something Mac-specific like that?

Any help would be appreciated!

Thanks,
Glenn

tw 05-11-2009 08:35 PM

does uname -n do what you want?

ganbustein 05-12-2009 12:31 AM

Quote:

Originally Posted by brycesutherland (Post 532548)
However, I need to get the name of the workstation that's visible in the Sharing pref pane. Something human-readable like Media-Studio-Mac-4.

For a starting point,

defaults read /Library/Preferences/SystemConfiguration/preferences System

followed by stripping out just the name you want. The easiest way to do that is with PlistBuddy, but first you have to tell bash where to find it.

On Tiger it's not part of the standard install, but it's so useful that you have about a kajillion of them squirreled away in /Library/Receipts. You can just aim bash at the newest copy:
Code:

hash -p "$(ls -t /Library/Receipts/*/Contents/Resources/PlistBuddy | head -1 )" PlistBuddy
On Leopard, that still works but better would be to use the copy installed in /usr/libexec/PlistBuddy. That's normally a little off the beaten path, so instead of the above:
Code:

hash -p /usr/libexec/PlistBuddy PlistBuddy
Either way, you can now say:
Code:

PlistBuddy -c 'Print System:System:ComputerName' /Library/Preferences/SystemConfiguration/preferences.plist

brycesutherland 05-12-2009 12:55 PM

ganbustein, that got me part of the way there!

Code:

PlistBuddy -c 'Print System:Network:HostNames:LocalHostName' /Library/Preferences/SystemConfiguration/preferences.plist
outputs what I want, "Multimedia-Studio-Mac-4"

However, I can't get it to work inside a shell script. This code fails (I think):
Code:

hostname=`PlistBuddy -c 'Print System:Network:HostNames:LocalHostName' /Library/Preferences/SystemConfiguration/preferences.plist`
When I run my script, I get the error: PlistBuddy: command not found.

Strange that it works in the Terminal but not in the script?

Ideas?

Thanks!

baf 05-12-2009 01:31 PM

Do you have:
hash -p .....
In your script ? That adds a temporary path to the command.
Better is to either fix the scripts path with something like
PATH=/usr/libexec/:$PATH
or giving the full path to PlistBuddy
/usr/libexec/PlistBuddy

brycesutherland 05-12-2009 05:59 PM

Thanks, baf. I added the hash -p line to the script (and added the PATH declaration at the top), and it works as expected.

tw 05-13-2009 11:52 AM

I'm curious. in a quick test uname -n (or rather uname -n | egrep -o '^[^.]+' -) seems to work fine, and is a lot simpler code. what am I missing?

NovaScotian 05-13-2009 12:06 PM

Works perfectly in my G5 (OS X 10.5.7) except that the name returned has an appended "-2" attached.

tw 05-13-2009 12:37 PM

Quote:

Originally Posted by NovaScotian (Post 532821)
Works perfectly in my G5 (OS X 10.5.7) except that the name returned has an appended "-2" attached.

you sure that's not part of your machine name? maybe you have two machines with the same name hooked up to your router, and the system is automatically generating the '-2'. at any rate, I can't for the life of me think how grep would append something to its output. that's very un-grep-like.

hmmm... ingrepitous? ungrepistic? non-grepular???

Hal Itosis 05-13-2009 12:47 PM

Quote:

Originally Posted by NovaScotian (Post 532821)
Works perfectly in my G5 (OS X 10.5.7) except that the name returned has an appended "-2" attached.

And what do these (four) get you then?

/usr/libexec/PlistBuddy -c 'Print System:Network:HostNames:LocalHostName' \
/Library/Preferences/SystemConfiguration/preferences.plist

hostname -s

networksetup -getcomputername 2>/dev/null

system_profiler SPSoftwareDataType |sed '/ *Computer Name: /!d;s///'

brycesutherland 05-13-2009 01:23 PM

Quote:

Originally Posted by tw (Post 532818)
I'm curious. in a quick test uname -n (or rather uname -n | egrep -o '^[^.]+' -) seems to work fine, and is a lot simpler code. what am I missing?

When I type uname -u into Terminal, I get:
dhcp-10-10-208-170.lib-s.pacific.edu

For the purposes of my script, that name is really unfriendly and I so I wanted to know how to get the LocalHostName instead. However, since that's apparently not tied directly to a simple unix command, we had to go the hash & PlistBuddy route.

Glenn

NovaScotian 05-13-2009 02:32 PM

Quote:

Originally Posted by tw (Post 532829)
you sure that's not part of your machine name? maybe you have two machines with the same name hooked up to your router, and the system is automatically generating the '-2'. at any rate, I can't for the life of me think how grep would append something to its output. that's very un-grep-like.

hmmm... ingrepitous? ungrepistic? non-grepular???

When I go to my laptop and look at Shared, the machine is identified (by the Finder) as ACB-G5 and in a Finder window's "devices" pane it's the same, but uname -n identifies it as ACB-G5-2, and without the pipe to egrep (just uname -n) it says ACB-G5-2.local.

The devices in my LAN are an AirPort (named ACB-AEN), my laptop (named ACB-MBP) connected by WiFi and my dual-core G5 (named ACB-G5) connected by wire.

tlarkin 05-13-2009 02:58 PM

uh this sound way too complicated to get the computer name from the command line in 10.5, here is an example

Code:

bash-3.2# networksetup -getcomputername
Admin 205

My computer name is Admin 205

NovaScotian 05-13-2009 03:10 PM

Which, btw, returns the correct name for me: ACB-G5

tw 05-13-2009 03:19 PM

Quote:

Originally Posted by NovaScotian (Post 532851)
The devices in my LAN are an AirPort (named ACB-AEN), my laptop (named ACB-MBP) connected by WiFi and my dual-core G5 (named ACB-G5) connected by wire.

interesting. and there's no ACB-G5 or ACB-G5-1? I wonder if it has an internal record of an old machine or different user that had the same name, and is calling your current one '2' to keep them separate?

at any rate, it looks like tlarkin might be onto it, though there might be something funky going on with DHCP systems that's not showing up on my machine at home. have to wait till I get to campus and see what it looks like there. :)

Hal Itosis 05-13-2009 07:16 PM

Quote:

Originally Posted by tw (Post 532862)
at any rate, it looks like tlarkin might be onto it

Say who? [the posts are in numerical order ya know. ;) ]

tw 05-13-2009 07:27 PM

Quote:

Originally Posted by Hal Itosis (Post 532923)
Say who? [the posts are in numerical order ya know. ;) ]

yes, but my brain isn't. :p

hayne 05-13-2009 09:15 PM

It seems (from the source code for 'uname') that it gets the same value as:
sysctl -n kern.hostname
but I note that this name is referred to in the source code as "node name" which makes me think it is the name given to this "node" on the network. And empirically, that's what it seems to be. It seems to be the name assigned to this computer by the DHCP server.

So I think maybe it's like this:
The name you specify in the Sharing preferences is what you'd like the machine to be named, but the DHCP server is the final authority on what it is named on the network and the DHCP server sometimes (often?) doesn't accept the name that you suggest.

NovaScotian 05-14-2009 10:01 AM

That's solid reasoning, but my DHCP server is an AirPort Extreme. If I use the AirPort Utility, select "Manual", then under the "Advanced" tab click the "Logs & Statistics" button and in that pane select the DHCP clients tab, I get a table with two entries, one column of which is Client ID. There, it says "ACB-MBP" and "ACB-G5" without appended numbers. That doesn't rule out your theory, since those may be simplified presentations to what is actually assigned as a DHCP node.

tlarkin 05-14-2009 12:11 PM

OK, but the Unix networking client doesn't care what your computer name is. You can have 1,000 computers with the same name on a Unix network, they will just have a (1), (2), and so on after the name.

The Windows networking client however does not like duplicate names at all. If you are managing your groups or authenticating against AD by computer groups I can see where this can be an issue.

If you are adding the machine to AD you can script it to bind with the computer name as a simple variable like this:

compname=/usr/sbin/networksetup -getcomputername

or

compname=/usr/sbin/scutil --get ComputerName

or any other method mentioned.

Is this what you are trying to do? Are you trying to add the computer name to an AD group or something?

As baf pointed out if you use PlistBuddy it is not in the standard $PATH so you will have to use full path (which is recommended anyway) or make sure your script is set to use that path. I personally find PlistBuddy rather tricky to use right at times, but that is just me personally.


So can you maybe explain what your higher goal is here so we can better assist you?

Thanks

Hal Itosis 12-11-2009 10:58 PM

commands to retrieve computer name
 
FWIW, i pieced together this script to quickly run through all [eight!] possibilities, so we can quickly compare their various outputs. I even added another approach that uses AppleScript (via osascript). So far, i think scutil seems the simplest. Before you discount the PlistBuddy method, it's the only one that works in [raw] single-user mode... though i'm not sure there's much use for knowing the computer name there.

Code:

#!/bin/bash -
# compare eight methods for getting the "Computer Name"
IFS=$' \t\n'
declare -x PATH=/bin:/usr/bin:/sbin:/usr/sbin:/usr/libexec

function show () { printf '\n\e[1m%s\e[0m\n' "$1"; }

show 'hostname -s'
hostname -s

show 'scutil --get ComputerName'
scutil --get ComputerName

show "uname -n |sed 's/\..*$//'"
uname -n |sed 's/\..*$//'

show "sysctl -n kern.hostname |sed 's/\..*$//'"
sysctl -n kern.hostname |sed 's/\..*$//'

show 'networksetup -getcomputername 2>/dev/null'
networksetup -getcomputername 2>/dev/null

show "osascript -e 'computer name of (system info)'"
osascript -e 'computer name of (system info)'

show "system_profiler SPSoftwareDataType |sed '/ *Computer Name: /!d;s///'"
system_profiler SPSoftwareDataType |sed '/ *Computer Name: /!d;s///'

show "PlistBuddy -c 'Print System:Network:HostNames:LocalHostName' \\
/Library/Preferences/SystemConfiguration/preferences.plist"
PlistBuddy -c 'Print System:Network:HostNames:LocalHostName' \
/Library/Preferences/SystemConfiguration/preferences.plist

echo
exit


tlarkin 12-12-2009 01:18 AM

Hal-

You forgot a method, there is also a switch in the systemsetup command to get and set the computer name.

Shardy 12-12-2009 07:38 AM

Well personally being a bit "old school" and a long time Unix head I'd use this...

uname -n | awk -F. '{ print $1 }'

:D

Hal Itosis 12-12-2009 03:08 PM

Hmm, interesting and weird: it pops up a *GUI* dialog requesting a password!!! :eek:

Think i'll skip that one. (it appears to be networksetup's little brother anyway).

renaultssoftware 01-03-2010 07:32 PM

Must be some OSA script:
Code:

osascript -e 'computer name of (system info)'
This is the simplest way.

Hal Itosis 01-03-2010 11:53 PM

I don't understand what's going on here. I visited this thread several times since posting my 12-12-2009, 03:08 PM reply (post #24) to tlarkin. The post by Shardy was never here then (so in fact, my post was actually numbered #23 when i replied back in Dec.). And it remained so for a while when i checked back. [i.e., no Shardy post.]

Now both Shardy and renaultssoftware are repeating stuff already contained in my post #21. It's almost as if they (at times) can't see my posts and i (at times) can't see theirs.

Maybe it's that thing where new member posts don't display until they're approved? If so, that would be *my* excuse then.

NovaScotian 01-04-2010 09:22 AM

Perhaps the Mods merged two threads.

anika123 01-04-2010 11:54 AM

Hal, I just now saw your post after visiting this thread several times.


All times are GMT -5. The time now is 10:19 PM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.