日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

Linux获取本机hostname函数,Linux下获得主机与域名-gethostbyname和gethostbyaddr

發(fā)布時間:2023/12/3 linux 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Linux获取本机hostname函数,Linux下获得主机与域名-gethostbyname和gethostbyaddr 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

1.數(shù)據(jù)結構hostent和servent:

struct hostent{

char *h_name;/* official domain name of host */

char **h_aliases;/* null-terminated array of domain names */

int h_addrtype;/* host address type (AF_INET) */

int h_length;/* length of an address, in bytes */

char **h_addr_list;/* null-terminated array of in_addr structs */

};

#define h_addr h_addr_list[0]

這里是這個數(shù)據(jù)結構的詳細資料:

char *h_name :表示的是主機的規(guī)范名。例如的規(guī)范名其實是。

char**h_aliases:表示的是主機的別名。就是google他自己的別名。有的時候,有的主機可能有好幾個別名,這些,其實都是為了易于用戶記憶而為自己的網站多取的名字。

inth_addrtype :表示的是主機ip地址的類型,到底是ipv4(AF_INET),還是ipv6(AF_INET6)

inth_length :表示的是主機ip地址的長度

int**h_addr_lisst:表示的是主機的ip地址,注意,這個是以網絡字節(jié)序存儲的。千萬不要直接用printf帶%s參數(shù)來打這個東西,會有問題的哇。所以到真正需要打印出這個IP的話,需要調用inet_ntop()。

結構體servent

typedef struct servent {

char FAR* s_name; //正規(guī)的服務名char FAR FAR** s_aliases;

short s_port; //連接到該服務時需要用到的端口號char FAR* s_proto;

} servent;

2.取得主機名與域名

2.1 gethostname(): 返回本地主機的標準主機名。

原型如下:

#include

int gethostname(char *name, size_t len);

參數(shù)說明:

這個函數(shù)需要兩個參數(shù):

接收緩沖區(qū)name,其長度必須為len字節(jié)或是更長

接收緩沖區(qū)name的最大長度

返回值:

如果函數(shù)成功,則返回0。如果發(fā)生錯誤則返回-1。錯誤號存放在外部變量errno中。

2.2 getdomainname(2)函數(shù):可以允許程序獲得程序正運行的主機的NIS域名。

原型如下:

#include

int getdomainname(char *name,size_t len);

參數(shù)說明:

這個函數(shù)的用法也gethostname相同。

2.3測試getdomainname與gethostname函數(shù)

下面這個程序演示了這兩個函數(shù)的用法。這個程序只是簡單的調用這兩個函數(shù)并報告其結果。

/*gethostn.c

*

* Example of gethostname(2):

*/

#include

#include

#include

#include

#include

int main(int argc,char **argv)

{

int z;

char buf[32];

z = gethostname(buf,sizeof buf);

if(z==-1)

{

fprintf(stderr,"%s:gethostname(2)\n",strerror(errno));

exit(1);

}

printf("host name = '%s'\n",buf);

z = getdomainname(buf,sizeof buf);

if(z==-1)

{

fprintf(stderr,"%s:getdomainname(2)\n",strerror(errno));

exit(1);

}

printf("domain name = '%s'\n",buf);

return 0;

}

這個程序的運行結果如下:

$ ./gethostn

host name = 'tux'

domain name = ''

在了解了如何獲得本地系統(tǒng)的信息以后,現(xiàn)在我們就可以將我們的注意力轉移到解析遠程主機名上了。

3.gethostbyaddr():

struct hostent *gethostbyaddr(const char *name)

這個函數(shù),傳入值是IP地址(注意,這里不是簡單的字符串,需要先將字符串形式的IP地址由inet_aton轉化一下),然后經過函數(shù)處理,將結果由返回值傳出。返回值是一個hostent結構.因為有了hosten這個傳出的結構,我們可以在這個結構里面找到我們想需要的信息。

下面的是例程。編譯后只需在命令行輸入./a.out 202.102.14.141(IP地址)就可以看結果了。

#include

#include

int main(int argc, char **argv)

{

char *ptr,**pptr;

struct hostent *hptr;

char str[32];

char ipaddr[16];

struct in_addr *hipaddr;

/*取得命令后第一個參數(shù),即要解析的IP地址*/

ptr = argv[1];

/*調用inet_aton(),ptr就是以字符串存放的地方的指針,hipaddr是in_addr形式的地址*/

if(!inet_aton(ptr,hipaddr))

{

printf("inet_aton error\n");

return 1;

}

/*調用gethostbyaddr()。調用結果都存在hptr中*/

if( (hptr = gethostbyaddr(hipaddr, 4, AF_INET) ) == NULL )

{

printf("gethostbyaddr error for addr:%s\n", ptr);

return 1; /*如果調用gethostbyaddr發(fā)生錯誤,返回1 */

}

/*將主機的規(guī)范名打出來*/

printf("official hostname:%s\n",hptr->h_name);

/*主機可能有多個別名,將所有別名分別打出來*/

for(pptr = hptr->h_aliases; *pptr != NULL; pptr++)

printf("alias:%s\n",*pptr);

/*根據(jù)地址類型,將地址打出來*/

switch(hptr->h_addrtype)

{

case AF_INET:

case AF_INET6:

pptr=hptr->h_addr_list;

/*將剛才得到的所有地址都打出來。其中調用了inet_ntop()函數(shù)*/

for(;*pptr!=NULL;pptr++)

printf("address:%s\n", inet_ntop(hptr->h_addrtype, *pptr, str, sizeof(str)));

break;

default:

printf("unknown address type\n");

break;

}

return 0;

}

4.gethostbyname():

使用這個東西,首先要包含2個頭文件:

#include

#include

struct hostent *gethostbyname(const char *name);

這個函數(shù)的傳入值是域名或者主機名,例如"","wpc"等等。

傳出值,是一個hostent的結構(如下)。如果函數(shù)調用失敗,將返回NULL。

struct hostent {

char*h_name;

char**h_aliases;

inth_addrtype;

inth_length;

char**h_addr_list;

};(對它的解釋見1)

const char *inet_ntop(int af, const void *src, char *dst, socklen_t cnt):

這個函數(shù),是將類型為af的網絡地址結構src,轉換成主機序的字符串形式,存放在長度為cnt的字符串中。

這個函數(shù),其實就是返回指向dst的一個指針。如果函數(shù)調用錯誤,返回值是NULL。

下面是例程,有詳細的注釋。

#include

#include

int main(int argc, char **argv)

{

char *ptr,**pptr;

struct hostent *hptr;

char str[32];

/*取得命令后第一個參數(shù),即要解析的域名或主機名*/

ptr = argv[1];

/*調用gethostbyname()。調用結果都存在hptr中*/

if( (hptr = gethostbyname(ptr) ) == NULL )

{

printf("gethostbyname error for host:%s\n", ptr);

return 0; /*如果調用gethostbyname發(fā)生錯誤,返回1 */

}

/*將主機的規(guī)范名打出來*/

printf("official hostname:%s\n",hptr->h_name);

/*主機可能有多個別名,將所有別名分別打出來*/

for(pptr = hptr->h_aliases; *pptr != NULL; pptr++)

printf("alias:%s\n",*pptr);

/*根據(jù)地址類型,將地址打出來*/

switch(hptr->h_addrtype)

{

case AF_INET:

case AF_INET6:

pptr=hptr->h_addr_list;

/*將剛才得到的所有地址都打出來。其中調用了inet_ntop()函數(shù)*/

for(;*pptr!=NULL;pptr++)

printf("address:%s\n", inet_ntop(hptr->h_addrtype, *pptr, str, sizeof(str)));

break;

default:

printf("unknown address type\n");

break;

}

return 0;

}

總結

以上是生活随笔為你收集整理的Linux获取本机hostname函数,Linux下获得主机与域名-gethostbyname和gethostbyaddr的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。