0

I have written a shell script which in turn calls another script, but the second one prompts for password. I want to supply the password and pass the prompt without user begin notified.

Here is my script:

su - s3 -c "su.sh"

I trided :

su - s3 -c "echo pass | su.sh"

But I got an error:

standard in must be a tty

Updated:

The screenshot of commands.

enter image description here

I also tired :

 su - s3 -c "echo mypass|sudo -S su.sh"

but another error comes up:

sudo: sorry, you must have a tty to run sudo

su.sh commands:

if [ ! -n "$IS_WINDOWS" ]
then
   /bin/su root -c "...."
fi
2
  • First locate what part of the script is issuing the error message. Or paste your script in the question. Chances are you really want to use sudo instead of su - hard coding passwords in scripts is never a good idea. Commented Mar 2, 2015 at 15:25
  • just edited the question. actually the command inside double quote throws the error. Commented Mar 2, 2015 at 15:34

2 Answers 2

1

It seems that you need to open a pty. You can try socat:

echo pass | socat -  exec:su.sh,pty,stderr,su=s3,ctty

This way the password is not in the command line arguments, which would be a security issue.

The better option would be to modify su.sh in the way that it does not need a password anymore.

2
  • I saw the su.sh file and it has a line what I mentioned in edited question. How could I modify it so it does not require password any more ? Commented Mar 2, 2015 at 15:49
  • If the command in the su root line can be called before the script, just delete the line and call it by hand before the script. Then you can avoid the double su issue which requires passwords. Commented Mar 2, 2015 at 16:15
0

You may use the expect program to supply a response when the password is asked. Note this also has the security problem of hard-coding passwords and is usually better to set a sudo rule for the passwordless execution of the command.

2
  • Is it only possible with expect tool ? Commented Mar 2, 2015 at 16:13
  • It's not the only solution but is the easiest one if you want to supply it through the script. Better solutions require changing configuration of system permissions and usually are more secure. The correct solution instead of script is to use sudo. Commented Mar 2, 2015 at 16:17

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.