Executing an interactive CLI program remotely

350 Views Asked by At

I wrote a small client/server application that can execute system commands remotely. It works fine with non interactive programs when you only need to read their output. However, is there a reliable way to send user input to the Process started IF it requires it? How do I even find out whether a process prompts for any input?

1

There are 1 best solutions below

4
On

Take a look at 'expectj'. This is a port of http://en.wikipedia.org/wiki/Expect

http://expectj.sourceforge.net/

Example:

// Create a new ExpectJ object with a timeout of 5s
ExpectJ expectinator = new ExpectJ(5);

// Fork the process
Spawn shell = expectinator.spawn("/bin/sh");

// Talk to it
shell.send("echo Chunder\n");
shell.expect("Chunder");
shell.send("exit\n");
shell.expectClose();