Script via Plink in .bat behaves differently

后端 未结 2 2054
天命终不由人
天命终不由人 2021-01-16 01:31

I have a .bat file on my Windows machine. This .bat file uses plink.exe to connect to an Ubuntu machine and execute an .sh

2条回答
  •  萌比男神i
    2021-01-16 01:38

    In the other cases, you are using interactive sessions.

    While the Plink uses non-interactive session by default, when you specify a command on its command-line.

    Your script probably relies on some environment variables (like PATH) being set specifically.

    It's quite probable that the variables are set only for interactive sessions. Probably because they are modified in a startup script that is executed (sourced) for the the interactive sessions only.

    Solutions are:

    • Correct the startup scripts to modify the variables unconditionally (even for non-interactive sessions).

    • Modify the script not to rely on environment variables.

      Or you can source the profile script, see Unable to run shell script with ktutil command from Windows using PLINK.

    • Force the Plink to use the interactive session using the -t switch

      This is not a recommended solution, as using the interactive session to automate a command execution can bring you nasty side effects. See for example Is there a simple way to get rid of junk values that come when you SSH using Python's Paramiko library and fetch output from CLI of a remote machine?


    Some more obscure SSH servers can also behave differently when "exec" channel is used to execute the command. See Executing command on Plink command line fails with "not found".

提交回复
热议问题