Linux获取主机信息

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/utsname.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    struct utsname uts;
    char hn[256];//hostname

    if(uname(&uts)<0)
    {
        perror("get utsname failed");
        exit(EXIT_FAILURE);
    }
    printf("Machine:%s\n",uts.machine);
    printf("System name: %s\n",uts.sysname);
    printf("Version: %s\n",uts.version);
    printf("Release: %s\n",uts.release);
    printf("Node Name: %s\n",uts.nodename);

    if(gethostname(hn,255))
    {
        perror("get host name failed");
        exit(EXIT_FAILURE);
    }
    printf("Host Name: %s\n",hn);
    printf("Host ID: %ld\n",gethostid());

    exit(EXIT_SUCCESS);
}

 

posted @ 2021-02-07 09:38  castor_xu  阅读(111)  评论(0编辑  收藏  举报