1

I am connecting to a server via plink as follows:

plink -ssh -l username -pw password example.host.com -m C:\Putty\input.txt > C:\Putty\output.txt

input.txt contains

whoami
su - newsuser
whoami

output.txt contains

username
username

SU is not working via plink.

If try the same command in putty its working fine and getting the output as "newsuser".

Linux PuTTY - automate su for password related issue

i tried the above solution but its not working for me.

thanks

1 Answers1

3

First, it does not work the way you think.

The commands in the script are executed in sequence. The su (had it succeeded) would execute an interactive shell session and wait for user input (commands). Only once the su exists, the second whoami runs (showing username).


Anyway, your su does not work indeed.

First, the su for sure outputs some error message. What is that? You capture only a standard output in your commands. Capture an error output too (2>&1). It most likely fails, because su may need an interactive terminal, while Plink with -m switch uses non-interactive terminal by default. Adding -t switch helps in this case.


Note that automating su is generally a bad idea. If you need to run some commands that require root privileges, a better solution is to associate a dedicated private key with the commands in sudoers file. And then use sudo and private key in the Plink.

See also Allowing automatic command execution as root on Linux using SSH.