Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 889
  • Last Modified:

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
     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
karans
Asked:
karans
  • 2
  • 2
  • 2
  • +2
1 Solution
 
jlevieCommented:
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
 
owensleftfootCommented:
I would go with jims third option - use expect. You could also write a perl script to do the same thing.
0
 
karansAuthor Commented:
I am not too sure what you mean by "use expect in a perl script". Could you elucidate?

karans
0
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 
owensleftfootCommented:
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
 
jlevieCommented:
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
 
GnsCommented:
... 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
 
bjorn_rohlenCommented:
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
 
GnsCommented:
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
 
karansAuthor Commented:
Well, Gns explained well about the comment from bjorn_rohlen.

Thanks to all of you for the answers.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now