-2

I am trying to write a program in C language using the nano ceditor in linux(ubuntu) in order to ping many devices at the same time, the goal is to retrieve roundtime information about the ping. The program looks like this :

#include <stdio.h>

main()
{
   int i;
   for (i=0; i<9; i++)
      printf("pinging device number:%d",i);
        ping6  fe80::acbd:ff:fe00:i%nstack -c 2
                printf(" \n Done");

}

With ping it works normally but im using ipv6 and it doesnt work. Note: if i do the ping6 command alone it works its just when im putting it in my program Thank you

Pang
  • 9,564
  • 146
  • 81
  • 122
Fred
  • 1
  • 1
  • 1
    what? does your code compile? O.o – anukul Apr 20 '16 at 14:48
  • "With ping it works normally but..." what did the ping code look like? Standard C and C++, or even generally available Linux headers I know of don't have ping or ping6 C library functions – Fire Lancer Apr 20 '16 at 14:58
  • Have you considered building a string and using that as the argument for `system()`? – Weather Vane Apr 20 '16 at 14:59
  • If he wants to do "the goal is to retrieve roundtime information about the ping" I don't think system is enough, he would want the output? popen perhaps if not using additional libraries – Fire Lancer Apr 20 '16 at 15:04
  • may be better off writing a shell script? – yano Apr 20 '16 at 15:21
  • the posted code fails to compile!! so your first consideration is fixing the compile problems. then post the updated code (as additional text). Suggest using the following to compile: `gcc -c -Wall -Wextra -pedantic -Wconversion -std=gnu99 myfile.c -o myfile.o` then you will see such messages as: `main()` needs a return type of `int`, unknown type name: `ping6`, Suggest using the function: `system()` to run the `ping6` command in a shell. – user3629249 Apr 20 '16 at 15:32
  • Note: the `for()` code block only includes the first call to `printf()` you probably want to use braces '{' and '}' so the call to ping6 is also repeated – user3629249 Apr 20 '16 at 15:34
  • Please use consistent indenting. indenting has no effect on the compile/running of the program. However, it makes it massively easier for humans (you and I) to read/understand – user3629249 Apr 20 '16 at 15:34
  • you might want to read the man page for `ping6` especially the statement: *ping6 is IPv6 version of ping, and can also send Node Information Queries (RFC4620). Intermediate hops may not be allowed, because IPv6 source routing was deprecated (RFC5095).* – user3629249 Apr 20 '16 at 15:38
  • when using `system()`, the output can be re-directed to a file and the code then open/read that file to get the output. – user3629249 Apr 20 '16 at 15:39
  • the think the `ipv6` addressing does not support an address that contains: `i%nstack` – user3629249 Apr 20 '16 at 15:42

1 Answers1

2

the posted code contains several problems:

Most of those problems are covered in the question comments.

The following code compiles cleanly and performs the desired function

#include <stdio.h>  // printf()
#include <stdlib.h> // system()

int main( void )
{
    int i;

    for (i=0; i<9; i++)
    {
        printf("pinging device number:%d\n",i);
        system( "ping6  fe80::acbd:ff:fe00:i%nstack -c 2" );
    }
    printf(" \n Done\n");
}

the output from the above code is:

pinging device number:0
unknown host
pinging device number:1
unknown host
pinging device number:2
unknown host
pinging device number:3
unknown host
pinging device number:4
unknown host
pinging device number:5
unknown host
pinging device number:6
unknown host
pinging device number:7
unknown host
pinging device number:8
unknown host

 Done

You may be able to reach a valid host from your network.

However, remember the excerpt from the man page about not being able to perform routing.

(edit) the following code cleanly compiles and uses sprintf()

However, I find nothing that supports the 6th address :1%nstack parameter! I would expect to only see the device number, not the text: %nstack what am I missing?

#include <stdio.h>  // printf(), sprintf()
#include <stdlib.h> // system()

int main( void )
{
    int i;

    char pingCmd[100] = {'\0'};

    for (i=0; i<9; i++)
    {
        printf("\npinging device number:%d\n",i);
        //system( "ping6  fe80::acbd:ff:fe00:i%nstack -c 2" );
        sprintf( pingCmd, "%s%d%s", "ping6  -c 2 fe80::acbd:ff:fe00:", i, "%nstack");
        printf( "%s\n", pingCmd);
        system( pingCmd );
    }
    printf(" \n Done\n");
}

the output from the above is:

pinging device number:0
ping6  -c 2 fe80::acbd:ff:fe00:0%nstack
unknown host

pinging device number:1
ping6  -c 2 fe80::acbd:ff:fe00:1%nstack
unknown host

pinging device number:2
ping6  -c 2 fe80::acbd:ff:fe00:2%nstack
unknown host

pinging device number:3
ping6  -c 2 fe80::acbd:ff:fe00:3%nstack
unknown host

pinging device number:4
ping6  -c 2 fe80::acbd:ff:fe00:4%nstack
unknown host

pinging device number:5
ping6  -c 2 fe80::acbd:ff:fe00:5%nstack
unknown host

pinging device number:6
ping6  -c 2 fe80::acbd:ff:fe00:6%nstack
unknown host

pinging device number:7
ping6  -c 2 fe80::acbd:ff:fe00:7%nstack
unknown host

pinging device number:8
ping6  -c 2 fe80::acbd:ff:fe00:8%nstack
unknown host

 Done
user3629249
  • 16,402
  • 1
  • 16
  • 17
  • if you modify the above code to use something like `sprintf()` to create the desired string for the ipv6 address, (along with the rest of the string to pass to `system()`) then it might actually be able to find some host. – user3629249 Apr 20 '16 at 15:57
  • THank you for your comment about the system() , actually what improved is that now ping6 works but the problem is he s giving unknown host because of the i integer which he is not understanding as part of the command line . I will explqin myself when I ping exemple system( "ping6 fe80::acbd:ff:fe00:2%nstack -c 2" ) it works but when i replace 2 by i it doesnt. i think its a simple concept I am missing could you help ? – Fred Apr 22 '16 at 09:17