HEX
Server: Apache/2.4.68 (Debian)
System: Linux as-cs-widget-demo-us-central1 6.1.0-44-cloud-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.164-1 (2026-03-09) x86_64
User: root (0)
PHP: 8.2.32
Disabled: NONE
Upload Files
File: //usr/lib/google-cloud-sdk/lib/surface/compute/__pycache__/ssh.cpython-312.pyc
�

��{S���dZddlmZddlmZddlmZddlZddlZddlZddlm	Z	ddl
mZddlm
Z
dd	lmZdd
lmZddlmZddlmZdd
lmZddlmZddlmZddlmZddlmZdd	lmZddlmZddlmZddlmZddlmZddl m!Z!dZ"dZ#ddd�Z$dZ%d�Z&d�Z'd�Z(d �Z)d!�Z*d"e+d#e+d$e+d%e+d&e+d'e,d(e+fd)�Z-d*�Z.		d1d+�Z/ej`ejbejHjd�Gd,�d-ejf���Z4ej`ejbejHjjejHjl�Gd.�d/e4���Z7d0�Z8e8�e7_9e8�e4_9y)2z3Implements the command for SSHing into an instance.�)�absolute_import)�division)�unicode_literalsN)�base_classes)�base)�
completers)�flags)�
iap_tunnel)�network_troubleshooter)�scope)�	ssh_utils)�user_permission_troubleshooter)�vm_boot_troubleshooter)�vm_status_troubleshooter)�vpc_troubleshooter)�
containers)�ssh)�log)�
properties)�retryz�
Recommendation: To check for possible causes of SSH connectivity issues and get
recommendations, rerun the ssh command with the --troubleshoot option.

{0}
z1
Or, to investigate an IAP tunneling issue:

{0}
�alpha�beta)rrzK
Starting ssh troubleshooting for instance {0} in zone {1}
Start time: {2}
c�*�|jdd��y)Nz	--commandzt      A command to run on the virtual machine.

      Runs the command on the target instance and then exits.
      ��help��add_argument��parsers �lib/surface/compute/ssh.py�
AddCommandArgr!Ds������
���c��|jddd��|jdtjdd��|jd	tjd
d��y
)z>Additional flags and positional args to be passed to *ssh(1)*.z
--ssh-flag�appendaA      Additional flags to be passed to *ssh(1)*. It is recommended that flags
      be passed using an assignment operator and quotes. Example:

        $ {command} example-instance --zone=us-central1-a --ssh-flag="-vvv" --ssh-flag="-L 80:localhost:80"

      This flag will replace occurences of ``%USER%'', ``%INSTANCE%'', and
      ``%INTERNAL%'' with their dereferenced values. For example, passing
      ``80:%INSTANCE%:80'' into the flag is equivalent to passing
      ``80:162.222.181.197:80'' to *ssh(1)* if the external IP address of
      'example-instance' is 162.222.181.197.

      If connecting to the instance's external IP, then ``%INSTANCE%'' is
      replaced with that, otherwise it is replaced with the internal IP.
      ``%INTERNAL%'' is always replaced with the internal interface of the
      instance.
      ��actionr�	user_hostz[USER@]INSTANCEa8      Specifies the instance to SSH into.

      ``USER'' specifies the username with which to SSH. If omitted,
      the user login name is used. If using OS Login, USER will be replaced
      by the OS Login user.

      ``INSTANCE'' specifies the name of the virtual machine instance to SSH
      into.
      )�	completer�metavarr�ssh_argszW          Flags and positionals passed to the underlying ssh implementation.
          z\        $ {command} example-instance --zone=us-central1-a -- -vvv -L 80:%INSTANCE%:80
      )�nargsr�exampleN)rr�InstancesCompleter�argparse�	REMAINDERrs r �
AddSSHArgsr0Nst������
�
���*	�����-�-��	
�	�
�	��������
�
�r"c�*�|jdd��y)Nz--containera?          The name or ID of a container inside of the virtual machine instance
          to connect to. This only applies to virtual machines that are using
          a Google Container-Optimized virtual machine image. For more
          information, see [](https://cloud.google.com/compute/docs/containers).
          rrrs r �AddContainerArgr2s��������r"c�.�|jdddd��y)Nz
--internal-ipF�
store_truead        Connect to instances using their internal IP addresses rather than their
        external IP addresses. Use this to connect from one instance to another
        on the same VPC network, over a VPN connection, or between two peered
        VPC networks.

        For this connection to work, you must configure your networks and
        firewall to allow SSH connections to the internal IP address of
        the instance to which you want to connect.

        To learn how to use this flag, see
        [](https://cloud.google.com/compute/docs/instances/connecting-advanced#sshbetweeninstances).
        )�defaultr&rr)�groups r �AddInternalIPArgr7�s#�������
��	�
r"c��d}tj�r|dz
}|dz
}tj�r|dz
}|dz
}tj�r|dz
}|dzS)z0Generate the help text for troubleshot argument.z�          If you can't connect to a virtual machine (VM) instance using SSH, you can investigate the problem using the `--troubleshoot` flag:

            $ {command} VM_NAME --zone=ZONE --troubleshootz [--tunnel-through-iap]z�

          The troubleshoot flag runs tests and returns recommendations for the following types of issues:
          - VM statusz!
          - Network connectivityzb
          - User permissions
          - Virtual Private Cloud (VPC) settings
          - VM bootze

          If you specify the `--tunnel-through-iap` flag, the tool also checks IAP port forwarding.z
          )r�
SupportIAP�SupportNetworkConnectivityTest)�	help_texts r �TroubleshootHelpr<�s���>�)�����
�.�.�I����)��0�0�2�
�$�$�I����)�����
�g�g�I�
��
�r"�
release_track�project_name�	zone_name�
instance_name�ssh_key_file�force_key_file_overwrite�returnc��dj||||�}|r|dj|�z
}|r|dz
}|dz
}tj|�}tj�s|Stj|dz�}||zS)z/Generate the recommend message for troubleshot.z3gcloud {0}compute ssh {1} --project={2} --zone={3} z--ssh-key-file={0} z--force-key-file-overwrite �--troubleshootz --tunnel-through-iap)�format�RECOMMEND_MESSAGErr9�_RECOMMEND_IAP)	r=r>r?r@rArB�command�recommend_message�
recommend_iaps	         r �RecommendMessagerL�s���
B�H�H��]�L�)�
�'���$�+�+�L�9�9�G���,�,�G�	�
��'�'�.�.�w�7��	�	 �	 �	"��� �'�'��2I�(I�J�-�	�]�	*�*r"c�<�|jddt���y)NrEr4r%)rr<rs r �AddTroubleshootArgrN�s!������
����r"c��tj�r"|||d�}tjdi|��}|�||||d�}t	j
di|��}|�||||d�}t
jdi|��}	|	�|||d�}
tjdi|
��}|�|||d�}tjdi|��}
|
�y)z)Run each category of troubleshoot action.)�project�zone�instance)rPrQrR�iap_tunnel_argsN�)rr:r�NetworkTroubleshooterr�UserPermissionTroubleshooterr�VPCTroubleshooterr�VMStatusTroubleshooterr�VMBootTroubleshooter)rPrQrRrS�network_args�network�user_permission_args�user_permission�vpc_args�vpc�vm_status_args�	vm_status�vm_boot_args�vm_boots              r �RunTroubleshootingrd�s����0�0�2�����L�
%�:�:�J�\�J�G��I����(�	��3�O�O����/������(�	�(�	�,�,�8�x�8�#��%�����.�
'�=�=�O��O�)��+�����,�

#�7�7�G�,�G�'�	�)r"c�H�eZdZdZej
ZdZed��Z	d�Z
d�Zy)�Sshz$SSH into a virtual machine instance.Fc���tjj|�t|�t	|�t|�t
|�tj�rtj|�tj|dd��tj|�|j�}t|�tj�rtj ||�yy)z\Set up arguments for this command.

    Args:
      parser: An argparse.ArgumentParser.
    rRz
connect to)�
resource_type�operation_typeN)r
�BaseSSHCLIHelper�Argsr!r0r2rNrr9r
�AddHostBasedTunnelArgsr	�AddZoneFlag�AddVerifyInternalIpArg�add_mutually_exclusive_groupr7�AddSshTunnelArgs)�clsr�
routing_groups   r rkzSsh.Argss������#�#�F�+��&���v���F���v����� ��'�'��/�	����j��G�
�$�$�V�,��7�7�9�M��]�#���� ��!�!�&�-�8�!r"c��|jd�xr|jd�}|rd|_tj|j	��}|j
}t
j�}|j|�d}|r�t
j|j�\}}tj||�}	tj�r%tj||j	�|�}|}
|}tj �}�n t
j|j�\}}
t"j$j'|
gt(j*j,|j.|j0t#j2|���d}|j5||�}|j7||j8�}|j:dk(rd}n|j=||||�}tj�r;tj>||j	�|t
j@|d���}t
jB|�}|jDr�tFjHjKtLjO||j.xs|j.tPjPjS���tU||j.xs|j.||�y|s|�tGjVd	�t
jX|�\}}|jrtj d
��}n�|jZj]�j_d��}d
|jv}tj`||||||j	�||j
jb��}|jd}tGjV|�|r|}
t
jf|�}n(|jhr|}
|
}nt
jj|�}
|
}tj||�}	tjl|�}d}d}d}|jsI|s|jZjn}|jqt
jf�|j:��}|jrs|jtr5tjvjxj.jz�}tj|||	|
|�}g}|j~r|j�|j~�|j�r|j�j�d�nd}t�j�|j�|�}t�j�|j�|�}tj�|	|||||||||��
} |j�rOtFj�jKdj�d�| j�|j��D���y|j�rt
j�|�|jxrt�d�|D��}!|!xrA|j�xr2t�j�jj�j��}"|js|j�rd
}#n |j�||	jd��}#|#rWt
j�|	||||��}$tFjHjKd�	|$j�|j�|"��|jhr!|s|j�jz|	|||"�	| j|j�|"��}%|r5tj�jxj.jz�|%rt�j�|%�yy#t�j�$rt
j���wxYw#tj�$r:}&|s1tFjHjK|j�|
��|&�d}&~&wwxYw)z$See ssh_utils.BaseSSHCLICommand.Run.r[�regionTN)�scope_listerr�no)�no_raisez@Unable to retrieve host keys from instance metadata. Continuing.F)�oslogin_enabled)�include_comment�@)�username_requested�messages)�host_keys_to_add� )
�remote�
identity_file�	cert_file�options�extra_flags�remote_command�ttyrS�	remainder�
identity_listc3�JK�|]}d|vrdj|�n|���y�w)r~z"{0}"N)rF)�.0�args  r �	<genexpr>zSsh.Run.<locals>.<genexpr>�s-����C�(A��58�3�J�W�^�^�C�0�C�G�(A�s�!#c3�LK�|]}|dk(xs|jd����y�w)z-iz-i=N)�
startswith)r��fs  r r�zSsh.Run.<locals>.<genexpr>�s&����J�k��A��I�4����e�!4�4�k�s�"$)�
expiration)r�z!Waiting for SSH key to propagate.)�putty_force_connect)b�IsKnownAndSpecified�plainr�ComputeApiHolder�ReleaseTrack�clientr
rj�Run�GetUserAndInstancer'r�Remoter9r
�CreateOnPremSshTunnelArgs�OsloginState�instance_flags�SSH_INSTANCE_RESOLVER�ResolveResources�
compute_scope�	ScopeEnum�ZONErQ�	resources�GetInstanceZoneScopeLister�GetInstance�
GetProjectrP�strict_host_key_checking�GetHostKeysFromGuestAttributes�CreateSshTunnelArgs�GetExternalInterface�GetInternalIPAddress�troubleshootr�status�Print�TROUBLESHOOT_HEADERrF�datetime�nowrd�debug�GetSSHKeyExpirationFromArgs�keys�GetPublicKey�ToEntry�GetOsloginStater|�user�HostKeyAlias�internal_ip�GetExternalIPAddress�WriteSecurityKeys�key_file�	GetConfig�third_party_user�require_certificates�CertFileFromComputeInstance�name�id�ParseAndSubstituteSSHFlagsr*�extendrI�splitr�GetTty�	container�GetRemoteCommand�
SSHCommand�dry_run�out�join�Build�env�enable_security_keys�ConfirmSecurityKeyStatus�any�oslogin_2fa_enabledr�VALUESr��GetBoolrx�EnsureSSHKeyExists�CreateSSHPoller�Pollr�
WaitException�NetworkError�PreliminarilyVerifyInstance�CommandError�createRecommendMessage�DeleteCertificateFile�sys�exit)'�self�args�on_prem�holderr��
ssh_helperrSr��ipr�instance_address�internal_address�
oslogin_stater@�instance_refrRrP�	host_keysr��expiration_micros�
public_keyr{�	dest_addr�identity_file_listr�r�r�r�r��command_listr�r��cmd�prompt_for_passwordr��keys_newly_added�poller�return_code�es'                                       r r�zSsh.Run)sQ��	
� � ��+�	+�� � ��*����d�j��
*�
*�4�+<�+<�+>�
?�F�
�]�]�F��+�+�-�J��N�N�4���O���-�-�d�n�n�=�h�d�B��z�z�"�d�#�f�	�	 �	 �	"�$�>�>��$�#�#�%�r�
�������&�&�(�m�%�8�8����H��d�M�#�9�9�J�J��/�=�2�2�7�7����
�
�
�%�@�@��H�K�J�KL�M�l��'�'���=�h��%�%�f�l�.B�.B�C�g�	
�	&�	&�$�	.��	��=�=��L�(�G�5�	�
�	 �	 �	"�$�8�8��������*�*�8�d�C�	
��#�7�7��A��	
�	�	��
�
���,�3�3��$�)�)�8�|�'8�'8����!�!�#�
�	�	�7�D�I�I�$B��1B�1B�#�_�	6��
�9�0��	�	� �	!�&/�&K�&K�
�'�#�j�#�
����(�(��?�
��_�_�1�1�3�;�;� �<�"�
�!�D�N�N�2���+�+����������1��]�]�+�+�-�
��!�!��	�i�i�
��	�,���*�*�8�4�	����+��$�	�$�9�9�(�C��$�	��z�z�)�T�*�f��.�.�}�=���M��I��G��:�:�
�"���0�0�
��$�$�
�
 �
 ��
*�
�
'�
'�$�%��g��%�%��)K�)K��1�1�
�,�,��)�)�8�;�;��i��0�0��f�&�(8��K��I��}�}����t�}�}�%�/3�l�l�4�<�<�%�%�c�*��L�
�
�
�D�N�N�L�
9�C��0�0�����N�N��.�.��#����%��'��(��C��|�|�	�g�g�m�m�C�H�H�C�(+�	�	�*�.�.�(A�C�C�D��
� � ��(�(��7�	
�
�
�	K��J�k�J�J�J��
 ��	@��1�1�1�	@����!�!�5�5�=�=�?��
�z�z�]�2�2���#�6�6�
�&�+�+�x��Z�7�I����(�(����)8�5@�B�f�
�j�j���:�;�'�����N�N� 3�	�	5������,�,�X�[�[�&�-�-4�6I�K���G�G�
�.�.�1��3�k��	������l�.?�.?����M��
�h�h�{����+�
 �
 �'��$�$�&�&�'������
��
�
����4�4�T�=�5A�7�L�	M�
�g��	�s$�_	�_4�	(_1�4a�5`<�<ac��tjt|j��j��}|r|dznd}|jxs|j}|j
}t
|||||j|j�S)Nr~�)	r��get�str�lowerrQr�rLrArB)r�r�r@r�rPr=r?r>s        r r�zSsh.createRecommendMessages�� �$�$�S��):�):�)<�%=�%C�%C�%E�F�M�+8�M�C�'�b�M��	�	�.�\�.�.�I��<�<�L����������%�%�
�r"N)�__name__�
__module__�__qualname__�__doc__r�TOOLS_CATEGORY�categoryr��classmethodrkr�r�rTr"r rfrf	s6��-�
�
 �
 �(����9��9�.V�pr"rfc��eZdZdZdZy)�SshAlphaBetaz+SSH into a virtual machine instance (Beta).TN)r�r�r�r�r�rTr"r rrs
��4��r"rc��dddd�}|S)z7Construct help text based on the command release track.z#SSH into a virtual machine instancea,*{command}* is a thin wrapper around the *ssh(1)* command that
takes care of authentication and the translation of the
instance name into an IP address.

To use SSH to connect to a Windows VM, refer to this guide:
https://cloud.google.com/compute/docs/connect/windows-ssh

The default network comes preconfigured to allow ssh access to
all VMs. If the default network was edited, or if not using the
default network, you may need to explicitly enable ssh access by adding
a firewall-rule:

  $ gcloud compute firewall-rules create --network=NETWORK default-allow-ssh --allow=tcp:22

*{command}* ensures that the user's public SSH key is present
in the project's metadata. If the user does not have a public
SSH key, one is generated using *ssh-keygen(1)* (if the `--quiet`
flag is given, the generated key will have an empty passphrase).

If the `--region` and `--network` flags are provided, then `--plain` and
`--tunnel-through-iap` are implied and an IP address must be supplied instead of
an instance name. This is most useful for connecting to on-prem resources.
a�To SSH into 'example-instance' in zone ``us-central1-a'', run:

  $ {command} example-instance --zone=us-central1-a

You can also run a command on the virtual machine. For
example, to get a snapshot of the guest's process tree, run:

  $ {command} example-instance --zone=us-central1-a --command="ps -ejH"

When running a command on a virtual machine, a non-interactive shell will
typically be used. (See the INVOCATION section of
https://linux.die.net/man/1/bash for an overview.) That behavior can be
overridden by specifying a shell to run the command, and passing the `-t` flag
to SSH to allocate a pseudo-TTY. For example, to see the environment variables
set during an interactive session, run:

  $ {command} example-instance --zone=us-central1-a --command="bash -i -c env" -- -t

If you are using the Google Container-Optimized virtual machine image,
you can SSH into one of your containers with:

  $ {command} example-instance --zone=us-central1-a --container=CONTAINER

You can limit the allowed time to ssh. For example, to allow a key to be
used through 2019:

  $ {command} example-instance --zone=us-central1-a --ssh-key-expiration="2020-01-01T00:00:00:00Z"

Or alternatively, allow access for the next two minutes:

  $ {command} example-instance --zone=us-central1-a --ssh-key-expire-after=2m

To use the IP address of your remote VM (eg, for on-prem), you must also specify
the `--region` and `--network` flags:

  $ {command} 10.1.2.3 --region=us-central1 --network=default
)�brief�DESCRIPTION�EXAMPLESrT)�
detailed_helps r �
_DetailedHelprs"��5��0%�5@�-�D
�r")NNNN):r��
__future__rrrr.r�r��googlecloudsdk.api_lib.computer�googlecloudsdk.callioper�"googlecloudsdk.command_lib.computerr	r
rrr�r
rrrr�,googlecloudsdk.command_lib.compute.instancesr��#googlecloudsdk.command_lib.util.sshrr�googlecloudsdk.corerr�googlecloudsdk.core.utilrrGrHr�r�r!r0r2r7r<r��boolrLrNrd�UniverseCompatible�
ReleaseTracks�GA�Commandrf�ALPHA�BETArrrrTr"r �<module>rs��� :�&��'���
�7�(�9�4�9�E�E�8�M�E�G�A�P�:�3�#�*�*���������
���.�b�
�(�6+��+��+��+��	+�
�+�#�
+�	�+�0�:>�'+�-�`�������D�%�%�(�(�)�B�$�,�,�B�*��B�J�������D�%�%�+�+�T�->�->�-C�-C�D��3��E���
D�N+�_���!�O��r"