feeding interactive shell commands

Hi,
like if i want to authenticate and i 100% know the password and username

i can run a script with
su - username
and then feed in the password through file pass.txt

script.sh < pass.txt

but if i don't know in which order the script is going to prompt for the input is there a way i can selectively answer prompts in a script mentioning the input before in a file like

if script asks for user:
input xyz
and if script ask password first
123456

please recommend
regards
Ltoso

Hi.

I don't know if it's that easy when you don't know the exact order of input your scripts expects.

But there is a tool - called expect - that may be able to help you.

Although I've never used it, I've seen it mentioned here a few times, and it does seem interesting.

expect(1) - Linux man page

Simple Telnet Automation Using Expect
(this isn't the exact example you were looking for, but it's not far off, I think)

I'm not sure I understand. I've never seen su prompt for user name. Or is the example of the script prompting for user name unrelated to su?

Anyway, one way to be able to selectively respond to requests for input is to use Expect.

For unix shell script I don't think so it's possible to input passwords, unless you are doing automated ftp from a script or sql scripts is possible unix level for normal shell scripts I think not possible.

However If you could su from root and run su command, then just

best of luck

You can use and expect(1) script for this.

Here is an example where the order of the quesions is known:

#!/usr/local/bin/expect 
spawn telnet <machine ip> 
expect "login:" 
send "<username>\n" 
expect "Password:" 
send "<password>\n" 
send "bash\n" 
send "cd /opt\n" 
send "ls -ltr\n"  (if you are not giving \n then it will wait for your response or u have to type enter manually). 
interact      

How to execute the �expect� command  expect �f <file name> 
Ex: expect �f <filename>.expect

from:
http://en.kioskea.net/forum/affich-37067-shell-script-to-telnet-and-run-commands#p96218

Expect can also respond to a list of questions with the appropriate answer using a while loop, e.g. (script to supply answers to fsck):

while 1 {
	expect {
		eof			{break}
		"UNREF FILE*CLEAR\\?"	{send "y\r"}
		"BAD INODE*FIX\\?"	{send "n\r"}
		"\\? "			{interact +}
		}
	}

# The last question mark is a catch all.
#  \\ prevents the next character from being interpreted as a wild card.