feeding interactive shell commands

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting feeding interactive shell commands
# 1  
Old 08-09-2009
feeding interactive shell commands

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

please recommend
# 2  
Old 08-09-2009

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)
# 3  
Old 08-09-2009
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.
# 4  
Old 08-09-2009
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.

ftp -inv << EOF
user oracle pass apple$2 (syntax varies)
cd /etc
<do something>
However If you could su from root and run su command, then just

example: su - oracle -c /home/oracle/bin/dbstart <<---(don't need to specify any passwords)
best of luck
# 5  
Old 08-09-2009
You can use and expect(1) script for this.

Here is an example where the order of the quesions is known:
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). 

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


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.

Last edited by TonyFullerMalv; 08-09-2009 at 12:34 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Interactive UNIX shell query

Hi guys, I've create 3 shells concerning my work, which named as 1.sh, 2.sh and 3.sh. However, how can I make an interactive query for these shells just like the old (fdisk) in windows9x. I want to make an interface tells the user just like this: Press 1 to execute "1.sh" Press 2 to... (5 Replies)
Discussion started by: leo_ultra_leo
5 Replies

2. Shell Programming and Scripting

Shell script using an interactive command

Hello experts, I have a to write script for monitoring, the script would use a command and I plan to write the script as follows while true do command -arg sleep 2 clear done The output would be set up on a screen for monitoring. However the issue is that the command used in... (2 Replies)
Discussion started by: maverick_here
2 Replies

3. UNIX for Dummies Questions & Answers

What is login and interactive shell?

Hi Guys, Excuse if am asking silly Que ... :rolleyes: Please explain me whats difference between login and interactive shell in Linux .. Have googled but still in doubt .. :confused: --Shirish Shukla (4 Replies)
Discussion started by: Shirishlnx
4 Replies

4. Shell Programming and Scripting

Interactive shell - Give answers to shell

Hi, I have a script that calles some other scripts. Those scripts are expecting some inputs. Right no I am typing manually. But now the number of questions from other scripts are too much and I want to give asnwers autimatically. example. Other scripts gives me 2 options 1) joom... (2 Replies)
Discussion started by: microsim
2 Replies

5. Shell Programming and Scripting

Interactive shell through a pipe

I'm new to working with pipes, so I don't know whether the following is expected behaviour or a bug on the part of an application. Since Version 0.47, Inkscape provides a non-GUI interactive shell mode of operation. I am trying to time the program's performance in converting SVG files to PNG... (1 Reply)
Discussion started by: ccprog
1 Replies

6. Homework & Coursework Questions

How to write script that behaves both in interactive and non interactive mode

Q. Write a script that behaves both in interactive and non interactive mode. When no arguments are supplied it picks up each C program from the directory and prints first 10 lines. It then prompts for deletion of the file. If user supplies arguments with the script , then it works on those files... (8 Replies)
Discussion started by: rits
8 Replies

7. Homework & Coursework Questions

Help with Interactive / Non Interactive Shell script

Q. Write a script that behaves both in interactive and non interactive mode. When no arguments are supplied it picks up each C program from the directory and prints first 10 lines. It then prompts for deletion of the file. If user supplies arguments with the script , then it works on those files... (1 Reply)
Discussion started by: rits
1 Replies

8. Shell Programming and Scripting

Can BASH execute commands on a remote server when the commands are embedded in shell

I want to log into a remote server transfer over a new config and then backup the existing config, replace with the new config. I am not sure if I can do this with BASH scripting. I have set up password less login by adding my public key to authorized_keys file, it works. I am a little... (1 Reply)
Discussion started by: bash_in_my_head
1 Replies

9. Shell Programming and Scripting

Korn shell interactive script

Hi, How can I prompt a user for two input and pass the input to variables in the script. I have the following script but it is not working: +++++++++Begin+++++++++++ #!/bin/sh database_c=$1 output_f=$2 echo "Your db is $1\nOutput is $2" +++++++++End+++++++++++ Thanks, Leonard (3 Replies)
Discussion started by: leonard905
3 Replies

10. Programming

need help in implementing simple interactive shell in C

hello all, i hv attached herewith my program to implement a simple interactive shell in C. no matter hw hard I try, I keep getting some errors. i need help - urgently !! proj1test7.c: In function `parseCommand': proj1test7.c:102: warning: assignment makes pointer from integer without a cast... (2 Replies)
Discussion started by: nix1209
2 Replies
Login or Register to Ask a Question