Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

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

1 Answer 1

up vote 1 down vote accepted

I've found a solution, thanks to following question: http://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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.