How to run and read the results of command prompt/telnet from a processing sketch?

I’d like to run commands on cmd.exe from a processing sketch, reading the results. Is this possible, and is there a most simple example I can take a look at (such as executing dir and then reading the results to an array)?

1 Like

I’ve got this class Command from “Command.java”: :coffee:

And this test-drive “.pde” sketch: :racing_car:

1 Like

The command library seems like just what I was looking for, thanks. I’ll give it a try and come back with any questions or issues I have.

import deadpixel.command.Command;

static final String BASH = platform == WINDOWS? "cmd /C " : "bash -c ";

static final String CD = "cd ";
static final String AMP = " && ", SPC = " ";

void setup() {
  Command cmd2 = new Command(BASH + "CD ../" + ENTER + "dir");
  //println(cmd2.command, ENTER); <-- this gives "cmd2.command" not visible error, both in the original unedited script and this stripped down one. Not sure what it is supposed to print.
  cmd2.run();
  //println("Success:", cmd.run(), ENTER);
  printArray(cmd2.getOutput());

  exit();
}

The output of this is:

“COMMAND ERROR: The system cannot find the path specified.”

If I just try running

Command cmd = new Command(BASH + “dir”);

It will printArray the results of the processing 3.5.3 directory.

How do I run a command, wait for a response, and then run another command? In my use case scenerio, I would need to wait for a server response, and depending on what the response is, enter another command. The server will reject the command if it isn’t in the same console session.

Which means does that server uses to communicate a response?
An exit ERRORLEVEL? ss64.com/nt/errorlevel.html
Logs to STDERR? ss64.com/nt/syntax-redirection.html

Much probably you’re gonna need to write some “.bat” file in order to read the response and decide whether to issue another command while keeping the same session: ss64.com/nt/if.html

I wonder if you should consider using some HTTP library instead?

I posted my attempt with sockets here, although I wasn’t able to get a connection to the server (in this case Google’s MX server on port 25).

I may have to look into executing telnet commands with a bat script, but I wish I could do it from within a processing sketch if possible.