12

In a DOS batch script, I'm running a single command on a remote (also windows) computer using plink. Formerly, this command was only run on the local machine, and was relying on the return code to determine success. Is there a way to easily get this information back through plink?

Nate
  • 12,499
  • 5
  • 45
  • 60

2 Answers2

6

That's not possible with plink. The current consensus is to have the remote script echo its exit code to a log file, then use pscp to transfer the log file to the local machine.

See http://fixunix.com/ssh/74235-errorlevel-capturing-plink.html.

Frédéric Hamidi
  • 258,201
  • 41
  • 486
  • 479
  • 9
    For the benefit of anyone reading this today, perhaps this was true when it was written but it's not true anymore. The current version of plink will return the remote exit code when using the ssh protocol. – richb Mar 28 '14 at 23:16
  • ...working examples [**here**](http://stackoverflow.com/a/2988139/319204) and [**here**](http://superuser.com/a/130528). – TheCodeArtist Feb 08 '15 at 05:07
  • 3
    @richb: Not exactly true. Plink 0.63 succeeds (exit status 0) when a connection cannot be made. :-( – jrw32982 May 13 '15 at 17:38
  • As it is said in http://stackoverflow.com/a/33666492 and from the comments here, you can get some exit code from plink. - 0:Succeded - 1:Failed – myuce Dec 06 '16 at 10:56
4

with plink 0.66

C:\Code>echo Y | "C:\Program Files (x86)\PuTTY\plink.exe" bob@myserver exit 42

C:\Code>echo %ERRORLEVEL%
42

Also for @John Wiersba's concern about when a connection cannot be made, this appears to be fixed

C:\CodeMisc>echo Y | "C:\Program Files (x86)\PuTTY\plink.exe" bob@garbageservername exit 42
Unable to open connection:
Host does not exist
C:\Code>echo %ERRORLEVEL%
1

Also note the piping of echo Y ... this enables you to accept the server fingerprint automatically (a little dangerous to say the least ... but our login server is load balanced, so you are always getting different fingerprints :( )

However as @LeonBloy notes, plink still has some connection conditions which return a zero exit code. If you know your exit code range and you don't have a good way of communicating back to windows via a file. You could either +3 to the exit code (if you know the exit code will never == 253-255) or you could apply a bitwise OR (I'd suggest exit $(($?|128)) - in bash).

Or if you don't care about the exact exit code, you could return 2 for success, and zero for failure. Thus a non-two exit code would indicate failure. In bash this would be: echo $((($?==0) << 1)). This would be by far the most robust general purpose solution, but you should make sure your exit code is logged for debug-ability.

DevNull
  • 131
  • 7
  • _... this appears to be fixed_ ... up to a point: ``>plink.exe -batch -ssh -i key.ppk pi@192.168.1.2 Using username "pi". Server refused our key >echo %ERRORLEVEL% 0 `` – leonbloy Jan 12 '17 at 14:33
  • Just in case, in PowerShell you can similarly use integer LASTEXITCODE variable or boolean $? variable. – yaromir Mar 15 '23 at 12:36