Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am connecting to a server via PuTTY as follows:

putty.exe -ssh user@host -pw password -noagent -m commands.txt

After that I have to use:

su someUser

It will ask for a password - I have put this inside command.txt but it's not working.

How can I automate the su command (including password) in a single line that I can place inside command.txt? Or perhaps there is a different approach?

I don't want to use sudo or execute and all. I tried with:

echo password | su someUser

However piping in this way did not work.

share|improve this question

migrated from Jun 17 '11 at 11:31

This question came from our site for professional and enthusiast programmers.

Why don't you log in as 'someuser' in the first place? – Flimzy Jun 17 '11 at 22:29

You can disable password questions by adding a script/program specific line in the /etc/sudoers file: yourusername ALL=(ALL) NOPASSWD: /usr/local/bin/whatever

share|improve this answer
For unexplained reasons the OP won't use sudo, though. Your answer is correct, but he's not interested. – CarlF Jun 17 '11 at 13:05

Is it vital for you to use su and save the password in a file? If not use a different approach, becasue:

$ su << LOL
> secretpassword
su: must be run from a terminal

$ echo secretpassword|su - 
su: must be run from a terminal

You would be better off with ssh keys: Quck ssh-keys intro

share|improve this answer

Why don't you want to use sudo? su will always ask for the password using terminal capabilities, so you'd need to create a pseudo-terminal to communicate with it. expect can do that for you.

However, I think that the "proper" way to solve this is either using a suid executable owned by a specific user, or even better - using properly configured sudo.

share|improve this answer
suid is rarely, if ever a good idea. – new123456 Jun 17 '11 at 14:29
@new123456 sudo with NOPASSWD, available for all is pretty much suid equivalent. Still better than expect + su. Of course properly configured sudo is the best choice here. – viraptor Jun 17 '11 at 14:53

You must log in to answer this question.