Example #1
0
    def _connection_settings(self, vm=None, **ssh_config_overrides):
        host = 'vagrant-temporary-%s' % self.uuid(vm=vm)

        # Extract current SSH settings
        self.up(vm=vm)
        ssh_settings = self.ssh_config(vm=vm)
        ssh_settings.update({'host': host, 'stricthostkeychecking': 'no'})
        ssh_settings.update(ssh_config_overrides)

        # Ensure that SSH config is being picked up and update it with the
        # connection settings for the vagrant box
        with settings(use_ssh_config=True):
            from fabric.network import ssh_config
            ssh_config('test')  # ensure that ssh config is in use and cached
            modified_config = copy.deepcopy(env._ssh_config)
            modified_config._config.append(ssh_settings)

        return {
            'use_ssh_config': True,
            'host': host,
            'host_string': '%(user)s@%(host)s:%(port)s' % ssh_settings,
            '_ssh_config': modified_config,
            'cwd': '',
            'path': ''
        }
Example #2
0
    def _connection_settings(self, vm=None, **ssh_config_overrides):
        host = 'vagrant-temporary-%s' % self.uuid(vm=vm)

        # Extract current SSH settings
        self.up(vm=vm)
        ssh_settings = self.ssh_config(vm=vm)
        ssh_settings.update({
            'host': host,
            'stricthostkeychecking': 'no'
            })
        ssh_settings.update(ssh_config_overrides)

        # Ensure that SSH config is being picked up and update it with the
        # connection settings for the vagrant box
        with settings(use_ssh_config=True):
            from fabric.network import ssh_config
            ssh_config('test')  # ensure that ssh config is in use and cached
            modified_config = copy.deepcopy(env._ssh_config)
            modified_config._config.append(ssh_settings)

        return {
            'use_ssh_config': True,
            'host': host,
            'host_string': '%(user)s@%(host)s:%(port)s' % ssh_settings,
            '_ssh_config': modified_config,
            'cwd': '',
            'path': ''
            }
Example #3
0
def _check_if_using_the_correct_account():
    # Ensure that the user we use to log in has the right credentials
    # OpenVMS' SSH2 doesn't handle well the connections where both a wrong
    # pkey and a valid password are given with paramiko under the hoods.
    # This is an issue with paramiko as of 1.16 (see related Issue#519)
    with settings(use_ssh_config=True):
        if 'user' in ssh_config() and 'user' in env:
            if ssh_config(env.host_string)['user'].upper() != env.user.upper():
                # Avoid using private keys if user doesn't match env.user
                puts('Avoid using ssh_config')
                env.use_ssh_config = False
        else:
            puts('Not using ssh_config:\n {}'
                 .format(ssh_config(env.host_string)))
            env.use_ssh_config = False
Example #4
0
def this_hostname(hostname):
    """
    Context manager that uses the current SSH confg to switch Fabric to a specific hostname.

    Updates hostname and port.
    """
    host_config = ssh_config(hostname)

    host_string = hostname
    port = host_config.get("port", env.default_port)

    with settings(host_string=host_string,
                  port=port):
        yield
Example #5
0
def main():
    parser = argparse.ArgumentParser(description="Run youtube-dl on a remote host and bring the resulting file back here.", add_help=False)
    parser.add_argument('-h', '--host', action='store', required=False)
    parser.add_argument('--verbose', action='store_true', required=False)
    parser.add_argument('--save-default', action='store_true', required=False)
    parser.add_argument('url', action='store', nargs='?', default=None)

    args = parser.parse_args()
    xargs = args
   
    host = validate_host(args)

    if args.url is None:
        parser.print_help()
        sys.exit(1)

    env.use_ssh_config = True
    env.host_string = host
    
    if not args.verbose:
        fabric_output['stdout'] = False
        fabric_output['stderr'] = False
        fabric_output['status'] = False
        fabric_output['aborts'] = False
        fabric_output['warnings'] = False
        fabric_output['running'] = False

    #XXX: does every ssh config actually need a user? (shrug)
    if ssh_config().get('user') is None:
        output('Host {} does not appear to be configured in your ssh config file, it must be configured for {} to work.'.format(host, PROGRAM))
        sys.exit(1)

    try:
        download(args.url)
    except ProgramNotFound:
        output('Host {} does not appear to have youtube-dl installed. Please install it.'.format(host))
    except YoutubeDLError as e:
        output(e.message)
Example #6
0
def _execute(channel, command, pty=True, combine_stderr=None,
    invoke_shell=False, stdout=None, stderr=None, timeout=None):
    """
    Execute ``command`` over ``channel``.

    ``pty`` controls whether a pseudo-terminal is created.

    ``combine_stderr`` controls whether we call ``channel.set_combine_stderr``.
    By default, the global setting for this behavior (:ref:`env.combine_stderr
    <combine-stderr>`) is consulted, but you may specify ``True`` or ``False``
    here to override it.

    ``invoke_shell`` controls whether we use ``exec_command`` or
    ``invoke_shell`` (plus a handful of other things, such as always forcing a
    pty.)

    Returns a three-tuple of (``stdout``, ``stderr``, ``status``), where
    ``stdout``/``stderr`` are captured output strings and ``status`` is the
    program's return code, if applicable.
    """
    # stdout/stderr redirection
    stdout = stdout or sys.stdout
    stderr = stderr or sys.stderr

    # Timeout setting control
    timeout = env.command_timeout if (timeout is None) else timeout

    # What to do with CTRl-C?
    remote_interrupt = env.remote_interrupt

    with char_buffered(sys.stdin):
        # Combine stdout and stderr to get around oddball mixing issues
        if combine_stderr is None:
            combine_stderr = env.combine_stderr
        channel.set_combine_stderr(combine_stderr)

        # Assume pty use, and allow overriding of this either via kwarg or env
        # var.  (invoke_shell always wants a pty no matter what.)
        using_pty = True
        if not invoke_shell and (not pty or not env.always_use_pty):
            using_pty = False
        # Request pty with size params (default to 80x24, obtain real
        # parameters if on POSIX platform)
        if using_pty:
            rows, cols = _pty_size()
            channel.get_pty(width=cols, height=rows)

        # Use SSH agent forwarding from 'ssh' if enabled by user
        config_agent = ssh_config().get('forwardagent', 'no').lower() == 'yes'
        forward = None
        if env.forward_agent or config_agent:
            forward = ssh.agent.AgentRequestHandler(channel)

        # Kick off remote command
        if invoke_shell:
            channel.invoke_shell()
            if command:
                channel.sendall(command + "\n")
        else:
            channel.exec_command(command=command)

        # Init stdout, stderr capturing. Must use lists instead of strings as
        # strings are immutable and we're using these as pass-by-reference
        stdout_buf, stderr_buf = [], []
        if invoke_shell:
            stdout_buf = stderr_buf = None

        workers = (
            ThreadHandler('out', output_loop, channel, "recv",
                capture=stdout_buf, stream=stdout, timeout=timeout),
            ThreadHandler('err', output_loop, channel, "recv_stderr",
                capture=stderr_buf, stream=stderr, timeout=timeout),
            ThreadHandler('in', input_loop, channel, using_pty)
        )

        if remote_interrupt is None:
            remote_interrupt = invoke_shell
        if remote_interrupt and not using_pty:
            remote_interrupt = False

        while True:
            if channel.exit_status_ready():
                break
            else:
                # Check for thread exceptions here so we can raise ASAP
                # (without chance of getting blocked by, or hidden by an
                # exception within, recv_exit_status())
                for worker in workers:
                    worker.raise_if_needed()
            try:
                time.sleep(ssh.io_sleep)
            except KeyboardInterrupt:
                if not remote_interrupt:
                    raise
                channel.send('\x03')

        # Obtain exit code of remote program now that we're done.
        status = channel.recv_exit_status()

        # Wait for threads to exit so we aren't left with stale threads
        for worker in workers:
            worker.thread.join()
            worker.raise_if_needed()

        # Close channel
        channel.close()
        # Close any agent forward proxies
        if forward is not None:
            forward.close()

        # Update stdout/stderr with captured values if applicable
        if not invoke_shell:
            stdout_buf = ''.join(stdout_buf).strip()
            stderr_buf = ''.join(stderr_buf).strip()

        # Tie off "loose" output by printing a newline. Helps to ensure any
        # following print()s aren't on the same line as a trailing line prefix
        # or similar. However, don't add an extra newline if we've already
        # ended up with one, as that adds a entire blank line instead.
        if output.running \
            and (output.stdout and stdout_buf and not stdout_buf.endswith("\n")) \
            or (output.stderr and stderr_buf and not stderr_buf.endswith("\n")):
            print("")

        return stdout_buf, stderr_buf, status
Example #7
0
def _execute(channel, command, pty=True, combine_stderr=None,
    invoke_shell=False, stdout=None, stderr=None, timeout=None):
    """
    Execute ``command`` over ``channel``.

    ``pty`` controls whether a pseudo-terminal is created.

    ``combine_stderr`` controls whether we call ``channel.set_combine_stderr``.
    By default, the global setting for this behavior (:ref:`env.combine_stderr
    <combine-stderr>`) is consulted, but you may specify ``True`` or ``False``
    here to override it.

    ``invoke_shell`` controls whether we use ``exec_command`` or
    ``invoke_shell`` (plus a handful of other things, such as always forcing a
    pty.)

    Returns a three-tuple of (``stdout``, ``stderr``, ``status``), where
    ``stdout``/``stderr`` are captured output strings and ``status`` is the
    program's return code, if applicable.
    """
    # stdout/stderr redirection
    stdout = stdout or sys.stdout
    stderr = stderr or sys.stderr

    # Timeout setting control
    timeout = env.command_timeout if (timeout is None) else timeout

    # What to do with CTRl-C?
    remote_interrupt = env.remote_interrupt

    with char_buffered(sys.stdin):
        # Combine stdout and stderr to get around oddball mixing issues
        if combine_stderr is None:
            combine_stderr = env.combine_stderr
        channel.set_combine_stderr(combine_stderr)

        # Assume pty use, and allow overriding of this either via kwarg or env
        # var.  (invoke_shell always wants a pty no matter what.)
        using_pty = True
        if not invoke_shell and (not pty or not env.always_use_pty):
            using_pty = False
        # Request pty with size params (default to 80x24, obtain real
        # parameters if on POSIX platform)
        if using_pty:
            rows, cols = _pty_size()
            channel.get_pty(width=cols, height=rows)

        # Use SSH agent forwarding from 'ssh' if enabled by user
        config_agent = ssh_config().get('forwardagent', 'no').lower() == 'yes'
        forward = None
        if env.forward_agent or config_agent:
            forward = ssh.agent.AgentRequestHandler(channel)

        # Kick off remote command
        if invoke_shell:
            channel.invoke_shell()
            if command:
                channel.sendall(command + "\n")
        else:
            channel.exec_command(command=command)

        # Init stdout, stderr capturing. Must use lists instead of strings as
        # strings are immutable and we're using these as pass-by-reference
        stdout_buf, stderr_buf = [], []
        if invoke_shell:
            stdout_buf = stderr_buf = None

        workers = (
            ThreadHandler('out', output_loop, channel, "recv",
                capture=stdout_buf, stream=stdout, timeout=timeout),
            ThreadHandler('err', output_loop, channel, "recv_stderr",
                capture=stderr_buf, stream=stderr, timeout=timeout),
            ThreadHandler('in', input_loop, channel, using_pty)
        )

        if remote_interrupt is None:
            remote_interrupt = invoke_shell
        if remote_interrupt and not using_pty:
            remote_interrupt = False

        while True:
            if channel.exit_status_ready():
                break
            else:
                # Check for thread exceptions here so we can raise ASAP
                # (without chance of getting blocked by, or hidden by an
                # exception within, recv_exit_status())
                for worker in workers:
                    worker.raise_if_needed()
            try:
                time.sleep(ssh.io_sleep)
            except KeyboardInterrupt:
                if not remote_interrupt:
                    raise
                channel.send('\x03')

        # Obtain exit code of remote program now that we're done.
        status = channel.recv_exit_status()

        # Wait for threads to exit so we aren't left with stale threads
        for worker in workers:
            worker.thread.join()
            worker.raise_if_needed()

        # Close channel
        channel.close()
        # Close any agent forward proxies
        if forward is not None:
            forward.close()

        # Update stdout/stderr with captured values if applicable
        if not invoke_shell:
            stdout_buf = ''.join(stdout_buf).strip()
            stderr_buf = ''.join(stderr_buf).strip()

        # Tie off "loose" output by printing a newline. Helps to ensure any
        # following print()s aren't on the same line as a trailing line prefix
        # or similar. However, don't add an extra newline if we've already
        # ended up with one, as that adds a entire blank line instead.
        if output.running \
            and (output.stdout and stdout_buf and not stdout_buf.endswith("\n")) \
            or (output.stderr and stderr_buf and not stderr_buf.endswith("\n")):
            print("")

        return stdout_buf, stderr_buf, status