Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

powershell - Executing (sudo) subcommands using Plink

I am trying to command Linux machine from Window PowerShell. The commands are dependent on the fail/pass of the commands before. Therefore, I have to put all the commands together. I have tried multiple ways of putting commands together but at the ends I only receive the output of the first command.

PS C:Userssams> plink -ssh -l username -pw root [email protected] -t
      "sudo -i && cd /root/docker/storm-supervisor/ && ./stop-all.sh"

The actual result: Only receives the output of the first command.

Expected result: Receives output of the final command.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
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 [email protected] -t sudo -i
    
  • Or even:

    (
      echo sudo -i
      echo cd /root/docker/storm-supervisor/
      echo ./stop-all.sh
    ) | plink -ssh -l username -pw root [email protected] -t
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...