I'm writing some automations and I've run into a problem where a host's key has changed and a wrench gets thrown into the works. I'd like to add a pre-flight check for this specific condition, but I can't seem to get ssh
to return anything more than its catch-all 255 return code.
eg:
$ ssh -o StrictHostKeyChecking=yes foobar@squiffy-host ls; echo $?
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: POSSIBLE DNS SPOOFING DETECTED! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
...snip...
255
How can I detect this one, specific case?
Building a little bit on your shell snippit, you could try this:
It should output 0 if the grep invocation finds the error message, and 1 if not.
At this point I'm not terribly optimistic that there's a perfect answer, so I've gone with your suggestions to parse the error text.
Basically, IF the return code is 255 AND all error messages are within the specified set THEN the return code is 1.
All other error messages/codes return 2, and no errors returns 0.