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
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
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
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.