![]() |
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 |
does uname -n do what you want?
|
Quote:
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 )" PlistBuddyCode:
hash -p /usr/libexec/PlistBuddy PlistBuddyCode:
PlistBuddy -c 'Print System:System:ComputerName' /Library/Preferences/SystemConfiguration/preferences.plist |
ganbustein, that got me part of the way there!
Code:
PlistBuddy -c 'Print System:Network:HostNames:LocalHostName' /Library/Preferences/SystemConfiguration/preferences.plistHowever, 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`Strange that it works in the Terminal but not in the script? Ideas? Thanks! |
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 |
Thanks, baf. I added the hash -p line to the script (and added the PATH declaration at the top), and it works as expected.
|
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?
|
Works perfectly in my G5 (OS X 10.5.7) except that the name returned has an appended "-2" attached.
|
Quote:
hmmm... ingrepitous? ungrepistic? non-grepular??? |
Quote:
/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///' |
Quote:
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 |
Quote:
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. |
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 |
Which, btw, returns the correct name for me: ACB-G5
|
Quote:
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. :) |
Quote:
|
Quote:
|
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. |
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.
|
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 |
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 - |
Hal-
You forgot a method, there is also a switch in the systemsetup command to get and set the computer name. |
Well personally being a bit "old school" and a long time Unix head I'd use this...
uname -n | awk -F. '{ print $1 }' :D |
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). |
Must be some OSA script:
Code:
osascript -e 'computer name of (system info)' |
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. |
Perhaps the Mods merged two threads.
|
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.