分类: LINUX
2008-12-03 16:36:46
可以使用uname系统调用实现。
获取主机名:
#include
int gethostname(char
*name, size_t namelen);
获取详细信息:
#include
int uname(struct
utsname *name);
utsname的结构:
utsname Member
Description
char sysname[] The
operating system name
char nodename[] The
host name
char release[] The
release level of the system
char version[] The
version number of the system
char machine[] The
hardware type
实例:
# cat hostget.c
#include
#include
#include
#include
int main()
{
char computer[256];
struct utsname uts;
if(gethostname(computer, 255) != 0 ||
uname(&uts) < 0) {
fprintf(stderr, "Could not get
host information\n");
exit(1);
}
printf("Computer host name is
%s\n", computer);
printf("System is %s on %s hardware\n",
uts.sysname, uts.machine);
printf("Nodename is %s\n",
uts.nodename);
printf("Version is %s, %s\n",
uts.release, uts.version);
exit(0);
}
运行结果:
# ./hostget
Computer host name is
localhost.localdomain
System is Linux on
x86_64 hardware
Nodename is
localhost.localdomain
Version is
获取主机id
#include
long
gethostid(void);
sun中一般是读取了永久存储的中的信.Linux中一般根据Internet地址生成,不是很可靠的。