Azure / azure-cli-extensions

Public Repository for Extensions of Azure CLI.
https://docs.microsoft.com/en-us/cli/azure
MIT License
384 stars 1.25k forks source link

az ssh exit code always 0, even if subcommand fails #6831

Open pwandl opened 1 year ago

pwandl commented 1 year ago

Describe the bug

When using az ssh vm, the exit code is always 0, even if ssh command itself failed. When using openssh, the exit code of the ssh process returns the exit code of the executed command, but when using az ssh, the exit code is ignored, which makes it hard to determined whether the command was executed successfully.

Related command

az ssh vm

Errors

No actual error, but exit code is wrongly set to 0

[pwandl@fedora ~]$ az ssh vm -n some-vm -g some-group "exit 1"
OpenSSH_9.0p1, OpenSSL 3.0.9 30 May 2023
Transferred: sent 5540, received 4636 bytes, in 0.7 seconds
Bytes per second: sent 7414.5, received 6204.6
[pwandl@fedora ~]$ echo $?
0

Issue script & Debug output

az ssh vm --debug -n some-vm -g some-group "exit 1"

[...]
OpenSSH_9.0p1, OpenSSL 3.0.9 30 May 2023
[..]
debug1: Sending command: exit 1
debug2: channel 0: request exec confirm 1
debug3: send packet: type 98
debug2: channel_input_open_confirmation: channel 0: callback done
debug2: channel 0: open confirm rwindow 0 rmax 32768
debug2: channel 0: rcvd adjust 2097152
debug3: receive packet: type 99
debug2: channel_input_status_confirm: type 99 id 0
debug2: exec request accepted on channel 0
debug3: receive packet: type 96
debug2: channel 0: rcvd eof
debug2: channel 0: output open -> drain
debug2: channel 0: obuf empty
debug2: chan_shutdown_write: channel 0: (i0 o1 sock -1 wfd 5 efd 6 [write])
debug2: channel 0: output drain -> closed
debug3: receive packet: type 98
debug1: client_input_channel_req: channel 0 rtype exit-status reply 0
debug3: receive packet: type 98
debug1: client_input_channel_req: channel 0 rtype eow@openssh.com reply 0
debug2: channel 0: rcvd eow
debug2: chan_shutdown_read: channel 0: (i0 o3 sock -1 wfd 4 efd 6 [write])
debug2: channel 0: input open -> closed
debug3: receive packet: type 97
debug2: channel 0: rcvd close
debug3: channel 0: will not send data after close
debug2: channel 0: almost dead
debug2: channel 0: gc: notify user
debug2: channel 0: gc: user detached
debug2: channel 0: send close
debug3: send packet: type 97
debug2: channel 0: is dead
debug2: channel 0: garbage collecting
debug1: channel 0: free: client-session, nchannels 1
debug3: channel 0: status: The following connections are open:
  #0 client-session (t4 r0 i3/0 o3/0 e[write]/0 fd -1/-1/6 sock -1 cc -1 io 0x00/0x00)

debug3: send packet: type 1
Transferred: sent 5540, received 4636 bytes, in 0.7 seconds
Bytes per second: sent 7602.6, received 6362.0
debug1: Exit status 1
cli.knack.cli: Event: CommandInvoker.OnTransformResult [<function _resource_group_transform at 0x7f4b46483240>, <function _x509_from_base64_to_hex_transform at 0x7f4b464832e0>]
cli.knack.cli: Event: CommandInvoker.OnFilterResult []
cli.knack.cli: Event: Cli.SuccessfulExecute []
cli.knack.cli: Event: Cli.PostExecute [<function AzCliLogging.deinit_cmd_metadata_logging at 0x7f4b46799940>]
az_command_data_logger: exit code: 0
cli.__main__: Command ran in 4.526 seconds (init: 0.173, invoke: 4.353)
telemetry.main: Begin splitting cli events and extra events, total events: 1
telemetry.client: Accumulated 0 events. Flush the clients.
telemetry.main: Finish splitting cli events and extra events, cli events: 1
telemetry.save: Save telemetry record of length 4444 in cache
telemetry.main: Begin creating telemetry upload process.
telemetry.process: Creating upload process: "/usr/bin/python3 /home/pwandl/.local/lib/python3.11/site-packages/azure/cli/telemetry/__init__.py /home/pwandl/.azure"
telemetry.process: Return from creating process
telemetry.main: Finish creating telemetry upload process.

Expected behavior

The exit code should match the exit code of the subcommand, similar when using ssh like this:

[pwandl@fedora ~]$ ssh -F devconf some-host "exit 1"
[pwandl@fedora ~]$ echo $?
1

Environment Summary

azure-cli 2.51.0 *

core 2.51.0 * telemetry 1.1.0

Extensions: containerapp 0.3.37 front-door 1.0.17 interactive 0.5.3 ssh 2.0.1

Dependencies: msal 1.24.0b1 azure-mgmt-resource 23.1.0b2

Additional context

No response

yonzhan commented 1 year ago

Thank you for opening this issue, we will look into it.

techmantel commented 6 months ago

Any updates on this? Facing the same problem when building a script executing this command that I expected to get an exit code from if the az ssh arc... command fails to connect to the Arc machine.

tomi commented 2 months ago

Would love to see this getting fixed. I believe it would be expected behavior to propagate the exit code from the ssh command to az cli