def __init__(self) -> None: """Create a new instance of this SSH LabHost.""" super().__init__() self.client = paramiko.SSHClient() self._c: typing.Dict[str, typing.Union[str, typing.List[str]]] = {} try: c = paramiko.config.SSHConfig() c.parse(open(pathlib.Path.home() / ".ssh" / "config")) self._c = c.lookup(self.hostname) except FileNotFoundError: # Config file does not exist pass except Exception: # Invalid config log.message(log.c("Invalid").red + " .ssh/config") raise if self.ignore_hostkey: self.client.set_missing_host_key_policy( paramiko.client.AutoAddPolicy()) else: self.client.load_system_host_keys() password = None key_file = None authenticator = self.authenticator if isinstance(authenticator, auth.PasswordAuthenticator): password = authenticator.password if isinstance(authenticator, auth.PrivateKeyAuthenticator): key_file = str(authenticator.key_file) log.message( "Logging in on " + log.c(f"{self.username}@{self.hostname}:{self.port}").yellow + " ...", verbosity=log.Verbosity.COMMAND, ) if "hostname" in self._c: hostname = str(self._c["hostname"]) else: hostname = self.hostname self.client.connect( hostname, username=self.username, port=self.port, password=password, key_filename=key_file, ) self.channel = channel.ParamikoChannel( self.client.get_transport().open_session())
def _new_channel(self) -> channel.Channel: return channel.ParamikoChannel( self.client.get_transport().open_session())