Beispiel #1
0
 def execute_sudo_command(self, command, password=None, user='******'):
     rc = clibssh.ssh_channel_request_pty(self.channel)
     rc = clibssh.ssh_channel_request_shell(self.channel)
     if not self.session.sudo_marker:
         sudo_marker = ''.join(chr(random.randint(ord('a'), ord('z'))) for x in xrange(32))
     else:
         sudo_marker = self.session.sudo_marker
     prompt = '[sudo via libssh, key=%s] password: '******'sudo -k && sudo -p "%s" -u %s -- "$SHELL" -c %s; echo %s\n' % (
         prompt, user, pipes.quote(command), sudo_marker)
     self.session.sudo_marker = sudo_marker
     sudo_output = ''
     clibssh.ssh_channel_write(self.channel, sudocmd, len(bytes(sudocmd)))
     
     if password:
         while not sudo_output.endswith(prompt):
             bufflen = self.poll()
             if bufflen>0:
                 buff = ctypes.create_string_buffer(bufflen)
                 n = clibssh.ssh_channel_read(self.channel, buff, bufflen, 0)
                 sudo_output += buff.value
             time.sleep(0.01)
         password = password + '\n'
         clibssh.ssh_channel_write(self.channel, password, len(bytes(password)))
     self.sudo=True
Beispiel #2
0
    def read(self, stderr=False):
        data = ''
        n = 0
        bufflen = self.poll(stderr)
        stderr = int(stderr)
        while bufflen != clibssh.SSH_EOF:
            if bufflen > 0:
                buff = ctypes.create_string_buffer(bufflen)
                n2 = clibssh.ssh_channel_read(self.channel, buff, bufflen, stderr)
                n = n + n2
                data = data + buff.value
                if self.sudo and self.session.sudo_marker in data:
                    break
            
            self.poller.poll()
            time.sleep(0.01)
            bufflen = self.poll(stderr)

        if self.sudo:
            data = data[:data.find(self.session.sudo_marker)]
            data = data.strip(' \t\n\r')
        return data, n