Linux exercises (obtain host information)

Source: Internet
Author: User
#include <sys/utsname.h>#include <unistd.h>#include <stdio.h>int main(){        char computer[256];        struct utsname uts;        if(gethostname(computer,256)!=0 || uname(&uts)<0)        {                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);        return 0;}

Function prototype:

# Include <unistd. h>

Int gethostname (char * Name, size_t namelen );

Write the Network Name of the machine to name. The string must be at least as long as namelen. 0 is returned. Otherwise,-1 is returned.

 

# Include <sys/utsname. h>

Int uname (struct utsname * Name );

Write host data to a struct directed by name.

Utsname struct data element:

Char sysname []; operating system name

Char nodename []; host name

Char release []; Release level of the system

Char version []; System Version Number

Char Machine []; hardware type

A non-negative integer is returned for success.-1 is returned for failure.

 

Note:

License issues:

# Include <unistd. h>

Long gethostid (void); returns a unique value for the host to ensure the software license.

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.