Thread: how do i ping some IPs through "system" function?

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    2

    how do i ping some IPs through "system" function?

    hello everyone!
    i need help and i hope someone would know the answer to my q.
    what i want to do is to ping a list of IPs stored in a vector or a file (it doesn't matter) using system and for each ip i need to store the output of the "ping" command.
    how do i use the function if the ip is variable?

    i need something like this:

    system("ping -c 1" + host[i]+ ">>output.txt") //host[i] is an ip from the vector

    witch is the correct syntax?

    thanks in advance...

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    > which is the correct syntax?
    Certainly not that. This isn't Java :-)

    After you've read a good C tutorial or book (like the one on this website) you'll find the following useful:
    * popen() / pclose() (speaks for itself) http://www.opengroup.org/onlinepubs/...xsh/popen.html
    * fopen() / fgets() / fclose() -- parsing each line with sscanf() or something ... being very careful to avoid buffer overflows. You could build the "command" with strcat() and/or sprintf().

  3. #3
    Registered User bboozzoo's Avatar
    Join Date
    Jan 2009
    Posts
    14
    my dear, why not just use shell script instead?

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    2

    Smile i fugure it out

    thank you both for your suggestions.

    RE: i can't use shell scripting because i must doi it in c (it's just i small part of a more complex project i have to make for school. ).

    i found that i can build my string outside the "system" function and than use it in "system".
    it's something like this:

    Code:
    char file[20]=" >>output_file.txt";
    for (i=1;i<=n;i++)
      {char string[200]="ping c -1 ";
        strcat(string,ip[i]);
        strcat(string,file);
       system(string);}
    // ip[i] = an element from my vector of IPs
    the output of each ping it's appended to output_file.txt

    this is just an example....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 07:18 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM