Sample C program to ping hosts using fping

Here’s some sample C code which one can integrate into their projects to ping hosts. It uses fping instead of ping to do the actual pinging. Unlike ping, fping is meant to be used in scripts, so its output is designed to be easy to parse.

#define      _GNU_SOURCE
#include <stdio.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdlib.h>

int
ping(char *ipaddr) {
  char *command = NULL;
  FILE *fp;
  int stat = 0;
  asprintf (&command, "%s %s -q 2>&1", "fping", ipaddr);
  fp = popen(command, "r");
  if (fp == NULL) {
    fprintf(stderr, "Failed to execute fping command\n");
    free(command);
    return -1;
  }
  stat = pclose(fp);
  free(command);
  return WEXITSTATUS(stat);
}

/*  Check if an ip address is valid */
int isValidIpAddress(char *ipaddr)
{
    struct sockaddr_in sa;
    int result = inet_pton(AF_INET, ipaddr, &(sa.sin_addr));
    return result != 0;
}


int main(int argc, char **argv) {
  int status = 0;
  if(argc != 2) {
    printf("Example Usage: %s 192.168.1.1\n", argv[0]);
    return 1;
  } else if(!isValidIpAddress(argv[1])) {
    printf("%s is an invalid IP Address\n", argv[1]);
    return 1;
  }
  status = ping(argv[1]);
  if (status) {
    printf("Could ping %s successfully, status %d\n", argv[1], status);
  } else {
    printf("Machine not reachable, status %d\n", status);
  }
  return status;
}

To install fping on ubuntu:

sudo apt-get install fping

1 comment

  1. Just compiled: gcc fping_ret.c -o fping
    And wrong return result:
    $./fping 10.101.10.20
    Machine not reachable, status 0
    (But host is online/reachable)
    $./fping 10.101.10.21
    Could ping 10.101.10.21 successfully, status 1
    (But host is offline)

Leave a Reply

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