I wrote a little program in C based on some requirements:
- Ping every IP from a file and check the result
- Show, by network adapter, ONLY the IP, subnet, default gateway and DNS
- Check the speed between two pings.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LEN 256
void readAndPing(char* path) {
/*
Lee el contenido del fichero indicado por path
y lanza pings a cada una de las direcciones ip del fichero.
Printa si el ping ha sido efectivo o no.
*/
printf("Reading file %s...\n", path);
FILE* fp = fopen(path, "r");
if (fp == NULL) {
printf("File not found!\n");
}
else {
char buffer[MAX_LEN];
while (fgets(buffer, MAX_LEN, fp)) {
buffer[strcspn(buffer, "\n")] = 0;
char comando[MAX_LEN];
snprintf(comando, sizeof(comando), "ping -n 1 %s > NUL", buffer);
printf("Executing command %s\n", comando);
int ping_res = system(comando);
if (ping_res) printf("Ping failed!\n");
else printf("Pinged!\n");
}
fclose(fp);
}
}
void showConfigNetAdpt(char* adptName) {
/*
Muestra por pantalla la configuración de red del adaptador adptNAme
creando un archivo netAdpt.txt y mostrando sus resultados.
*/
char comando[MAX_LEN];
snprintf(comando, sizeof(comando),
"netsh interface ip show config %s > netAdpt.txt", adptName);
printf("Executing command %s\n", comando);
int res = system(comando);
if (res) {
printf("Command failed!\n");
}
else {
//Lectura del fichero creado
FILE* fp = fopen("netAdpt.txt", "r");
if (fp == NULL) {
printf("File not found\n");
}
else {
char buffer[MAX_LEN];
while (fgets(buffer, MAX_LEN, fp)) {
//Elimina el newline
buffer[strcspn(buffer, "\n")] = 0;
char* line;
//Cosas de no tener utf-8...
line = strstr(buffer, "Direcci¢n IP:");
if (line) printf("%s\n", line);
line = strstr(buffer, "Prefijo de subred:");
if (line) printf("%s\n", line);
line = strstr(buffer, "Puerta de enlace predeterminada:");
if (line) printf("%s\n", line);
line = strstr(buffer, "Servidores DNS configurados a trav‚s de DHCP:");
if (line) {
printf("%s\n", line);
fgets(buffer, MAX_LEN, fp);
line = strstr(buffer, "Registrar con el sufijo:");
while (line == NULL) {
printf("\t%s\n", buffer);
fgets(buffer, MAX_LEN, fp);
line = strstr(buffer, "Registrar con el sufijo:");
}
}
}
fclose(fp);
}
}
}
void printResultsPingMean(char* path, char* ip) {
FILE* fp = fopen(path, "r");
printf("Results from %s\n", ip);
char buffer[MAX_LEN];
char* line;
if (fp == NULL) {
printf("File not foudn\n");
}
else {
while (fgets(buffer, MAX_LEN, fp)) {
//Elimina el newline
buffer[strcspn(buffer, "\n")] = 0;
line = strstr(buffer, "Media ");
if (line != NULL) {
printf("\t%s\n", line);
}
}
fclose(fp);
}
}
void fastestDNS(char* ip1, char* ip2) {
char comando[MAX_LEN];
snprintf(comando, sizeof(comando),
"ping -n 5 %s > pingResDNS1.txt", ip1);
printf("Executing command %s...\n", comando);
int e_c1 = system(comando);
if (e_c1) {
printf("Error executing command! Is %s a valid IP? !\n", ip1);
}
snprintf(comando, sizeof(comando),
"ping -n 5 %s > pingResDNS2.txt", ip2);
printf("Executing command %s...\n", comando);
int e_c2 = system(comando);
if (e_c2) {
printf("Error executing command! Is %s a valid IP? !\n", ip2);
}
printResultsPingMean("pingResDNS1.txt", ip1);
printResultsPingMean("pingResDNS2.txt", ip2);
}
void showMenu() {
printf("-------------------------------------------------------\n");
printf("Net Utilities\n");
printf("1: Ping every IP from a file\n");
printf("2: Check net config by adapter\n");
printf("3: Compare ping speed of two IP's\n");
printf("4: Exit\n");
printf("-------------------------------------------------------\n");
}
int readOption() {
showMenu();
int opcion = -1;
scanf("%d", &opcion);
while (opcion < 1 || opcion > 4) {
printf("Invalid option!\n");
showMenu();
scanf("%d", &opcion);
}
return opcion;
}
int main() {
int opcion = -1;
char buffer[MAX_LEN], buffer2[MAX_LEN];
while (opcion != 4) {
opcion = readOption();
printf("Option %d\n", opcion);
if (opcion == 1) {
printf("\n-------------------------------------------------------\n");
printf("Ping every IP from a file\n");
printf("-------------------------------------------------------\n");
printf("Filename: ");
scanf("%s", buffer);
readAndPing(buffer);
}
if (opcion == 2) {
printf("\n-------------------------------------------------------\n");
printf("Check net config by adapter\n");
printf("-------------------------------------------------------\n");
printf("Adapter name: ");
scanf("%s", buffer);
showConfigNetAdpt(buffer);
}
if (opcion == 3) {
printf("\n-------------------------------------------------------\n");
printf("Compare ping speed of two IP's\n");
printf("-------------------------------------------------------\n");
printf("First IP: ");
scanf("%s", buffer);
printf("Second IP: ");
scanf("%s", buffer2);
fastestDNS(buffer, buffer2);
}
printf("\n\n");
}
}
I'm using windows and my default language is spanish, so that's why the strstr
calls are strange... However, I would like some feedback. This is the first time I used windows commands inside C.