Solved

Re-direction of data to STDIN

Posted on 2004-08-28
9
882 Views
Last Modified: 2008-02-01
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
     OR
binaryname --options < mypass
     OR
#!/bin/bash
binaryname --options < EOF
mypass
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.

Thanks
Karan
0
Comment
Question by:karans
  • 2
  • 2
  • 2
  • +2
9 Comments
 
LVL 40

Expert Comment

by:jlevie
ID: 11921831
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
0
 
LVL 17

Expert Comment

by:owensleftfoot
ID: 11922510
I would go with jims third option - use expect. You could also write a perl script to do the same thing.
0
 

Author Comment

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

karans
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 17

Accepted Solution

by:
owensleftfoot earned 50 total points
ID: 11925754
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.  
0
 
LVL 40

Expert Comment

by:jlevie
ID: 11926171
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"
close
0
 
LVL 20

Expert Comment

by:Gns
ID: 11929478
... 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
0
 

Expert Comment

by:bjorn_rohlen
ID: 11932859
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.

-Bjorn
0
 
LVL 20

Expert Comment

by:Gns
ID: 11939997
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
0
 

Author Comment

by:karans
ID: 11949340
Well, Gns explained well about the comment from bjorn_rohlen.

Thanks to all of you for the answers.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
Fine Tune your automatic Updates for Ubuntu / Debian
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question