-2

I need to get information about internet package and I was trying with the following code but I don't have experience with C++.

I executed this code from this tutorial http://yuba.stanford.edu/~casado/pcap/section1.html

#include <stdio.h>
#include <stdlib.h>
#include <pcap.h>  /* GIMME a libpcap plz! */
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

int main(int argc, char **argv)
{
  char *dev; /* name of the device to use */ 
  char *net; /* dot notation of the network address */
  char *mask;/* dot notation of the network mask    */
  int ret;   /* return code */
  char errbuf[PCAP_ERRBUF_SIZE];
  bpf_u_int32 netp; /* ip          */
  bpf_u_int32 maskp;/* subnet mask */
  struct in_addr addr;

  /* ask pcap to find a valid device for use to sniff on */
  dev = pcap_lookupdev(errbuf);

  /* error checking */
  if(dev == NULL)
  {
   printf("%s\n",errbuf);
   exit(1);
  }

  /* print out device name */
  printf("DEV: %s\n",dev);

  /* ask pcap for the network address and mask of the device */
  ret = pcap_lookupnet(dev,&netp,&maskp,errbuf);

  if(ret == -1)
  {
   printf("%s\n",errbuf);
   exit(1);
  }

  /* get the network address in a human readable form */
  addr.s_addr = netp;
  net = inet_ntoa(addr);

  if(net == NULL)/* thanks Scott :-P */
  {
    perror("inet_ntoa");
    exit(1);
  }

  printf("NET: %s\n",net);

  /* do the same as above for the device's mask */
  addr.s_addr = maskp;
  mask = inet_ntoa(addr);

  if(mask == NULL)
  {
    perror("inet_ntoa");
    exit(1);
  }

  printf("MASK: %s\n",mask);

  return 0;
}

When I execute this code, I get a file a.out but I cannot open the file, why? How can i pass my information to .csv?

hubman
  • 149
  • 1
  • 15
  • `.out` or `a.out`? – stark Apr 11 '20 at 19:12
  • @stark yeah a.out – hubman Apr 11 '20 at 21:44
  • 1
    @hubman: What do you mean by _information about internet package_? This is C code, not C++. `a.out` is an executable. You need to run it on terminal to see its output i.e. `./a.out`. You'll see its output as mentioned in that tutorial. Explain this: _How can i pass my information to .csv?_ What _information_? What `.csv`? Please update these details in your question. – Azeem Apr 15 '20 at 05:42

2 Answers2

3

I don't have experience with C++

Although the code may compile using a C++ compiler it looks more like C code. The name of the file, ldev.c, and the compiler used also says that it's a C program.

When I execute this code, I get a file a.out but I cannot open the file, why?

The last step in the tutorial tells you how to compile the program, not how to execute it:

gcc ldev.c -lpcap

That step produces a.out which is the program you are supposed to execute.

To actually execute the program, run the command:

./a.out

Then, if everything works, the output should be something similar to:

DEV: eth0
NET: 192.168.12.0
MASK: 255.255.255.0
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
  • 1
    yes it works, I wanted to approve your answer but there was a lack of time and then I forgot sorry – hubman May 21 '21 at 02:18
0

Writing to a .csv file is not very different from the basic writing to any other file. We need to open the file using the library

#include <fstream>

Then we create our file to write to

std::ofstream outputFile;

we open it using

outputFile.open("random.csv");

and start writing using commas as separators

outputFile << "a,b,c,\n";
outputFile << mask << "\n"; // in your case

Then don't forget to close it!

outputFile.close();
vikAy
  • 314
  • 6
  • 15
  • The following code gave me this output: a,b,c, Mind that some compilers treat ';' as a separator for a row not the comma – vikAy Apr 11 '20 at 22:40
  • i cannot, please you post your all code please, i am new in c++ – hubman Apr 11 '20 at 22:55
  • @hubman `#include #include int main() { std::string DEV = "eth0"; std::string NET = "192.168.12.0"; std::string MASK = "255.255.255.0"; std::ofstream output; output.open("random.csv", std::ios::out | std::ios::app); output << DEV << "; " << NET << "; " << MASK << "\n"; output.close(); }` – vikAy Apr 11 '20 at 22:59
  • your code can i put in my code? but where i put it? – hubman Apr 11 '20 at 23:20
  • add the include on new line at the includes section then create the ofstream file open after the declaration of main method, replace printf with output << mask and at the end of the file close the output, good luck – vikAy Apr 12 '20 at 00:33