0

I am using the ftm cli by Oracle. When I run a command, it asks for a password by saying "Enter a password". I am writing a script which uses this command and so I want to enter the password programatically. Here's what I have tried -

echo "password" | java -jar ftmcli.jar list

But this doesn't work. Any idea what to do to make it work?

Eduard Itrich
  • 836
  • 1
  • 8
  • 20

3 Answers3

4

Without being able to look into the source code of ftmcli, I presume that Oracle is using Console.readPassword() from the java.io.Console class. However, there is a snag with this function:

public final class Console

Methods to access the character-based console device, if any, associated with the current Java virtual machine.

Whether a virtual machine has a console is dependent upon the underlying platform and also upon the manner in which the virtual machine is invoked. If the virtual machine is started from an interactive command line without redirecting the standard input and output streams then its console will exist and will typically be connected to the keyboard and display from which the virtual machine was launched. If the virtual machine is started automatically, for example by a background job scheduler, then it will typically not have a console.

If this virtual machine has a console then it is represented by a unique instance of this class which can be obtained by invoking the System.console() method. If no console device is available then an invocation of that method will return null.

By piping/redirecting the stdout of echo to the JVM, the Java method System.console() will return null and hence not read anything from your redirection.

Look at the following questions which handle the same problem, but on Java side: "How to handle java passwd reading when System.console() returns null?" or How to pipe input to Java program with bash

On your side, there is more or less nothing you can really do. Executing echo $password | java -jar ftmcli.jar list or java -jar ftmcli.jar list <<< $password will always fail as long Oracle doesn't change the way how ftmcli reads the password from stdin.

Eduard Itrich
  • 836
  • 1
  • 8
  • 20
  • One thing you could try is to send the password to `/proc/$(pgrep -f '-jar ftmcli.jar list)/fd/0` (`stdin` of the particular process) after you launched `ftmcli` which is then waiting for input. However, I didn't try this and can't guarantee it will work. – Eduard Itrich Aug 03 '17 at 07:47
  • 1
    Nice answer. Sometimes passwords can be passed by setting environment variables (e.g. `export FTM_PASSWORD=secret`). No idea if that is possible here. – Mark Setchell Aug 03 '17 at 07:57
1

If you are ready for a one time password setup, then follow the below steps.

  1. run java -jar ftmcli.jar --save-auth-key

  2. You will be prompted for teh password and once it is given, the file will be uploaded and a ftmclikeystore file is created under ftmcli folder.

  3. Now as long as if you don't change the user, then ftmcli will take the password from this file.

0

Found this little but fantastic tool called expect does the magic:

$ ./expect.script
spawn java -cp /tmp/brm-test/oraclepki.jar:/tmp/brm-test/BRMActions.jar com.oracle.installer.brm.BRMInstallHelper 7 /tmp/brm-test/client upgC
Enter Password for the wallet:
$

Perhaps it's not very much visible in the snippet above, but it is working:

$ cat /tmp/brm-test/client/.wp
upgC=MyMag1cPa$$word#

What is in the expect.script?

$ cat expect.script
#!/usr/bin/expect

stty -echo

spawn java -cp /tmp/brm-test/oraclepki.jar:/tmp/brm-test/BRMActions.jar com.oracle.installer.brm.BRMInstallHelper 7 /tmp/brm-test/client upgC

expect "Enter Password for the wallet:"
send "MyMag1cPa$$word#\r"

interact

Seems this can be used also in Chef, see this cookbook.

I hope it will help, Jarek

Jarek
  • 782
  • 5
  • 16