Home > Enterprise >  Windows ping in C with individual IP address input
Windows ping in C with individual IP address input

Time:11-28

I'm still relatively new to programming and have decided to create an emergency tool in C as a project for general problems in Windows. In addition I would like to create a menu with different problems, which should be selectable.

Problem one would be e.g. that a server/client cannot be reached. Then a ping and a tracert should be triggered in CMD. But my challenge is that I can't get an individual IP address with every query to be entered. And the result should also displayed. Does somebody has any idea?

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define buffer[BUFFER_SIZE] = { 0 };

int main()
{
    int selection1;

    printf("What is the problem? Type in the appropriate number and press Enter: \n");

    printf("1) Something is unavailable.\n");
    printf("2) Problem 2\n");
    printf("3) Problem 3\n");
    printf("4) Problem 4\n");
    printf("5) Problem 5\n");
    printf("6) Problem 6\n");
    printf("7) Problem 7\n");
    fflush(stdout);
    scanf("%d", &selection1);

    if (selection1 == 1)
    {
 
        fflush(stdout);
        char* pingAdress;
        scanf("%c", &pingAdress)
        system( "ping %c", pingAdress)
        
    }

CodePudding user response:

Here's an example of how to do your ping use case.

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main()
{
    int selection1;

    printf("What is the problem? Type in the appropriate number and press Enter: \n");

    printf("1) Something is unavailable.\n");
    fflush(stdout);
    scanf("%d", &selection1);

    if (selection1 == 1)
    {
 
        fflush(stdout);
        char pingAdr[100], pingCmd[100];
        printf("Enter host address: ");
        scanf("%s", pingAdr);
        sprintf(pingCmd, "ping -c 5 %s", pingAdr);
        system(pingCmd);
    }
    return 0;
}
  • Related