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

**URL:** https://discourse.processing.org/t/how-to-run-and-read-the-results-of-command-prompt-telnet-from-a-processing-sketch/11571
**Category:** Coding Questions
**Created:** [May 25, 2019, 1:16am UTC](https://discourse.processing.org/t/how-to-run-and-read-the-results-of-command-prompt-telnet-from-a-processing-sketch/11571 "2019-05-25T01:16:18Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Birb](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/birb/32/3280_2.png) [@Birb](https://discourse.processing.org/u/Birb)
#### Post date: [May 25, 2019, 1:16am UTC](https://discourse.processing.org/t/how-to-run-and-read-the-results-of-command-prompt-telnet-from-a-processing-sketch/11571/1 "2019-05-25T01:16:18Z")

</div>

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)?

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [May 25, 2019, 8:46am UTC](https://discourse.processing.org/t/how-to-run-and-read-the-results-of-command-prompt-telnet-from-a-processing-sketch/11571/2 "2019-05-25T08:46:51Z")

</div>

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

> <https://github.com/GoToLoop/command/blob/patch-1/src/deadpixel/command/Command.java>

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

- [Forum.Processing.org/two/discussion/23471/combining-two-pieces-of-code/p2#Item\_51](http://Forum.Processing.org/two/discussion/23471/combining-two-pieces-of-code/p2#Item_51)

---

<div class="post-metadata">

### Author: ![Birb](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/birb/32/3280_2.png) [@Birb](https://discourse.processing.org/u/Birb)
#### Post date: [May 26, 2019, 6:30am UTC](https://discourse.processing.org/t/how-to-run-and-read-the-results-of-command-prompt-telnet-from-a-processing-sketch/11571/3 "2019-05-26T06:30:27Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Birb](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/birb/32/3280_2.png) [@Birb](https://discourse.processing.org/u/Birb)
#### Post date: [May 26, 2019, 7:13am UTC](https://discourse.processing.org/t/how-to-run-and-read-the-results-of-command-prompt-telnet-from-a-processing-sketch/11571/4 "2019-05-26T07:13:14Z")

</div>

```auto
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.

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [May 26, 2019, 10:27pm UTC](https://discourse.processing.org/t/how-to-run-and-read-the-results-of-command-prompt-telnet-from-a-processing-sketch/11571/5 "2019-05-26T22:27:00Z")

</div>

> [@Birb](#):
>
> , I would need to wait for a server response, and depending on what the response is, enter another command.

Which means does that server uses to communicate a response?  
An exit ERRORLEVEL? [Errorlevel - Windows CMD - SS64.com](http://ss64.com/nt/errorlevel.html)  
Logs to STDERR? [Command Redirection, Pipes - Windows CMD - SS64.com](http://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: [If - Conditionally perform command - Windows CMD - SS64.com](http://ss64.com/nt/if.html)

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

> **[GitHub - runemadsen/HTTP-Requests-for-Processing](https://github.com/runemadsen/HTTP-Requests-for-Processing)**
>
> Contribute to runemadsen/HTTP-Requests-for-Processing development by creating an account on GitHub.

---

<div class="post-metadata">

### Author: ![Birb](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/birb/32/3280_2.png) [@Birb](https://discourse.processing.org/u/Birb)
#### Post date: [May 26, 2019, 10:31pm UTC](https://discourse.processing.org/t/how-to-run-and-read-the-results-of-command-prompt-telnet-from-a-processing-sketch/11571/6 "2019-05-26T22:31:14Z")

</div>

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).

> [@TCP mail client session in Processing not working](https://discourse.processing.org/t/tcp-mail-client-session-in-processing-not-working/11618):
>
> I’m trying to open a TCP socket with a mail server and then send/read commands, but it isn’t working. After running for several minutes with no response I get the IOException catch error “Couldn’t get I/O for the connection to: hostname”. import java.io.\*; import java.net.\*; void setup() { size(200,200); Socket smtpSocket = null; DataOutputStream os = null; // DataInputStream is = null; BufferedReader br = null; try { smtpSocket = new Socket("64.233.177.26", 25); os = new…

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.
