1

ssh -i private_linux_key user@machine "exit 1" || echo %errorlevel%

This always prints 0. How can I get actual error code returned by ssh?

|| somehow detects that there was an error.

3
  • Well ssh exited cleanly, so the return value is 0. Apr 22, 2013 at 15:57
  • I understand this. But what is the proper way to get exit code returned from ssh session?
    – Roman
    Apr 23, 2013 at 7:17
  • You could append ;echo $? to your command, but that won't set the errorlevel variable :\ Apr 23, 2013 at 11:46

1 Answer 1

2

I've found a solution, thanks to following question: https://stackoverflow.com/questions/11554324/batch-file-errorlevel-issue

setlocal enabledelayedexpansion
ssh -i private_linux_key user@machine "exit 1" || echo !errorlevel!

Or even simplier

ssh -i private_linux_key user@machine "exit 1"
exit %ERRORLEVEL%

The latter is possible only when there are no more additional script lines after exit, of course

To read more about enabledelayedexpansion and windows/unix shells differences: http://blogs.msdn.com/b/oldnewthing/archive/2006/08/23/714650.aspx

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .