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.