Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you execute multiple commands in a single session in Paramiko? (Python)

def exec_command(self, command, bufsize=-1):     #print "Executing Command: "+command     chan = self._transport.open_session()     chan.exec_command(command)     stdin = chan.makefile('wb', bufsize)     stdout = chan.makefile('rb', bufsize)     stderr = chan.makefile_stderr('rb', bufsize)     return stdin, stdout, stderr 

When executing a command in paramiko, it always resets the session when you run exec_command. I want to able to execute sudo or su and still have those privileges when I run another exec_command. Another example would be trying to exec_command("cd /") and then run exec_command again and have it be in the root directory. I know you can do something like exec_command("cd /; ls -l"), but I need to do it in separate function calls.

like image 924
Takkun Avatar asked Jun 01 '11 15:06

Takkun


People also ask

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.

What is the difference between Paramiko and Netmiko?

Paramiko is more of a generic SSH module that you can use to automate specific SSH tasks. In contrast, Netmiko is broader and well optimized for managing network devices such as switches and routers. Abstraction is the other advantage of using Netmiko. Netmiko provides a simple function you can use to disable paging.

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.


2 Answers

Non-Interactive use cases

This is a non-interactive example... it sends cd tmp, ls and then exit.

import sys sys.stderr = open('/dev/null')       # Silence silly warnings from paramiko import paramiko as pm sys.stderr = sys.__stderr__ import os  class AllowAllKeys(pm.MissingHostKeyPolicy):     def missing_host_key(self, client, hostname, key):         return  HOST = '127.0.0.1' USER = '' PASSWORD = ''  client = pm.SSHClient() client.load_system_host_keys() client.load_host_keys(os.path.expanduser('~/.ssh/known_hosts')) client.set_missing_host_key_policy(AllowAllKeys()) client.connect(HOST, username=USER, password=PASSWORD)  channel = client.invoke_shell() stdin = channel.makefile('wb') stdout = channel.makefile('rb')  stdin.write(''' cd tmp ls exit ''') print stdout.read()  stdout.close() stdin.close() client.close() 

Interactive use cases
If you have an interactive use case , this answer won't help... I personally would use pexpect or exscript for interactive sessions.

like image 113
Mike Pennington Avatar answered Sep 28 '22 08:09

Mike Pennington


Try creating a command string separated by \n character. It worked for me. For. e.g. ssh.exec_command("command_1 \n command_2 \n command_3")

like image 29
Sandeep Avatar answered Sep 28 '22 10:09

Sandeep