Running terminal command from Processing (OSX)

Hey, I was wondering if its possible to execute a terminal command from a processing sketch. I'm currently working with the carnivore library for processing, which is is a network sniffer. Right now I Need to execute the following code in the terminal (OSX) to give carnivore acces to sniff packages.

sudo chmod 777 / dev / bpf *

However I was wondering If I can run that command from the processing sketch? And how?

Answers

  • edited May 2017

    Maybe launch(). Maybe open()... maybe not. Maybe exec(). Yeah, exec() looks good.

    https://processing.org/reference/launch_.html (links to java docs, which has:)

    exec

    public static Process exec(String... args)

    Pass a set of arguments directly to the command line. Uses Java's Runtime.exec() method. This is different from the launch() method, which uses the operating system's launcher to open the files. It's always a good idea to use a full path to the executable here.

    exec("/usr/bin/say", "-v", "Pipe Organ", "welcome to the command line");

    Or if you want to wait until it's completed, something like this:

     Process p = exec("/usr/bin/say", "waiting until done");
     try {
       int result = p.waitFor();
       println("the process returned " + result);
     } catch (InterruptedException e) { }
    

    You can also get the system output and error streams from the Process object, but that's more that we'd like to cover here.

    Returns: a Process object

  • edited May 2017

    Thx for the answer! I dont really have any experience working with the terminal commands. I tried to run the following code:

    exec("/Users/"username/", "sudo chmod 777 / dev / bpf *");

    and

    exec("sudo chmod 777 / dev / bpf *");

    And also:

    Process p = exec("sudo chmod 777 / dev / bpf");
    try {
      int result = p.waitFor();
      println("the process returned " + result);
    } catch (InterruptedException e) { }
    

    But processing returns the following error:

    Could not open sudo chmod 777 / dev / bpf *.

Sign In or Register to comment.