def connect(self):
"""Finish building the client and connects to the target server, returning a paramiko client object"""
assert self._client_class
assert self._hostname is not None, 'destination hostname was not specified'
client = self._client_class()
if self._missing_host_key_policy:
client.set_missing_host_key_policy(self._missing_host_key_policy())
config_data = self._config.lookup(self._hostname)
ssh_kwargs = {
'timeout': self._timeout,
'banner_timeout': self._banner_timeout,
'port': self._port
}
# unless one is explicitely specified with .user(), get our username from configuration, defaulting to $USER
if self._username is None:
ssh_kwargs['username'] = config_data.get('user', os.getenv('USER'))
else:
ssh_kwargs['username'] = self._username
if self._password is not None:
ssh_kwargs['password'] = self._password
if 'proxycommand' in config_data:
ssh_kwargs['sock'] = paramiko.ProxyCommand(config_data['proxycommand'])
elif self._proxy_command is not None:
ssh_kwargs['sock'] = paramiko.ProxyCommand(self._proxy_command)
if config_data.get('identity_file') is not None:
ssh_kwargs['key_filename'] = config_data.get('identity_file')
# unless explicitely specified with .allow_agent, allow agent by default unless identitiesonly is yes in our config
if self._allow_agent is None:
ssh_kwargs['allow_agent'] = config_data.get('identitiesonly', 'no') != 'yes'
else:
ssh_kwargs['allow_agent'] = self._allow_agent
if self._sock is not None:
ssh_kwargs['sock'] = self._sock
logging.debug('Connecting to %s with options %s', config_data['hostname'], ssh_kwargs)
client.connect(config_data['hostname'], **ssh_kwargs)
return client
评论列表
文章目录