tags:

views:

5064

answers:

8

I'm writing a C/Shell program that will be doing su or sudo or ssh. They all want their passwords in consol input rather than stdin.

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.

+2  A: 

Take a look at expect linux utility.

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

Dev er dev
+5  A: 

hardcoding a password in an expect script is the same as having a passwordless sudo, actually worse, sudo at least logs its commands

Aleksandar Ivanisevic
+5  A: 

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.

diciu
A: 

You can provide password as parameter to expect script.

Dev er dev
A: 

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.

A: 

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.

mlambie
+1  A: 

su -c "Command" < "Password"

Hope it is helpful.

Shankar
A: 
echo <password> | su -c <command> <user> 

This is working.

DevOz