0

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.

Gaff
  • 18,569
  • 15
  • 57
  • 68

3 Answers3

2

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

Friek
  • 121
  • 2
  • 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
0

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

0

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.

viraptor
  • 589
  • 7
  • 20
  • `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