Get errorlevel in cmd script calling ssh

1

2

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.

Roman

Posted 2013-04-22T15:51:30.893

Reputation: 161

Well ssh exited cleanly, so the return value is 0. – Der Hochstapler – 2013-04-22T15:57:39.383

I understand this. But what is the proper way to get exit code returned from ssh session? – Roman – 2013-04-23T07:17:28.603

You could append ;echo $? to your command, but that won't set the errorlevel variable :\ – Der Hochstapler – 2013-04-23T11:46:06.520

Answers

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

Roman

Posted 2013-04-22T15:51:30.893

Reputation: 161