开发者

get return code from plink?

开发者 https://www.devze.com 2023-01-23 14:38 出处:网络
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 det

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 ge开发者_如何学Ct this information back through plink?


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消