Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

After executing a command by Python Paramiko how could I save result?

Tags:

As you see below, is it possible to save the result? Cause, at second and third stdout.read() I couldn't reach the result.

import paramiko import os dssh = paramiko.SSHClient() dssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) dssh.connect('192.168.1.250', username='root', password='pass') import os stdin, stdout, stderr = dssh.exec_command('ifconfig') print stdout.read() print ('Sleeping 2 seconds!') os.system('sleep 2') stdin, stdout, stderr = dssh.exec_command('ls -l') print stdout.read() print stdout.read() print stdout.read() dssh.close() 
like image 360
MrCskncn Avatar asked Nov 15 '11 14:11

MrCskncn


People also ask

How do I get command output in paramiko?

Using exec_command() in Paramiko returns a tuple (stdin, stdout, stderr) . Most of the time, you just want to read stdout and ignore stdin and stderr . You can get the output the command by using stdout. read() (returns a string) or stdout.

How do I use paramiko in Python?

A Paramiko SSH Example: Connect to Your Server Using a Password. This section shows you how to authenticate to a remote server with a username and password. To begin, create a new file named first_experiment.py and add the contents of the example file. Ensure that you update the file with your own Linode's details.

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.

How do I change directory in paramiko?

readlines() will return the directory that you changed to. Show activity on this post. Well paramiko creates an instance of shell and all the commands that you wish to execute in paramiko have to be given in that instance of shell only. For example: Let us say I have some folder in the directory I am in.


2 Answers

Imagine that stdout is an ordinary file. What do you expect to get if you call file.read() the second time? -- nothing (empty string) unless the file has changed outside.

To save the string:

output = stdout.read() 

You might find Fabric simpler to use (it uses paramiko to execute commands under the hood).

like image 167
jfs Avatar answered Nov 29 '22 07:11

jfs


You can try this Generic API

def ssh_ctrl(ip, user, password,cmd):     ssh = paramiko.SSHClient()     ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())     try:         ssh.connect(hostname=ip, username=user, password=password, timeout=tout, compress = True,look_for_keys=False, allow_agent=False)     except (socket.error,paramiko.AuthenticationException,paramiko.SSHException) as message:         print "ERROR: SSH connection to "+ip+" failed: " +str(message)         sys.exit(1)      stdin, stdout, ssh_stderr = ssh.exec_command(cmd)     out = stdout.read()     stdin.flush()     ssh.close()     return out 
like image 37
Mikhilesh Sekhar Avatar answered Nov 29 '22 08:11

Mikhilesh Sekhar