macos 如何发现机器类型?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/481355/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 06:01:21  来源:igfitidea点击:

How to discover the machine type?

linuxmacosshellscriptingsolaris

提问by Giovanni Funchal

I would like to discover the machine architecture type of a big number of machines. I have the hostname of each machine. The machines have Debian 4 linux, SunOS 9, SunOS 10 or Apple Darwin. All are unix-like, but with minor differences.

我想发现大量机器的机器架构类型。我有每台机器的主机名。这些机器有 Debian 4 linux、SunOS 9、SunOS 10 或 Apple Darwin。所有都是类 Unix,但有细微的差别。

I would like to know: - architecture (x86, x86_64, ia64, sparc, powerpc...) - processor type (intel pentium, pentium pro, pentium II, sparc, powerpc, itanium, athlon, core 2 duo, cytrix, etc...) - number of processors

我想知道: - 架构(x86、x86_64、ia64、sparc、powerpc...) - 处理器类型(intel pentium、pentium pro、pentium II、sparc、powerpc、itanium、athlon、core 2 duo、cytrix 等...) - 处理器数量

Beware, I want the "type" of the machine. The stupid approach using 'uname' does not work on Sun and it also returns things like 'i686' when the machine is in fact 'x86_64' but the operating system is 32 bits. /proc/cpuinfo doesn't work neither, and things get even more complicated because some machines dont have a C compiler installed (I'm sure they all have sh, perhaps python or perl, dunno).

当心,我想要机器的“类​​型”。使用“uname”的愚蠢方法在 Sun 上不起作用,当机器实际上是“x86_64”但操作系统是 32 位时,它也会返回诸如“i686”之类的东西。/proc/cpuinfo 也不起作用,事情变得更加复杂,因为有些机器没有安装 C 编译器(我确定它们都有 sh,也许是 python 或 perl,不知道)。

Thanks in advance!! :)

提前致谢!!:)

回答by Joshua

arch ; uname -a

arch ; uname -a

arch is the standard way to get the name of the CPU instruction set. uname -a gets a bunch of stuff about the OS. uname withouth the a gets the OS name.

arch 是获取 CPU 指令集名称的标准方法。uname -a 获取有关操作系统的大量信息。uname withouth a 获取操作系统名称。

回答by Chris Lutz

You can try the following Perl one-liner:

你可以试试下面的 Perl one-liner:

perl -MConfig -e 'print "$Config{myarchname}\n";'

I know on Mac OS X Leopard with Perl 5.10.0 it prints "i386-darwin". I don't know of a way in Perl to get the actual processor name - your best bet is probably C since it's a fairly limited set of possibilities. You can get at a C compiler's predefined macros from Perl:

我知道在带有 Perl 5.10.0 的 Mac OS X Leopard 上它会打印“i386-darwin”。我不知道在 Perl 中有什么方法可以获取实际的处理器名称 - 您最好的选择可能是 C,因为它的可能性相当有限。您可以从 Perl 获得 C 编译器的预定义宏:

perl -MConfig -e 'print join("\n", split(/ /, $Config{cppsymbols})), "\n";'

This will list C macros like __LITTLE_ENDIAN__and __MACH__for Mach-O format and __i386__(on Leopard at least), as well as the useless ones like __GNUC__and __STDC__. Of course, all of this help assumes you have Perl on all machines. If not, I'm sure other languages have similar facilities to help you.

这将列出C宏象__LITTLE_ENDIAN____MACH__为Mach-O的格式__i386__(在Leopard至少),以及无用的像__GNUC____STDC__。当然,所有这些帮助都假设您在所有机器上都安装了 Perl。如果没有,我相信其他语言也有类似的功能可以帮助您。

回答by user49117

Why /proc/cpuinfo doesn't work?

为什么 /proc/cpuinfo 不起作用?

I don't know all of the OSs you mentioned, but I think it give quite detailed information under Linux. At least, with the model of CPU, other informations can be looked up from a data table.

我不知道你提到的所有操作系统,但我认为它在 Linux 下提供了相当详细的信息。至少,有了CPU的型号,其他的信息可以从数据表中查到。

The software can only see what the OS let it to see, so if the OS doesn't provide informations like/proc/cpuinfo, then you'll have no way to know it.

软件只能看到操作系统让它看到的东西,所以如果操作系统不提供/proc/cpuinfo这样的信息,那么你将无法知道它。

reply to the comments:

回复评论:

I am not saying look for /proc/cpuinfo for all the OS. It's a two steps method: first you find out which OS it is using uname, then look for the OS specific position for the cpu info.

我并不是说为所有操作系统寻找 /proc/cpuinfo。这是一个两步方法:首先找出它使用的是哪个操作系统 uname,然后查找 CPU 信息的操作系统特定位置。

回答by earino

I would suggest you look at the facter component of the Puppet system. From the URL http://reductivelabs.com/projects/facter/.

我建议您查看 Puppet 系统的因子组件。从 URL http://reductivelabs.com/projects/facter/

A cross-platform Ruby library for retrieving facts from operating systems. Supports multiple resolution mechanisms, any of which can be restricted to working only on certain operating systems or environments. Facter is especially useful for retrieving things like operating system names, IP addresses, MAC addresses, and SSH keys.
用于从操作系统检索事实的跨平台 Ruby 库。支持多种解析机制,其中任何一种都可以限制为仅在某些操作系统或环境中工作。Facter 对于检索操作系统名称、IP 地址、MAC 地址和 SSH 密钥等内容特别有用。

回答by Foo

This is not a complete answer, but it may help you reach your goal.

这不是一个完整的答案,但它可以帮助您实现目标。

arch does not work on HP-UX Itanium, and it does not have the /proc filesystem, as you mentioned.

arch 在 HP-UX Itanium 上不起作用,而且它没有 /proc 文件系统,正如您所提到的。

Thisexplains how to determine the endianness (byte order) the O/S is using simply with shell commands. It works on at least 4 major Unixes (Linux x86_64, Solaris Sparc, AIX/Power, HP-UX Itanium). If you know the byte ordering you can deduce a lot about which CPU you're dealing with, based on thissource.

解释了如何通过 shell 命令来确定 O/S 正在使用的字节顺序(字节顺序)。它至少适用于 4 个主要 Unix(Linux x86_64、Solaris Sparc、AIX/Power、HP-UX Itanium)。如果您知道字节顺序,则可以根据来源推断出很多有关您正在处理的 CPU 的信息。

For instance, if Solaris won't tell you the correct architecture, but you see that it's big endian, you at least know you're not on x86_64, and probably Sparc.

例如,如果 Solaris 不会告诉您正确的体系结构,但您看到它是大端,那么您至少知道您不是在 x86_64 上,而且可能是 Sparc。

Finally, for Sparc you can do this to determine whether the OS is running in 32 or 64 bit mode:

最后,对于 Sparc,您可以这样做来确定操作系统是在 32 位模式还是 64 位模式下运行:

# isalist -v
sparcv9+vis2 sparcv9+vis sparcv9 sparcv8plus+vis2 sparcv8plus+vis sparcv8plus sparcv8 sparcv8-fsmuld sparcv7 sparc

If it says 'sparcv9' it's 64 bit, sparcv8 is 32

如果它说 'sparcv9' 它是 64 位,sparcv8 是 32

回答by ItsMe

The use of uname -awill bring you to much information, so you have to search for the information you want to have.

的使用uname -a会给你带来很多信息,所以你必须搜索你想要的信息。

Just use:

只需使用:

uname -i

for the hardware platform

对于硬件平台

or

或者

uname -m

for the machine hardware name

对于机器硬件名称