I am looking for a way to have two stdins to a bash script, namely one that is interactive and another which could leverage redirection
Say I need to invoke a program which asks for credentials of the type username, password I want to feed that interactively and then I want to feed a while loop from a redirection. Something like:
$> ./myscript.sh < input.lst
or
$> cat input.lst | ./myscript.sh
In my script there would be something like (very simplified):
#!/bin/bash
./authentication_application
while read i
do
echo $i
done
Where authentication_application is a binary I don't own (have no access to source code and haven't compiled it myself), I can't change and can't be replaced with any alternative authentication method.
And the invocation would result in
Username: some_user
Password: password
... contents of input.lst ...
With some_user
and password
input interactively and contents of input.lst
taken from the redirection
I tried redirecting stdin to a new fd and using read -u <fd>
inside the script with fd being "7" for instance, and something like this:
#!/bin/bash
exec <fd><&0
exec 0<&-
... but here is where I have no idea of how to re-open (or reset) stdin to the interactive one
Any Ideas? you people even think it's possible?
BTW, I'm not looking for the following:
- prepending
input.lst
with username and password on cleartext (nor any kind of cypher hack) - cat-ing two files into the script (the first one storing username and password on cleartext)
- A workaround where I have to use parameters to
get_credentials.sh
to pass username and password without prompting the user - passing the file input.lst as a parameter to the script and using positional parameters to access the file in the while loop (it would defeat the purpose of redirecting the input and leveraging the pipelining nature of the script i.e.
$> cat input.lst | ./filter_1.sh | ./filter_2.sh |... | ./filter_n.sh
)
./authentication_application
ask for the password in stdin? And not e.g. open/dev/tty
for that purpose, likessh
does (the client)? I suppose it doesn't have any other method for passing the credentials, like giving it a filename or using an environment variable, because in that case you probably wouldn't be asking this. – ilkkachu Feb 02 '21 at 18:49./authentication_applicationn
and feeding those in clear text but it would defeat the purpose. The purpose is creating succesive filters in bash that behave like native linux commands but with a small tweak (namely they need authentication) – Jorge Q Feb 02 '21 at 19:02< /dev/tty
works, then it indeed reads from stdin. You're right, passing a password on the command line is quite iffy, but through an env var should be ok. E.g.sshpass
accepts both, and is able to read from a file. – ilkkachu Feb 02 '21 at 19:04