1

I'm writing a program to ping an IP. I must ping a specific address for 1000s. Now I want save the TTL to a file to draw it's histogram. How I can do it? How can I just save the TTL to file?

Here's what I've tried:

#include "stdafx.h"
#include "iostream"
#include "string.h" 
#include "stdlib.h"
#include "conio.h"
#include <string>
#include <windows.h>
#include <iostream>
#include <fstream>   

using namespace std;
static string host;
static string ping_again;

void ping()
{   
  cout << "Host: ";
  host="www.yahoo.com";
  system (("ping " + host).c_str);
}

int main()
{
  ping(); 
  return(0);
}
idanshmu
  • 5,061
  • 6
  • 46
  • 92
Sajjad Rostami
  • 303
  • 2
  • 3
  • 12

1 Answers1

0

You've chosen to run the ping executable directly with system() and what you are missing is the capture of the output of the command, so you can parse the TTL.

Capturing stdout could have been done with popen instead of system, however, from your list of includes you seem to be on Windows, and that’s where it gets complicated. Check out this question: What is the equivalent to Posix popen() in the Win32 API?, and note that you can call popen if you application is a console application, otherwise the answers will refer you to an MSDN example which painfully details how you should invoke a process and redirect its input/output.

Once you redirect and capture the output you should parse the strings to extract the TTL. Note that the output of ping.exe may be different for different versions of Windows, and that you have little control what ping.exe you are actually invoking.

The alternative and the better approach is to use the ICMP APIs directly instead of invoking a ping executable on hosts where your application runs. Start with IcmpSendEcho and note that it provides the RTT in the reply structure.

Community
  • 1
  • 1
mockinterface
  • 14,452
  • 5
  • 28
  • 49