Posted: . At: 9:49 AM. This was 3 years ago. Post ID: 14938
Page permalink. WordPress uses cookies, or tiny pieces of information stored on your computer, to verify who you are. There are cookies for logged in users and for commenters.
These cookies expire two weeks after they are set.



Sponsored



A simple program to print information about kernel version and nodename.


This is a very simple program that prints information about the kernel version and nodename. This is very useful when building a larger system information program.

sysinfo.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <unistd.h> /* for _syscallX macros/related stuff */
#include <syscall.h>
#include <sys/utsname.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
 
int main(int argc, char **argv)
{
	struct utsname uname_pointer;
	printf("\n-Kernel information. \n");
 
	uname(&uname_pointer);
	if (strlen(uname_pointer.__domainname) < 7 ||
	    strncmp(uname_pointer.__domainname, "(none)", 10) == 0) {
		printf("--System name - %s \n"		\
			"--Nodename    - %s \n"		\
			"--Release     - %s \n"		\
			"--Version     - %s \n"		\
			"--Machine     - %s \n\n",
			uname_pointer.sysname,
			uname_pointer.nodename,
			uname_pointer.release,
			uname_pointer.version,
			uname_pointer.machine
			);
	} else {
		printf("--System name - %s \n"		\
			"--Nodename    - %s \n"		\
			"--Release     - %s \n"		\
			"--Version     - %s \n"		\
			"--Machine     - %s \n"		\
			"--Domain Name - %s \n\n",
			uname_pointer.sysname,
			uname_pointer.nodename,
			uname_pointer.release,
			uname_pointer.version,
			uname_pointer.machine,
			uname_pointer.__domainname
		);
	}
	return 0;
}

This is how to use the built-in functionality of Linux to get system information easily.

┌──[jason@192.168.1.2][~/Documents]
└──╼  ╼ $ ./a.out 
 
-Kernel information. 
--System name - Linux 
--Nodename    - jason-desktop 
--Release     - 5.8.0-41-generic 
--Version     - #46~20.04.1-Ubuntu SMP Mon Jan 18 17:52:23 UTC 2021 
--Machine     - x86_64

The above example shows the output of this program. This is very simple.

This is another way of getting your IP address.

┌──[jason@192.168.1.2][~/Documents]
└──╼  ╼ $ hostname -I | awk 'NR==1{print $1}'
192.168.1.2

This is also very simple.


Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.