Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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 stackoverflow.com 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
add comment

3 Answers

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
add comment

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
> LOL
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
add comment

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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.