1

in scala I run

val result = Process("kinit user@SOMETHING.COM")

which then expects an input

Password for user@SOMETHING.COM:

is there a way for me to enter it from code?

Jas
  • 14,493
  • 27
  • 97
  • 148

1 Answers1

2

In order to work with input and output from a process you need to open it so that you can get the STDIN and STDOUT streams. A good example can be found in the questionJava Process with Input/Output Stream. This is still pretty painful, if you are trying to automate some code that requires input on prompts.

In unix this is traditionally handled with the Expect library, and fortunately, there is a java port of it on github as Expect4J. Using Expect4J, you still start the process the same way and get it's input and output streams, but then feed those to Expect4J and program your interaction from there:

val expect = new Expect4j(process.getInputStream(), process.getOutputStream())
expect.expect("Password for user@SOMETHING.COM:")
expect.send(password)
Community
  • 1
  • 1
Arne Claassen
  • 14,088
  • 5
  • 67
  • 106
  • Not to nitpick but if you start the process the same way you get a "value getInputStream is not a member of scala.sys.process.ProcessBuilder" – Luis Sisamon Mar 11 '21 at 04:38