@@ -56,12 +56,26 @@ def _sshpass(self, password):
5656 return []
5757 return ["sshpass" , "-p" , password ]
5858
59+ def _ensure_ssh (self , cmd , user , password = "" , keyfile = None ):
60+ if not self .running ():
61+ self .start ()
62+ for i in range (3 ):
63+ try :
64+ ret , _ = self ._run ("true" , user , password , keyfile )
65+ if ret == 0 :
66+ return
67+ except Exception as e :
68+ print (f"ssh not ready { e } " )
69+ time .sleep (1 )
70+
5971 def run (self , cmd , user , password = "" , keyfile = None ):
72+ self ._ensure_ssh (cmd , user , password , keyfile )
73+ return self ._run (cmd , user , password , keyfile )
74+
75+ def _run (self , cmd , user , password = "" , keyfile = None ):
6076 """
6177 Run a command on the VM via SSH using the provided credentials.
6278 """
63- if not self .running ():
64- self .start ()
6579 ssh_cmd = self ._sshpass (password ) + [
6680 "ssh" , "-p" , str (self ._ssh_port ),
6781 ] + _non_interactive_ssh
@@ -81,8 +95,7 @@ def run(self, cmd, user, password="", keyfile=None):
8195 return p .returncode , output .getvalue ()
8296
8397 def scp (self , src , dst , user , password = "" , keyfile = None ):
84- if not self .running ():
85- self .start ()
98+ self ._ensure_ssh (user , password , keyfile )
8699 scp_cmd = self ._sshpass (password ) + [
87100 "scp" , "-P" , str (self ._ssh_port ),
88101 ] + _non_interactive_ssh
0 commit comments