Пример #1
0
    def _get_conn(self):
        user = self.runner.remote_user

        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

        try:
            ssh.connect(self.host,
                        username=user,
                        allow_agent=True,
                        look_for_keys=True,
                        key_filename=self.runner.private_key_file,
                        password=self.runner.remote_pass,
                        timeout=self.runner.timeout,
                        port=self.port)
        except Exception, e:
            msg = str(e)
            if "PID check failed" in msg:
                raise errors.AnsibleError(
                    "paramiko version issue, please upgrade paramiko on the machine running ansible"
                )
            elif "Private key file is encrypted" in msg:
                msg = 'ssh %s@%s:%s : %s\nTo connect as a different user, use -u <username>.' % (
                    user, self.host, self.port, msg)
                raise errors.AnsibleConnectionFailed(msg)
            else:
                raise errors.AnsibleConnectionFailed(msg)
Пример #2
0
    def connect(self):
        ''' activates the connection object '''

        if not HAVE_PARAMIKO:
            raise errors.AnsibleError("paramiko is not installed")

        user = self.runner.remote_user

        vvv("ESTABLISH CONNECTION FOR USER: %s" % user, host=self.host)

        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

        try:
            ssh.connect(self.host,
                        username=user,
                        allow_agent=True,
                        look_for_keys=True,
                        key_filename=self.runner.private_key_file,
                        password=self.runner.remote_pass,
                        timeout=self.runner.timeout,
                        port=self.port)
        except Exception, e:
            msg = str(e)
            if "PID check failed" in msg:
                raise errors.AnsibleError(
                    "paramiko version issue, please upgrade paramiko on the machine running ansible"
                )
            elif "Private key file is encrypted" in msg:
                msg = 'ssh %s@%s:%s : %s\nTo connect as a different user, use -u <username>.' % (
                    user, self.host, self.port, msg)
                raise errors.AnsibleConnectionFailed(msg)
            else:
                raise errors.AnsibleConnectionFailed(msg)
Пример #3
0
    def _connect_uncached(self):
        ''' activates the connection object '''

        if not HAVE_PARAMIKO:
            raise errors.AnsibleError("paramiko is not installed")

        vvv("ESTABLISH CONNECTION FOR USER: %s on PORT %s TO %s" %
            (self.user, self.port, self.host),
            host=self.host)

        ssh = paramiko.SSHClient()

        self.keyfile = os.path.expanduser("~/.ssh/known_hosts")

        if C.HOST_KEY_CHECKING:
            ssh.load_system_host_keys()

        ssh.set_missing_host_key_policy(MyAddPolicy(self.runner))

        allow_agent = True

        if self.password is not None:
            allow_agent = False

        try:

            if self.private_key_file:
                key_filename = os.path.expanduser(self.private_key_file)
            elif self.runner.private_key_file:
                key_filename = os.path.expanduser(self.runner.private_key_file)
            else:
                key_filename = None
            ssh.connect(self.host,
                        username=self.user,
                        allow_agent=allow_agent,
                        look_for_keys=True,
                        key_filename=key_filename,
                        password=self.password,
                        timeout=self.runner.timeout,
                        port=self.port)

        except Exception as e:

            msg = str(e)
            if "PID check failed" in msg:
                raise errors.AnsibleError(
                    "paramiko version issue, please upgrade paramiko on the machine running ansible"
                )
            elif "Private key file is encrypted" in msg:
                msg = 'ssh %s@%s:%s : %s\nTo connect as a different user, use -u <username>.' % (
                    self.user, self.host, self.port, msg)
                raise errors.AnsibleConnectionFailed(msg)
            else:
                raise errors.AnsibleConnectionFailed(msg)

        return ssh
Пример #4
0
    def _connect_uncached(self):
        ''' activates the connection object '''

        if not HAVE_PARAMIKO:
            raise errors.AnsibleError("paramiko is not installed")

        user = self.runner.remote_user

        vvv("ESTABLISH CONNECTION FOR USER: %s on PORT %s TO %s" %
            (user, self.port, self.host),
            host=self.host)

        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

        allow_agent = True
        if self.runner.remote_pass is not None:
            allow_agent = False
        try:
            private_key = None
            key_filename = None

            if self.runner.private_key:
                private_key = paramiko.RSAKey.from_private_key(
                    StringIO.StringIO(self.runner.private_key))

            if self.runner.private_key_file:
                key_filename = os.path.expanduser(self.runner.private_key_file)

            ssh.connect(self.host,
                        username=user,
                        allow_agent=allow_agent,
                        look_for_keys=True,
                        key_filename=key_filename,
                        pkey=private_key,
                        password=self.runner.remote_pass,
                        timeout=self.runner.timeout,
                        port=self.port)
        except Exception, e:
            msg = str(e)
            if "PID check failed" in msg:
                raise errors.AnsibleError(
                    "paramiko version issue, please upgrade paramiko on the machine running ansible"
                )
            elif "Private key file is encrypted" in msg:
                msg = 'ssh %s@%s:%s : %s\nTo connect as a different user, use -u <username>.' % (
                    user, self.host, self.port, msg)
                raise errors.AnsibleConnectionFailed(msg)
            else:
                raise errors.AnsibleConnectionFailed(msg)
Пример #5
0
    def exec_command(self,
                     cmd,
                     tmp_path,
                     sudo_user=None,
                     sudoable=False,
                     executable='/bin/sh',
                     in_data=None,
                     su=None,
                     su_user=None):
        ''' run a command on the remote host '''

        if in_data:
            raise errors.AnsibleError(
                "Internal Error: this module does not support optimized module pipelining"
            )

        bufsize = 4096

        try:

            chan = self.ssh.get_transport().open_session()
            self.ssh.get_transport().set_keepalive(5)

        except Exception, e:

            msg = "Failed to open session"
            if len(str(e)) > 0:
                msg += ": %s" % str(e)
            raise errors.AnsibleConnectionFailed(msg)
Пример #6
0
    def connect(self):
        ''' activates the connection object '''

        if not HAVE_PARAMIKO:
            raise errors.AnsibleError("paramiko is not installed")

        user = self.runner.remote_user

        vvv("ESTABLISH CONNECTION FOR USER: %s" % user, host=self.host)



        #SSHConfig() checks
        config = SSHConfig()
        home_directory = os.getenv('HOME')
        config.parse(open(os.path.join(home_directory, '.ssh/config')))
        o = config.lookup(self.host)

        if o.has_key('port'):
            self.port = int(o['port'])
        if o.has_key('hostname'):
            self.host = o['hostname']
        if o.has_key('user'):
            user = o['user']


        #print user,self.host
        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

        allow_agent = True
        if self.runner.remote_pass is not None:
            allow_agent = False
        try:
            ssh.connect(self.host, username=user, allow_agent=allow_agent, look_for_keys=True,
                key_filename=self.runner.private_key_file, password=self.runner.remote_pass,
                timeout=self.runner.timeout, port=self.port)
        except Exception, e:
            msg = str(e)
            if "PID check failed" in msg:
                raise errors.AnsibleError("paramiko version issue, please upgrade paramiko on the machine running ansible")
            elif "Private key file is encrypted" in msg:
                msg = 'ssh %s@%s:%s : %s\nTo connect as a different user, use -u <username>.' % (
                    user, self.host, self.port, msg)
                raise errors.AnsibleConnectionFailed(msg)
            else:
                raise errors.AnsibleConnectionFailed(msg)
Пример #7
0
    def exec_command(self, cmd, tmp_path, sudo_user=None, sudoable=False, executable='/bin/sh', in_data=None, su=None, su_user=None):
        ''' run a command on the remote host '''

        bufsize = 4096
        try:
            chan = self.ssh.get_transport().open_session()
        except Exception, e:
            msg = "Failed to open session"
            if len(str(e)) > 0:
                msg += ": %s" % str(e)
            raise errors.AnsibleConnectionFailed(msg)
Пример #8
0
    def _get_conn(self):
        user = self.runner.remote_user

        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

        try:
            ssh.connect(
                self.host,
                username=user,
                allow_agent=True,
                look_for_keys=True,
                key_filename=self.runner.private_key_file,
                password=self.runner.remote_pass,
                timeout=self.runner.timeout,
                port=self.port
            )
        except Exception, e:
            if str(e).find("PID check failed") != -1:
                raise errors.AnsibleError("paramiko version issue, please upgrade paramiko on the machine running ansible")
            else:
                raise errors.AnsibleConnectionFailed(str(e))
Пример #9
0
    def exec_command(self,
                     cmd,
                     tmp_path,
                     sudo_user=None,
                     sudoable=False,
                     executable='/bin/sh',
                     in_data=None,
                     su=None,
                     su_user=None):
        ''' run a command on the remote host '''

        if in_data:
            raise errors.AnsibleError(
                "Internal Error: this module does not support optimized module pipelining"
            )

        bufsize = 4096

        try:

            self.ssh.get_transport().set_keepalive(5)
            chan = self.ssh.get_transport().open_session()

        except Exception as e:

            msg = "Failed to open session"
            if len(str(e)) > 0:
                msg += ": %s" % str(e)
            raise errors.AnsibleConnectionFailed(msg)

        no_prompt_out = ''
        no_prompt_err = ''
        if not (self.runner.sudo and sudoable) and not (self.runner.su and su):

            if executable:
                quoted_command = executable + ' -c ' + pipes.quote(cmd)
            else:
                quoted_command = cmd
            vvv("EXEC %s" % quoted_command, host=self.host)
            chan.exec_command(quoted_command)

        else:

            # sudo usually requires a PTY (cf. requiretty option), therefore
            # we give it one by default (pty=True in ansble.cfg), and we try
            # to initialise from the calling environment
            if C.PARAMIKO_PTY:
                chan.get_pty(term=os.getenv('TERM', 'vt100'),
                             width=int(os.getenv('COLUMNS', 0)),
                             height=int(os.getenv('LINES', 0)))
            if self.runner.sudo or sudoable:
                shcmd, prompt, success_key = utils.make_sudo_cmd(
                    self.runner.sudo_exe, sudo_user, executable, cmd)
            elif self.runner.su or su:
                shcmd, prompt, success_key = utils.make_su_cmd(
                    su_user, executable, cmd)

            vvv("EXEC %s" % shcmd, host=self.host)
            sudo_output = ''

            try:

                chan.exec_command(shcmd)

                if self.runner.sudo_pass or self.runner.su_pass:

                    while True:

                        if success_key in sudo_output or \
                            (self.runner.sudo_pass and sudo_output.endswith(prompt)) or \
                            (self.runner.su_pass and utils.su_prompts.check_su_prompt(sudo_output)):
                            break
                        chunk = chan.recv(bufsize)

                        if not chunk:
                            if 'unknown user' in sudo_output:
                                raise errors.AnsibleError(
                                    'user %s does not exist' % sudo_user)
                            else:
                                raise errors.AnsibleError(
                                    'ssh connection ' +
                                    'closed waiting for password prompt')
                        sudo_output += chunk

                    if success_key not in sudo_output:

                        if sudoable:
                            chan.sendall(self.runner.sudo_pass + '\n')
                        elif su:
                            chan.sendall(self.runner.su_pass + '\n')
                    else:
                        no_prompt_out += sudo_output
                        no_prompt_err += sudo_output

            except socket.timeout:

                raise errors.AnsibleError('ssh timed out waiting for sudo.\n' +
                                          sudo_output)

        stdout = ''.join(chan.makefile('rb', bufsize))
        stderr = ''.join(chan.makefile_stderr('rb', bufsize))

        return (chan.recv_exit_status(), '', no_prompt_out + stdout,
                no_prompt_out + stderr)