One idea is to launch the process in the background, redirecting its stdout (stderr?) to a file, and have your script wait and monitor that file in a loop. Whenever the process prints the password prompt, the script feeds the password into the process via its stdin.
Achieving this is actually tricky. You can use two scripts, one for monitoring the output and feeding the passwords, and another one for launching them first, applying the necessary redirects.
So the launcher script is:
start-dfs.sh <(./feed-passwords.py "$TEMP_FILE") > "$TEMP_FILE"
For whatever reason, I'll use Python for the other one:
from sys import argv
temp_file = argv # Raises exception if argument is missing
counter = 0
maximum = 3
previous_length = 0
with open(temp_file) as f:
output = f.read()
if len(output) == previous_length:
previous_length = len(output)
last_line = output.strip().split('\n')[-1]
if last_line.endswith(' password:'):
print(PASSWORD) # Our stdout goes to `start-dfs.sh`
counter += 1
if counter == maximum:
I haven't tested any of this. One assumption this approach relies on is that the one who recieves the password via its stdin is
start-dfs.sh itself, which has a pretty good possibility to be untrue. Start scripts often just invoke other processes, so the password likely needs to be sent to the other processes' stdin.
You might have to tinker with the
start-dfs.sh script itself, and have it redirect the stdin of the processes that will recieve the password. This also implies that our
feed-passwords.py script must print the passwords into the appropriate file each time, which likely means the architecture of the whole approach would change.
Also, while we're still not there yet, one more thing about this script that is is inflexible; it only works with a preset number of passwords (defined by the
maximum variable). It might be desirable to modify it such that it keeps monitoring for password prompts until a certain timeout (say, 15 seconds) has passed, thus accomodating to any number of password prompts without the need to change the number every time. (I can help you with that in case it is needed.)
Another note: If you don't want to store your password, just replace the
PASSWORD=... line with:
PASSWORD=input("Type your password: "). This will have you input your password only once every time you launch the script, and will not store your password anywhere.