powershellsshraspberry-piplinklshell

Executing command using Plink does not work, but does in PuTTY


I am trying to create a PowerShell script to SSH into a Raspberry Pi and perform some commands. I am using Plink to SSH into the Pi over command line. However I have to SSH into the Pi user which runs in lshell. I can PuTTY into the Pi with zero issues, but when using Plink I get an error saying the command I am using is forbidden. I am trying to use su which works when using PuTTY by not Plink.

The error I get is below:

plink : *** forbidden char/command over SSH: "su"
At line:1 char:1
+ plink -ssh pi@<IP> -pw <password> su
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (*** forbidden c... over SSH:         
"su":String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError

This incident has been reported.

Thank you for any help you can provide.


Solution

  • There are two differences when using plink ... command comparing to logging in with PuTTY and typing command:

    1. plink ... command opens an "exec" channel to run the command. While PuTTY opens "shell" channel with login shell and only the shell runs the command.

    2. plink ... command by default opens a non-interactive session (no TTY). While PuTTY opens an interactive session (with TTY).


    To test the Plink behavior in PuTTY:

    1. Enter the command to Connection > SSH > Remote command.
    2. Check Connection > SSH > TTY > Don't allocate a pseudo-terminal.

    Conversely, to test PuTTY behavior in Plink, use:

    echo command | plink ...
    

    Check also Plink -T/-t switches.

    This behaves as PuTTY in 2, but not in 1:

    plink -t ... command
    

    Conversely, this behaves as PuTTY in 1, but not in 2:

    echo command | plink -T ...
    

    Experiment with these to find out, what is causing your particular problem.

    For solutions, see Script via Plink in .bat behaves differently.