Example #1
0
 def get_client(self,
                host,
                username=None,
                port=None,
                proxy_jump=None,
                host_config=None,
                config_files=None,
                proxy_client=None,
                **connect_parameters):
     if isinstance(host, netaddr.IPAddress):
         host = str(host)
     host_config = host_config or _config.ssh_host_config(
         host=host, config_files=config_files)
     hostname = host_config.hostname
     port = port or host_config.port
     username = username or host_config.username
     host_key = hostname, port, username, proxy_jump
     client = self.clients.get(host_key, UNDEFINED_CLIENT)
     if client is UNDEFINED_CLIENT:
         # Put a placeholder client to avoid infinite recursive lookup
         self.clients[host_key] = None
         proxy_client = proxy_client or self.get_proxy_client(
             host=host, proxy_jump=proxy_jump, config_files=config_files)
         self.clients[host_key] = client = SSHClientFixture(
             host=host,
             hostname=hostname,
             port=port,
             username=username,
             proxy_client=proxy_client,
             host_config=host_config,
             **connect_parameters)
     return client
Example #2
0
 def get_proxy_client(self,
                      host=None,
                      proxy_jump=None,
                      host_config=None,
                      config_files=None):
     if isinstance(proxy_jump, SSHClientFixture):
         return proxy_jump
     host_config = host_config or _config.ssh_host_config(
         host=host, config_files=config_files)
     proxy_jump = host_config.proxy_jump
     return proxy_jump and self.get_client(proxy_jump) or None
Example #3
0
def ssh_command(host,
                username=None,
                port=None,
                command=None,
                config_files=None,
                host_config=None,
                proxy_command=None,
                key_filename=None,
                **options):
    host_config = host_config or _config.ssh_host_config(
        host=host, config_files=config_files)

    command = command or host_config.default.command.split()
    if isinstance(command, six.string_types):
        command = command.split()

    hostname = host_config.hostname
    username = username or host_config.username
    command += [ssh_login(hostname=hostname, username=username)]

    #     if host_config.default.debug:
    #         command += ['-vvvvvv']

    port = port or host_config.port
    if port:
        command += ['-p', str(port)]

    if key_filename:
        command += ['-i', key_filename]

    if proxy_command:
        if not isinstance(proxy_command, six.string_types):
            proxy_command = subprocess.list2cmdline(
                [str(a) for a in proxy_command])
        options['ProxyCommand'] = proxy_command

    for name, value in host_config.host_config.items():
        if name not in {'hostname', 'port', 'user'}:
            options.setdefault(name, value)
    options.setdefault('UserKnownHostsFile', '/dev/null')
    options.setdefault('StrictHostKeyChecking', 'no')
    options.setdefault('LogLevel', 'quiet')
    options.setdefault('ConnectTimeout', int(host_config.timeout))
    options.setdefault('ConnectionAttempts', host_config.connection_attempts)
    if options:
        for name, value in sorted(options.items()):
            name = name.replace('_', '')
            command += ['-o', '{!s}={!s}'.format(name, value)]

    return command
Example #4
0
    def get_client(self, host, hostname=None, username=None, port=None,
                   proxy_jump=None, host_config=None, config_files=None,
                   proxy_client=None, **connect_parameters) -> \
            SSHClientFixture:
        if isinstance(host, netaddr.IPAddress):
            host = str(host)

        if host_config:
            hostname = hostname or host_config.hostname
            port = port or host_config.port
            username = username or host_config.username

        global_host_config = _config.ssh_host_config(host=host,
                                                     config_files=config_files)
        hostname = hostname or global_host_config.hostname
        port = port or global_host_config.port
        username = username or global_host_config.username

        host_key = hostname, port, username, proxy_jump
        existing_client = self.clients.get(host_key)
        if isinstance(existing_client, SSHClientFixture):
            return existing_client

        # Put a placeholder to avoid infinite recursive lookup
        if existing_client is UNDEFINED_CLIENT:
            raise RuntimeError('Recursive SSH proxy client definition')
        self.clients[host_key] = UNDEFINED_CLIENT

        proxy_client = proxy_client or self.get_proxy_client(
            host=host, proxy_jump=proxy_jump, config_files=config_files)
        self.clients[host_key] = new_client = SSHClientFixture(
            host=host,
            hostname=hostname,
            port=port,
            username=username,
            proxy_client=proxy_client,
            host_config=host_config,
            **connect_parameters)
        return new_client
Example #5
0
 def setup_global_host_config(self):
     self.global_host_config = config = _config.ssh_host_config(
         host=self.host, config_files=self.config_files)
     return config
Example #6
0
 def setup_host_config(self):
     if not self.host_config:
         self.host_config = _config.ssh_host_config(
             host=self.host, config_files=self.config_files)
     return self.host_config