Search code examples
pythonlinuxsshparamiko

how to do SSH with -t option using paramiko


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.

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

So my Question is how to do SSH with -t option using paramiko ? Or How to overcome this problem ?

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")

Solution

  • You can use the get_pty parameter:

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

    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.