-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservers.c
More file actions
98 lines (86 loc) · 2.23 KB
/
servers.c
File metadata and controls
98 lines (86 loc) · 2.23 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* SERVERS[] = {
"192.168.1.1:router",
"192.168.1.101:nexus",
"192.168.1.110:fakeserver",
NULL
};
int arraySize(char* array[])
{
int i = 0;
int res = 0;
while ( array[i] != NULL)
{
i++;
res++;
}
return res;
}
/* - replaced by strtok() - no need to roll my own function when there is one in stdlib
char** chopString(char* s, char delim)
{
int i;
char* res[2];
res[0] = malloc(strlen(s) + sizeof(char));
res[1] = malloc(strlen(s) + sizeof(char));
// split the first part
for (i=0; i<strlen(s); i++)
{
if (s[i] == delim)
{
break;
}
res[0][i] = s[i];
}
res[0][i++] = '\0';
// split the second part
int j = 0;
for (i; i<strlen(s); i++)
{
res[1][j] = s[i];
j++;
}
res[1][i++] = '\0'; // the almighty null-terminator
return res;
}
*/
int main()
{
int serverCount = arraySize(SERVERS);
int i;
for (i=0; i<serverCount; i++)
{
// split up string into two parts
/* deprecated by strtok()
char* ADDRESS = chopString(SERVERS[i], ':')[0];
char* HOSTNAME = chopString(SERVERS[i], ':')[1];
*/
// vars to hold stuff
char* ADDRESS;
char* HOSTNAME;
char* STATUS;
char* COLOR;
// use strtok() to split our string, after allocating some ram for the line of text
char* serverLine = calloc(strlen(SERVERS[i]) + i, sizeof(char));
strcpy(serverLine, SERVERS[i]); // copy SERVERS[i] into opur fresh block of memory
ADDRESS = strtok(serverLine, ":");
HOSTNAME = strtok(NULL, ":");
// set up the ping command
char CMD[50];
sprintf(CMD, "ping %s -c 1 -W 1 &>/dev/null", ADDRESS);
// do it and set status vars
if (!system(CMD))
{
STATUS = "ONLINE"; COLOR = "#00ff00";
}
else
{
STATUS = "OFFLINE"; COLOR = "#ff0000";
}
// print the result
printf("${color0}%s (%s)${alignr}${color %s}%s\n", HOSTNAME, ADDRESS, COLOR, STATUS);
}
return 0;
}