Python + Paramiko + Error: sudo: no tty present and no askpass program specified.

问题描述 投票:0回答:1

我已经验证了现有的线程,但无法得到这个问题的确切原因和解决方案。

问题:'sudo: no tty present and no askpass program specified'当代码如下。

str_command_to_exec += str(each_cmd).strip() + "\n"
# Now execute the command
stdin, stdout, stderr = self.client.exec_command('sudo -S ls')

以下是我应用的可能的解决方案 但仍然没有进展。

  1. 解决方法:'将密码附加到命令中。'sudo: no tty present and no askpass program specified. bash: line 1:::command not found' (命令未找到)

  2. Sol2: stdin, stdout, stderr = client.exec_command(command, get_pty=True)超过30秒仍不知道控制权是否从exec_command(...)传递。

  3. Sol3: self.client.get_pty()无法设置连接。

  4. 解答4: stdin, stdout, stdin()无法设置连接。

    stdin, stdout, stderr = self.client.exec_command('sudo -S ls')stdin.write('\n')stdin.flush()time.sleep(2)

无法对sudo命令'stdin, stdout, stderr = self.client.exec_command('sudo -S info')'做同样的处理,导致同样的PROBLEM。

谁能告诉我是否有任何处理Sudo命令的解决方案或变通方法?

python linux sudo paramiko
1个回答
0
投票

我能够得到这个问题的解决方案。

我用send(..)代替了execute_command(...)。

SETUP:

self.client = paramiko.client.SSHClient()
self.client.set_missing_host_key_policy(paramiko.client.AutoAddPolicy())
self.client.connect(hostname=self.str_host_name, port=22,
                    username=self.str_user_name, password=self.str_user_pass)
self.transport = paramiko.Transport(self.str_host_name, 22)
self.transport.connect(username=self.str_user_name, password=self.str_user_pass)

EXECUTION:

if self.shell:
    self.shell.send(str_sudo_command + "\n")

    if self.shell is not None:
    time.sleep(2)
    self.str_sudo_command_result += str(self.shell.recv(1024).decode('utf-8'))
    self.str_sudo_command_result = str(self.str_sudo_command_result).strip()
    self.str_sudo_command_result = self.str_sudo_command_result.splitlines()
    if len(self.str_sudo_command_result) > 0:
        if "[sudo] password for " in self.str_sudo_command_result[-1]:
            self.str_sudo_command_result = ""
            self.shell.send(self.str_user_pass + "\n")
            time.sleep(2)
        else:
            while True:
                result = str(self.str_sudo_command_result)
                result = result.splitlines()
                time.sleep(2)
                if self.str_result_end_line not in result[-1]:
                    while self.shell.recv_ready():
                        self.str_sudo_command_result += str(self.shell.recv(9999).decode('utf-8'))
            else:
                break

欢迎提出建议和纠正。

© www.soinside.com 2019 - 2024. All rights reserved.