Search code examples
javaubuntuterminalprocessbuilderarp

Process Builder Arguments


final String commands[] =  {"arp", "-n", "|" ,"grep", "98:5d:ad:3d:36:ef", "|", "awk '", "{print $1}", "'"};
ProcessBuilder pb = new ProcessBuilder(commands); 

I would like to retrieve the IP, given the MAC ADDRESS.
When I insert this command to the terminal (ubuntu 16.04) it works.
But it doesn't work when I use it in JAVA.

What am i doing wrong?

It only works when I run it like this:

final String commands[] =  {"arp", "-n"};
ProcessBuilder pb = new ProcessBuilder(commands); 

Solution

  • You need to invoke "sh" and pass to that program your piped command. Try:

    ProcessBuilder b = new ProcessBuilder( "/bin/sh", "-c",
                   "arp -n | grep 98:5d:ad:3d:36:ef | awk '{print $1}'" );