如何使用 paramiko 使用 -t 选项执行 SSH [英] how to do SSH with -t option using paramiko

查看:57
本文介绍了如何使用 paramiko 使用 -t 选项执行 SSH的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试登录某些远程服务器并尝试获取系统信息.在这个过程中,我在一些服务器中遇到了一个问题,在这些服务器中,作为 SUDO 执行命令并不真正需要密码.为了克服这个问题并使脚本通用,想出了像sudo -k udisksctl status"这样的选项,即提供 -k 选项,以便每次请求密码时.

I am trying to login to some remote servers and trying to fetch the system info. During that process I have faced an issued in some of the servers, where the password is not really required to execute commands as SUDO. To overcome this issue and make the script generic came up with option like this "sudo -k udisksctl status", i.e providing -k option so that everytime it requests for a password.

现在出现了一个不同的问题:当尝试登录某些服务器时,出现以下错误抱歉,您必须有一个 tty 才能运行 sudo centos".当谷歌搜索出来时,从 这里

Now a different problem has occured : When trying to login to some of the servers the following error is occuring "sorry you must have a tty to run sudo centos". When googled out came to know about the option of doing ssh with -t option from here

所以我的问题是如何使用 paramiko 使用 -t 选项执行 SSH?或者如何克服这个问题?

import paramiko
import pprint

ip_list = [
["192.168.11.44", "root", "aaa", "aaa"],
["192.168.11.6", "root", "bbb", "bbb"]
]
os_check_list = ["DISTRIB_DESCRIPTION"]
os = None

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

for ip in ip_list:
    ssh.connect(ip[0], username = ip[1], password = ip[2])

    stdin, stdout, stderr = ssh.exec_command("sudo -k udisksctl status")
    stdin.write(ip[3]+"\n")

推荐答案

您可以使用 get_pty 参数:

stdin, stdout, stderr = ssh.exec_command("sudo -k udisksctl status", get_pty=True)

虽然有一些副作用.使用 get_pty=True,所有输出都将发送到 stdout,因此您无法再从 stderr 获取数据.

There's some side effect though. With get_pty=True, all output will be sent to stdout and so you cannot get data from stderr any more.

这篇关于如何使用 paramiko 使用 -t 选项执行 SSH的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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