Solved

Re-direction of data to STDIN

Posted on 2004-08-28
9
881 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Guacamole cut and paste issue 3 74
Weird Samba Connectivity Issue... 7 40
Coding C# in Linux 8 69
Like to learn Linux, need advice for a good book and setup. 3 45
rdate is a Linux command and the network time protocol for immediate date and time setup from another machine. The clocks are synchronized by entering rdate with the -s switch (command without switch just checks the time but does not set anything). …
Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
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…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

778 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