Chinaunix首页 | 论坛 | 博客
  • 博客访问: 19733882
  • 博文数量: 679
  • 博客积分: 10495
  • 博客等级: 上将
  • 技术积分: 9308
  • 用 户 组: 普通用户
  • 注册时间: 2006-07-18 10:51
文章分类

全部博文(679)

文章存档

2012年(5)

2011年(38)

2010年(86)

2009年(145)

2008年(170)

2007年(165)

2006年(89)

分类: 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 2.6.9-67.ELsmp, #1 SMP Wed Nov 7 13:56:44 EST 2007

 

获取主机id

#include

long gethostid(void);

 

         sun中一般是读取了永久存储的中的信.Linux中一般根据Internet地址生成,不是很可靠的。

阅读(3508) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~