Advertisement
Advertisement


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


Question

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.

2018/08/17
1
149
8/17/2018 5:12:50 PM

Accepted Answer

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.

2010/12/01
215
12/1/2010 5:27:26 PM

I wrote some Applescript which prompts for a password via a dialog box and then builds a custom bash command, like this:

echo <password> | sudo -S <command>

I'm not sure if this helps.

It'd be nice if sudo accepted a pre-encrypted password, so I could encrypt it within my script and not worry about echoing clear text passwords around. However this works for me and my situation.

2008/12/09

For ssh you can use sshpass: sshpass -p yourpassphrase ssh [email protected].

You just need to download sshpass first :)

$ apt-get install sshpass
$ sshpass -p 'password' ssh [email protected]
2018/05/18

I've got:

ssh [email protected] bash -c "echo mypass | sudo -S mycommand"

Works for me.

2012/07/30

For sudo you can do this too:

sudo -S <<< "password" command
2015/06/09

The usual solution to this problem is setuiding a helper app that performs the task requiring superuser access: http://en.wikipedia.org/wiki/Setuid

Sudo is not meant to be used offline.

Later edit: SSH can be used with private-public key authentication. If the private key does not have a passphrase, ssh can be used without prompting for a password.

2008/10/24

Source: https://stackoverflow.com/questions/233217
Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Email: [email protected]