
    {S                     
   S r SSKJr  SSKJr  SSKJr  SSKrSSKrSSKrSSKJ	r	  SSK
Jr  SSKJr  SS	KJr  SS
KJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKJr  SS	KJr  SSKJr  SSKJr  SSKJr  SSKJr  SSK J!r!  Sr"Sr#SSS.r$Sr%S r&S r'S r(S  r)S! r*S"\+S#\+S$\+S%\+S&\+S'\,S(\+4S) jr-S* r.  S1S+ jr/\R`                  \Rb                  " \RH                  Rd                  5       " S, S-\Rf                  5      5       5       r4\R`                  \Rb                  " \RH                  Rj                  \RH                  Rl                  5       " S. S/\45      5       5       r7S0 r8\8" 5       \7l9        \8" 5       \4l9        g)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)r   r   zK
Starting ssh troubleshooting for instance {0} in zone {1}
Start time: {2}
c                 $    U R                  SSS9  g )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!   D   s    
      c                     U R                  SSSS9  U R                  S[        R                  SSS9  U R                  S	[        R                  S
SS9  g)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)r   r   InstancesCompleterargparse	REMAINDERr   s    r    
AddSSHArgsr0   N   st    
  * 	--	
	   	
  r"   c                 $    U R                  SSS9  g )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).
          r   r   r   s    r    AddContainerArgr2      s      r"   c                 (    U R                  SSSSS9  g )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&   r   r   )groups    r    AddInternalIPArgr7      s#    	  r"   c                      Sn [         R                  " 5       (       a  U S-  n U S-  n [         R                  " 5       (       a  U S-  n U S-  n [         R                  " 5       (       a  U S-  n U S-   $ )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2 $ $I  )  g gI 
  
 r"   release_trackproject_name	zone_nameinstance_namessh_key_fileforce_key_file_overwritereturnc                    SR                  XX5      nU(       a  USR                  U5      -  nU(       a  US-  nUS-  n[        R                  U5      n[        R                  " 5       (       d  U$ [        R                  US-   5      nXx-   $ )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_MESSAGEr   r9   _RECOMMEND_IAP)	r=   r>   r?   r@   rA   rB   commandrecommend_messagerecommend_iaps	            r    RecommendMessagerL      s     BHHL' $++L99G,,G	''..w7		 	 	"	" ''2I(IJ-		**r"   c                 6    U R                  SS[        5       S9  g )NrE   r4   r%   )r   r<   r   s    r    AddTroubleshootArgrN      s!      r"   c                    [         R                  " 5       (       a#  U UUS.n[        R                  " S0 UD6nU" 5         U UUUS.n[        R
                  " S0 UD6nU" 5         U UUUS.n[        R                  " S0 UD6n	U	" 5         U UUS.n
[        R                  " S0 U
D6nU" 5         U UUS.n[        R                  " S0 UD6nU" 5         g)z)Run each category of troubleshoot action.)projectzoneinstance)rP   rQ   rR   iap_tunnel_argsN )r   r:   r   NetworkTroubleshooterr   UserPermissionTroubleshooterr   VPCTroubleshooterr   VMStatusTroubleshooterr   VMBootTroubleshooter)rP   rQ   rR   rS   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2L
 %::J\JGI (	 3OO / (	( 	,,8x8#% .
 '==OO)+ ,
 #77G,G'	)r"   c                   P    \ rS rSrSr\R                  rSr\	S 5       r
S rS rSrg)	Sshi	  z$SSH into a virtual machine instance.Fc                    [         R                  R                  U5        [        U5        [	        U5        [        U5        [        U5        [        R                  " 5       (       a  [        R                  " U5        [        R                  " USSS9  [         R                  " U5        UR                  5       n[        U5        [        R                  " 5       (       a  [        R                   " X5        gg)zPSet up arguments for this command.

Args:
  parser: An argparse.ArgumentParser.
rR   z
connect to)resource_typeoperation_typeN)r   BaseSSHCLIHelperArgsr!   r0   r2   rN   r   r9   r
   AddHostBasedTunnelArgsr	   AddZoneFlagAddVerifyInternalIpArgadd_mutually_exclusive_groupr7   AddSshTunnelArgs)clsr   routing_groups      r    rk   Ssh.Args  s     ##F+&vFv  ''/	jG$$V,779M]#  !!&8 !r"   c                    UR                  S5      =(       a    UR                  S5      nU(       a  SUl        [        R                  " U R	                  5       5      nUR
                  n[        R                  " 5       nUR                  U5        SnU(       a  [        R                  " UR                  5      u  px[        R                  " X5      n	[        R                  " 5       (       a%  [        R                  " XR	                  5       U5      nUn
Un[        R                   " 5       nGOD[        R                  " UR                  5      u  p}["        R$                  R'                  U/[(        R*                  R,                  UR.                  UR0                  ["        R2                  " U5      S9S   nUR5                  XN5      nUR7                  XNR8                  5      nUR:                  S:X  a  SnOUR=                  XNUU5      n[        R                  " 5       (       a:  [        R>                  " UU R	                  5       U[        R@                  " USS95      n[        RB                  " U5      nURD                  (       a  [F        RH                  RK                  [L        RO                  XR.                  =(       d    UR.                  [P        RP                  RS                  5       5      5        [U        UUR.                  =(       d    UR.                  X5        gU(       d  Ub  [F        RV                  " S	5        [        RX                  " U5      u  nnUR                  (       a  [        R                   " S
S9nOURZ                  R]                  5       R_                  SS9nSUR                  ;   n[        R`                  " UUUUUU R	                  5       UUR
                  Rb                  S9nURd                  n[F        RV                  " U5        U(       a  Un
[        Rf                  " U5      nO.URh                  (       a  Un
U
nO[        Rj                  " U5      n
U
n[        R                  " UU5      n	[        Rl                  " U5      nSnSnSnUR                  (       dL  U(       d  URZ                  Rn                  nURq                  [        Rf                  " W5      UR:                  WS9nURr                  (       d  URt                  (       a6  [        Rv                  " WRx                  WR.                  WRz                  5      n[        R|                  " XX5      n/ nUR~                  (       a  UR                  UR~                  5        UR                  (       a  UR                  R                  S5      OSn[        R                  " UR                  U5      n[        R                  " UR                  U5      n[        R                  " U	UUUUUUUUUS9
n UR                  (       aO  [F        R                  RK                  SR                  S U R                  UR                  5       5       5      5        gU R                  (       a  [        R                  " U5        UR                  =(       a    [        S U 5       5      (       + n!U!(       + =(       aJ    UR                  (       + =(       a2    [        R                  R                  R                  R                  5       n"UR                  (       d  UR                  (       a  S
n#OUR                  XIRd                  WWWS9n#U#(       aR  [        R                  " U	UUUUS9n$[F        RH                  RK                  S5         U$R                  UR                  U"S9  URh                  (       a&  U(       d  UR                  WRz                  U	UUU"5         U R                  UR                  U"S9n%U(       a6  [        R                  " WRx                  WR.                  WRz                  5        U%(       a  [        R                  " U%5        gg! [        R                   a    [        R                  " 5       ef = f! [        R                   a?  n&U(       d1  [F        RH                  RK                  U R                  UWWW5      5        U&eSn&A&ff = f)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_listc              3   T   #    U  H  nS U;   a  SR                  U5      OUv   M      g7f)r   z"{0}"N)rF   ).0args     r    	<genexpr>Ssh.Run.<locals>.<genexpr>  s,      C(A 583JW^^C0CG(As   &(c              3   Z   #    U  H!  oS :H  =(       d    UR                  S5      v   M#     g7f)z-iz-i=N)
startswith)r   fs     r    r   r     s#     JkI4e!44ks   )+)
expiration)r   z!Waiting for SSH key to propagate.)putty_force_connect)bIsKnownAndSpecifiedplainr   ComputeApiHolderReleaseTrackclientr   rj   RunGetUserAndInstancer'   r   Remoter9   r
   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   GetBoolry   EnsureSSHKeyExistsCreateSSHPollerPollr   WaitExceptionNetworkErrorPreliminarilyVerifyInstanceCommandErrorcreateRecommendMessageDeleteCertificateFilesysexit)'selfargson_premholderr   
ssh_helperrS   r   ipr   instance_addressinternal_addressoslogin_stater@   instance_refrR   rP   	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   Ssh.Run)  s>    	  + 	+  *  dj **4+<+<+>?F]]F++-JNN4O--dnn=hdzz"#f		 	 	"	"$>>##%r
 &&(m%88Hd#99JJ/=2277


%@@H K J KLMl ''=h%%f.B.BCg		&	&$	.	==(G5	 
	 	 	"	"$88**8dC	
 #77A			

,33))8|'8'8!!#
 	 	7DII$B1B1B#	690		   	!&/&K&K
'#j# 
((?__113;;  < "
 !DNN2++1]]++- !!	ii	 ,**84	+$	$99(C$	zz)T*f ..}=MIG::"00$$

 
 
*

'
'$ % g %%)K)K11
,,))8;;i 00&K I}}t}}% /3ll4<<%%c*L


DNNL
9C00NN ..#%'(C ||	ggmmCHH C(+		*..(AC C D
   ((7 	

 	KJkJJJ    	@111	@!!55==? 
 zz]22#66
++xZ 7 I (()85@Bf 
jj:;'NN 3 	 	5 ,,X[[&--46IKGG
..1  3k 	l.?.?M 
hh{ +    '$$&&'  

44T=5A7L 	Mg	s$   a! +b !*bc!":cc!c                 0   [         R                  [        U R                  5       5      R                  5       5      nU(       a  US-   OSnUR                  =(       d    UR                  nUR
                  n[        UUUUUR                  UR                  5      $ )Nr    )	r   getstrlowerrQ   r   rL   rA   rB   )r   r   r@   r   rP   r=   r?   r>   s           r    r   Ssh.createRecommendMessage  s     $$S):):)<%=%C%C%EFM+8MC'bM		.\..I<<L%% r"   rT   N)__name__
__module____qualname____firstlineno____doc__r   TOOLS_CATEGORYcategoryr   classmethodrk   r   r   __static_attributes__rT   r"   r    rf   rf   	  s6     -  (9 9.Vpr"   rf   c                       \ rS rSrSrSrSrg)SshAlphaBetai  z+SSH into a virtual machine instance (Beta).TrT   N)r   r   r   r  r  r   r  rT   r"   r    r  r    s     4r"   r  c                      SSSS.n U $ )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    _DetailedHelpr    s"     50%5@-D 
r"   )NNNN):r  
__future__r   r   r   r.   r   r   googlecloudsdk.api_lib.computer   googlecloudsdk.callioper   "googlecloudsdk.command_lib.computer   r	   r
   r   r   r   r   r   r   r   r   ,googlecloudsdk.command_lib.compute.instancesr   #googlecloudsdk.command_lib.util.sshr   r   googlecloudsdk.corer   r   googlecloudsdk.core.utilr   rG   rH   r   r   r!   r0   r2   r7   r<   r   boolrL   rN   rd   UniverseCompatibleReleaseTracksGACommandrf   ALPHABETAr  r  r  rT   r"   r    <module>r     s    : &  '   
 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"   