Getting host information under Linux

Source: Internet
Author: User
Tags aliases

The program originates from the Linux programming book, which is used by individuals to learn from this book as a knowledge point.

Here we introduce several structures:

1234567 structhostent{char* h_name;//主机名称char* h_aliases;//主机别名short h_addrtype;//主机地址类型short h_length;//主机地址长度char**h_addr_list;//主机地址列表}

Where the host address type h_addrtype is af_inet; The address list is a string and varies in length.

123456 structservent {    char *s_name;       /* 服务名 */    char    **s_aliases;    /* 服务别名列表 */    int s_port;       /* 端口号 */    char*s_proto;      /* 使用的协议 */};

Here's some work, just insert the code:

1 #include<unistd.h> 2 #include<netinet/in.h> 3 #include<arpa/inet.h> 4 #include<netdb.h> 5 #include<stdio.h> 6 7 int main( int argc, char *argv[]){ 8     char *host, **names,**addrs; 9     struct hostent *hostinfo; 10 11     if (argc==1){ //若没有主机名陈,则获取本机信息 12         char myname[256]; 13         gethostname(myname,255); 14         host = myname; 15     } 16     else 17         host = argv[1]; 18 19     hostinfo = gethostbyname(host);//获取主机信息 20     if (!hostinfo){ 21         fprintf (stderr, "cannot get info host:%s\n" ,host);                   22         exit (1); 23     } 24 25     printf ( "resluts for host %s:\n" ,host); 26     printf ( "Name:%s\n" ,hostinfo -> h_name); //主机名 27     printf ( "Aliases:" ); 28     names = hostinfo -> h_aliases; //主机别名 29     while (*names){//输出全部主机别名 30         printf ( " %s" ,*names); 31         names++; 32     } 33 34     printf ( "\n" ); 35 36     if (hostinfo -> h_addrtype !=AF_INET){//主机地址类型判断 37         fprintf (stderr, "not an IP host!\n" ); 38         exit (1); 39     } 40 41     addrs = hostinfo -> h_addr_list; 42     while (*addrs){//转换主机地址格式 43         printf ( " %s" ,inet_ntoa(*( struct in_addr *)*addrs)); 44         addrs++; 45     } 46     printf ( "\n" ); 47     exit (0); 48 }

Program running results: Linux under the photo album is not good to pass the picture, here directly to the text.

./getname resluts for host farbeyond-aspire-4741:

name:farbeyond-aspire-4741

aliases:127.0.1.1

./getname baidu.com resluts for host baidu.com:

Name:baidu.com

Aliases:

220.181.57.217 220.181.57.216 123.125.114.144

Above these three IP address, everybody can use to visit Baidu. Google also has more than 10 IP in use, depending on the size of the company and different.

This is the client to query the server side, of course, the server side can also view the visitor's source IP, which provides the means to collect user information.

Getting host information under Linux

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.