Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Paramiko and Pseudo-tty Allocation

Tags:

I'm trying to use Paramiko to connect to a remote host and execute a number of text file substitutions.

i, o, e = client.exec_command("perl -p -i -e 's/" + initial + "/"                                + replaced + "/g'" + conf); 

Some of these commands need to be run as sudo, which results in:

sudo: sorry, you must have a tty to run sudo

I can force pseudo-tty allocation with the -t switch and ssh.

Is it possible to do the same thing using paramiko?

like image 782
Jon Avatar asked May 26 '10 00:05

Jon


People also ask

What is Paramiko used for?

Paramiko is a Python library that makes a connection with a remote device through SSh. Paramiko is using SSH2 as a replacement for SSL to make a secure connection between two devices. It also supports the SFTP client and server model.

What is Paramiko SSHClient ()?

SSH client & key policies class paramiko.client. SSHClient. A high-level representation of a session with an SSH server. This class wraps Transport , Channel , and SFTPClient to take care of most aspects of authenticating and opening channels.

Is Paramiko open source?

Open Source Python-paramiko is used by IBM Netezza Host Management.

Is Python Paramiko safe?

The python package paramiko was scanned for known vulnerabilities and missing license, and no issues were found. Thus the package was deemed as safe to use.


2 Answers

Actually it's quite simple. Just:

stdin, stdout, stderr = client.exec_command(command,  get_pty=True) 
like image 69
Maciej Wawrzyńczuk Avatar answered Oct 08 '22 21:10

Maciej Wawrzyńczuk


The following code works for me:

#!/usr/bin/env python import paramiko  ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect('localhost',username='root',password='secret') chan = ssh.get_transport().open_session() chan.get_pty() chan.exec_command('tty') print(chan.recv(1024)) 

This was just assembled from looking at a few examples online... not sure if its the "right" way.

like image 34
David Avatar answered Oct 08 '22 22:10

David