-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcheck-ip.c
141 lines (121 loc) · 4.1 KB
/
check-ip.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
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <locale.h>
#include <time.h>
#define DATA_FILE "data.txt"
#define OR ||
#define AND &&
#define USAGE printf("Usage: %s [OPTION] IP_ADRESSES_TO_CHECK\n", argv[0]); \
printf("Options:\n-s Save data to data.txt\n-h, --help Get this help message\n"); \
printf("\nExample: %s -s 8.8.8.8 github.com\n", argv[0]);
void check(char *argv[], int SAVE_DATA, int a, int argc){
// Start
printf("[INFO] Checking: ");
for (int i = a; i < argc; i++){
printf("%s ", argv[i]);
}
printf("\n\n");
for (int i = a; i < argc; i++){
#ifdef _WIN32
char command[100] = "ping ";
strcat(command, argv[i]);
strcat(command, " >");
strcat(command, ".temp__null__file");
int response = system(command);
remove(".temp__null__file");
#else
#include <sys/wait.h>
char command[100] = "ping -c 1 ";
strcat(command, argv[i]);
strcat(command, " >/dev/null 2>&1");
int response = WEXITSTATUS(system(command));
#endif
switch(response){
// Alive
case 0:
printf("[INFO] ---> %s is alive\n", argv[i]);
if (SAVE_DATA == 1){
FILE *fp;
fp = fopen(DATA_FILE, "a+");
time_t t = time(NULL);
struct tm *tm = localtime(&t);
fprintf(fp, "%s ---> '%s' was alive\n\n", asctime(tm), argv[i]);
fclose(fp);
}
break;
// Unreachable
case 1:
case 256:
printf("[INFO] ---> %s is unreachable\n", argv[i]);
if (SAVE_DATA == 1){
FILE *fp;
fp = fopen(DATA_FILE, "a+");
time_t t = time(NULL);
struct tm *tm = localtime(&t);
fprintf(fp, "%s ---> '%s' was unreachable\n\n", asctime(tm), argv[i]);
fclose(fp);
}
break;
// Unknown host
case 2:
case 68:
printf("[INFO] ---> %s unknown host\n", argv[i]);
if (SAVE_DATA == 1){
FILE *fp;
fp = fopen(DATA_FILE, "a+");
time_t t = time(NULL);
struct tm *tm = localtime(&t);
fprintf(fp, "%s ---> '%s' unknown host\n\n", asctime(tm), argv[i]);
fclose(fp);
}
break;
// Unknown code
default:
printf("[INFO] ---> %s is probably inactive\n", argv[i]);
if (SAVE_DATA == 1){
FILE *fp;
fp = fopen(DATA_FILE, "a+");
time_t t = time(NULL);
struct tm *tm = localtime(&t);
fprintf(fp, "%s ---> '%s' was probably inactive\n\n", asctime(tm), argv[i]);
fclose(fp);
}
break;
}
}
if (SAVE_DATA == 1){
printf("\n[INFO] This have been saved to '%s'.\n", DATA_FILE);
}
}
int main(int argc, char *argv[]){
// Check command line arguments
if (argc == 1){
USAGE
return 1;
}
else if (strcmp("-h", argv[1]) == 0 OR strcmp("--help", argv[1]) == 0){
USAGE
return 0;
}
else if (strcmp("-s", argv[1]) == 0 AND argc == 2){
USAGE
return 1;
}
else if (argc > 1 AND strcmp("-s", argv[1]) == 0){
int a = 2;
int SAVE_DATA = 1; // 1 is true, 0 is false
check(argv, SAVE_DATA, a, argc);
}
else if (argc > 1 AND strcmp("-s", argv[1]) != 0){
int a = 1;
int SAVE_DATA = 0;
check(argv, SAVE_DATA, a, argc);
}
else {
USAGE
return 1;
}
return 0;
}