Skip to content

Commit c882766

Browse files
committed
host: add method to validate ssh connection
We need a way to check if the connection is still up since we last ran ssh_connect. host.run will re-establish the connection for us, but we may want to know for other reasons whether the connection has yet been established. Signed-off-by: Salvatore Daniele <[email protected]>
1 parent f32620c commit c882766

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

host.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,29 @@ def ssh_connect_looped(self, logins: list[Login], timeout: float = 3600) -> None
190190

191191
raise ConnectionError(f"Failed to establish an SSH connection to {self._hostname}")
192192

193+
def is_connected(self) -> bool:
194+
if self.is_localhost():
195+
return True
196+
try:
197+
assert self._host is not None
198+
_, stdout, stderr = self._host.exec_command("echo 'connection check'")
199+
200+
out = []
201+
for line in iter(stdout.readline, ""):
202+
logger.log(logging.DEBUG, f"{self._hostname}: {line.rstrip()}")
203+
out.append(line)
204+
205+
err = []
206+
for line in iter(stderr.readline, ""):
207+
err.append(line)
208+
209+
exit_code = stdout.channel.recv_exit_status()
210+
211+
return exit_code == 0
212+
except Exception as e:
213+
logger.log(logging.DEBUG, e)
214+
return False
215+
193216
def _rsa_login(self) -> Optional[KeyLogin]:
194217
for x in self._logins:
195218
if isinstance(x, KeyLogin) and x._is_rsa():

0 commit comments

Comments
 (0)