Re-direction of data to STDIN

Hi All

I have a binary that asks me for a password for some operations. I want to supply it the password by re-directing the passwd from a BASH script/C program. The standard methods:
binaryname --options < filename-containing-mypass
binaryname --options < mypass
binaryname --options < EOF

does not work. I am still prompted for the password. The program has the following output:
$ binaryname --options
enter password:

I am running a box having RH9.0, my shell being bash, and I would like to solve it on a bash shell only. I know that it can be solved on a ksh with relative ease. I have the source for the program as well, it opens the descriptor for tty and gets data from stdin.

Who is Participating?
owensleftfootConnect With a Mentor Commented:
I didnt say use expect in a perl script. I said you could use expect or a perl script. Expect would probably be easier. From the manpage -

Expect is a program that "talks" to other interactive programs according to a script. Following the script, Expect knows what can be expected from a program and what the correct response should be. An interpreted language provides branching and high-level control structures to direct the dialogue. In addition, the user can take control and interact directly when desired, afterward returning control to the script.

Expectk is a mixture of Expect and Tk. It behaves just like Expect and Tk's wish. Expect can also be used directly in C or C++ (that is, without Tcl). See libexpect(3).

The name "Expect" comes from the idea of send/expect sequences popularized by uucp, kermit and other modem control programs. However unlike uucp, Expect is generalized so that it can be run as a user-level command with any program and task in mind. Expect can actually talk to several programs at the same time.

For example, here are some things Expect can do:

        Cause your computer to dial you back, so that you can login without paying for the call.
        Start a game (e.g., rogue) and if the optimal configuration doesn't appear, restart it (again and again) until it does, then hand over control to you.
        Run fsck, and in response to its questions, answer "yes", "no" or give control back to you, based on predetermined criteria.
        Connect to another network or BBS (e.g., MCI Mail, CompuServe) and automatically retrieve your mail so that it appears as if it was originally sent to your local system.
        Carry environment variables, current directory, or any kind of information across rlogin, telnet, tip, su, chgrp, etc.

There are a variety of reasons why the shell cannot perform these tasks. (Try, you'll see.) All are possible with Expect.

In general, Expect is useful for running any program which requires interaction between the program and the user. All that is necessary is that the interaction can be characterized programmatically. Expect can also give the user back control (without halting the program being controlled) if desired. Similarly, the user can return control to the script at any time.  
If the program is opening the tty for the password it isn't reading from STDIN and redirection by the shell won't work. Your choices then might be:

o - Modify the source to read the password from STDIN

o - Modify the source to read the password from a command line option (e.g., 'binaryname -p password ...')

o - Use and Expect script to read the password prompt and supply the password
I would go with jims third option - use expect. You could also write a perl script to do the same thing.
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

karansAuthor Commented:
I am not too sure what you mean by "use expect in a perl script". Could you elucidate?

owensleftfoot did a good job of explaining what Expect does. A trivial example of using for what you've described might look something like:

#!/usr/local/bin/expect --
spawn binaryname -opt1 -opt2
expect "enter password:"
send "user-password\r"
... Provided expect is installed in /usr/local/bin Jim;-). Do "which expect" to see that you have it installed, and where... Otherwise use your distributions usual method to install it.

Expect is probably the best solution for you (perl fanatics might disagree:-), and you can even check the script by typiong it manually... first run "expect" so you get to the expect prompt, then enter the script Jim gave above (everything but the first line ("#!/usr/local....") since that is an instruction to the shell which command to pass the script to:-)... Should work nicely. If the spawning of "binaryname" takes a bit too long, you can insert a "sleep 1" between the spawn and first expect line.

-- Glenn
Or try

shell$ programname --option `cat /my/file/with/password`

those are enclosing backticks -> `

make sure the file contains just the password on a single line, no extra data.

No Björn, that won't work, unless the program in question can take the passwd as a commandline option... Which would eliminate the initial problem entirely, so isn't likely to be the case...;-)

-- Glenn
karansAuthor Commented:
Well, Gns explained well about the comment from bjorn_rohlen.

Thanks to all of you for the answers.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.