Execute Sudo Command on Linux from Plink.Exe on Windows

Execute sudo command on Linux from plink.exe on Windows

i know that the question is old but this can help

you can execute plink (putty tools)

plink -v -ssh -m "path/to/commands.txt" -l usertologin -pw PASSWORDSERVER

commands.txt:

echo -e "PASSWORD\n" | sudo -S cat /etc/passwd

sudo -S is to receive the password from stdin and with the echo -e "password\n" is to pass the password (and \n is like to press intro -new line- )

the same way you can execute the passwd command:

> echo -e "OLDPASSWORD\nNEWPASSWORD\nNEWPASSWORD" | passwd

Executing (sudo) subcommands using Plink

sudo -i && cd /root/docker/storm-supervisor/ && ./stop-all.sh

Try your command in Linux shell. It won't work either. It will execute an elevated shell and wait for you to type commands. Only after you leave sudo shell, it will run the other commands (using the original account).

The cd and ./stop-all.sh are sub-commands of the sudo. So you have to treat them that way.

  • Best way is to provide the commands on sudo commandline:

      sudo "cd /root/docker/storm-supervisor/ && ./stop-all.sh"

    But that will probably require modifications of the sudoers file. Though it's the right way.

  • Or you will need to feed the commands to sudo input:

      echo "cd /root/docker/storm-supervisor/ && ./stop-all.sh && exit" | sudo
  • Or feed everything to Plink input:

    (
    echo cd /root/docker/storm-supervisor/
    echo ./stop-all.sh
    ) | plink -ssh -l username -pw root username@10.223.26.34 -t sudo -i
  • Or even:

    (
    echo sudo -i
    echo cd /root/docker/storm-supervisor/
    echo ./stop-all.sh
    ) | plink -ssh -l username -pw root username@10.223.26.34 -t

use powershell and plink to shutdown remote linux computer with sudo command

Make sure that the user can indeed run sudo commands.
The issue might be that the user that is trying to run the command is requested to insert the sudo password.
In the sudoers file insert <YOUR_USERNAME> ALL=(ALL) NOPASSWD

Executing command on Plink command line fails with not found

This is covered in these questions:

  • Script via Plink in .bat behaves differently
  • sudo: command not found while using plink

So one easy solution is that you can try which poweroff in a normal session, to see where poweroff resides (can be /sbin/poweroff). And then use a full path in your plink command-line.

Though the right solution is to fix your startup scripts. See the links above.


As your command-line does not work even with the -t switch, your SSH server must execute a command in "exec" channel (used when a command is provided on command-line or using -m switch) differently than in a "shell" channel. This is rather unusual.

You can force plink to use "shell" channel (like in an interactive session) by using an input redirection:

echo poweroff| plink ...


Related Topics



Leave a reply



Submit