Paramiko:没有 tty 存在,也没有指定 ssh python 的 askpass 程序 [英] Paramiko: no tty present and no askpass program specified ssh python

查看:46
本文介绍了Paramiko:没有 tty 存在,也没有指定 ssh python 的 askpass 程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python 新手:

我有以下应该在远程主机上循环的代码块.

I have below block of code that is supposed to loop on remote hosts.

    import paramiko
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.load_system_host_keys()
    ssh.connect(host, 22, username, password, timeout=5)
    stdin, stdout, stderr = ssh.exec_command('sudo hostname')

我明白了:

sudo: no tty present and no askpass program specified

我试过了

stdin, stdout, stderr = ssh.exec_command('sudo -S hostname')

stdin, stdout, stderr = ssh.exec_command('sudo hostname',  get_pty=True)

但是代码永远存货(不工作).

But the code is getting stock forever (not working).

有什么建议吗?

推荐答案

您应该将 sudo 的密码发送到 stdin:

You should to send your password for sudo to the stdin:

import paramiko

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.load_system_host_keys()
ssh.connect(host, 22, username, password, timeout=5)

stdin, stdout, stderr = ssh.exec_command('sudo hostname', get_pty=True)
stdin.write('password\n')  # Password for sudo
stdin.flush()

这篇关于Paramiko:没有 tty 存在,也没有指定 ssh python 的 askpass 程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆