Alex Rivera | Logout

How to pass the password to su/sudo/ssh without overriding the TTY?

Asked 2008-10-24T11:59:55.780
165

I'm writing a C Shell program that will be doing su or sudo or ssh. They all want their passwords in console input (the TTY) rather than stdin or the command line.

Does anybody know a solution?

Setting up password-less sudo is not an option.

could be an option, but it's not present on my stripped-down system.

Edit
Report

4 Answers

240

For sudo there is a -S option for accepting the password from standard input. Here is the man entry:

    -S          The -S (stdin) option causes sudo to read the password from
                the standard input instead of the terminal device.

This will allow you to run a command like:

echo myPassword | sudo -S ls /tmp

As for ssh, I have made many attempts to automate/script it's usage with no success. There doesn't seem to be any build-in way to pass the password into the command without prompting. As others have mentioned, the "expect" utility seems like it is aimed at addressing this dilemma but ultimately, setting up the correct private-key authorization is the correct way to go when attempting to automate this.

answered 2010-12-01T17:27:26.380
8

Take a look at expect linux utility.

It allows you to send output to stdio based on simple pattern matching on stdin.

answered 2008-10-24T12:01:57.507
3
echo <password> | su -c <command> <user> 

This is working.

answered 2010-10-18T14:02:06.383
-3

You can provide password as parameter to expect script.

answered 2008-10-24T12:07:16.997

Your Answer