get return code from plink?

浪子不回头ぞ 提交于 2019-12-04 01:59:08

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!