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.
expect could be an option, but it's not present on my stripped-down system.
You can provide password as parameter to expect script.
Set SSH up for Public Key Authentication, with no pasphrase on the Key. Loads of guides on the net. You won't need a password to login then. You can then limit connections for a key based on client hostname. Provides reasonable security and is great for automated logins.
This can be done by setting up public/private keys on the target hosts you will be connecting to. The first step would be to generate an ssh key for the user running the script on the local host, by executing:
Then enter a blank password. After that, copy your ssh key onto the target host which you will be connecting to.
After registering the ssh keys, you would be able to perform a silent
ssh remote_user@other_host
from you local host.Maybe you can use an
expect
command?:That command gives the password automatically.
This is working.
For sudo there is a -S option for accepting the password from standard input. Here is the man entry:
This will allow you to run a command like:
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.