【问题标题】:unable to sudo su using paramiko无法使用 paramiko sudo su
【发布时间】:2020-03-06 20:13:45
【问题描述】:

我使用 paramiko 连接到服务器并尝试切换到另一个用户。但我面临以下错误。

sudo:不存在 tty,也没有指定 askpass 程序

sudo: pam_authenticate: 对话错误

我已经尝试使用“sudo -S”选项,并且还通过命令传递密码。但没有运气。请帮我解决这个问题

我正在尝试从 windows 服务器连接到远程 linux 服务器,所以我无法使用 pexpect,因为它现在不支持 windows

PFB 脚本。

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(host, username=user, password=pwd)
print("Connected to: ", host)
stdin, stdout, stderr = ssh.exec_command('sudo su test_user')
if stdin:
    print("asked for inp: ", stdin)
    stdin.write(pwd+"\n")
    stdin.flush()
error = ""
output = ""
for err in stderr.readlines():
    error = error + err
if error != "":
    print("error: ", error)```



【问题讨论】:

标签: python-3.x linux paramiko


【解决方案1】:

尝试将 sudo 合并到我需要通过 paramiko 执行的所有命令中,这似乎工作正常。运行良好的代码:

stdin, stdout, stderr = ssh.exec_command('sudo -S chown -R test_user:test_user /tmp/test.txt')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-30
    • 1970-01-01
    相关资源
    最近更新 更多