Linux编程之获取每台主机的唯一标识

Linux 编程中可以用API 获取主机的唯一标识,许可证管理者也会用此来保证相关程序只在拥有合法许可证的机器上运行。

  #include <unistd.h>

   long gethostid(void);
 

AME
       gethostid, sethostid - get or set the unique identifier of the current host

SYNOPSIS
       #include <unistd.h>

       long gethostid(void);
       int sethostid(long hostid);

   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):

       sethostid(): _BSD_SOURCE || (_XOPEN_SOURCE && _XOPEN_SOURCE < 500)

DESCRIPTION
       gethostid() and sethostid() respectively get or set a unique 32-bit identifier for the current machine.  The 32-bit identifier is intended to be
       unique among all Unix systems in existence.  This normally resembles the Internet address for the  local  machine,  as  returned  by  gethostby-
       name(3), and thus usually never needs to be set.

       The sethostid() call is restricted to the superuser.

RETURN VALUE
       gethostid() returns the 32-bit identifier for the current host as set by sethostid().

       On success, sethostid() returns 0; on error, -1 is returned, and errno is set to indicate the error.
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
int main()
{
	int result;
	if(-1==(result=gethostid()))
	{ 
		printf("gethostid err\n");
		exit(0);
	}
	printf("hosid is :%d\n",result);
	
	return 0;
}

运行结果:

dg@localhost My_project]$ 
[zdg@localhost My_project]$ ./getusid
hosid is :8323328
[zdg@localhost My_project]$ 

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值