How to Use Paramiko Logging

How to use Paramiko logging?

Paramiko names its loggers, so simply:

import logging
import paramiko

logging.basicConfig()
logging.getLogger("paramiko").setLevel(logging.WARNING) # for example

See the logging cookbook for some more examples.

You can also use log_to_file from paramiko.util to log directly to a file.

Python Paramiko log output of commands to a file

Don't use the paramiko logger. Rather create your own.

import logging
logger = logging.getLogger(__name__)

def restart_service(node_name):
print('='*30 + ' Starting to work on ' + node_name + ' ' + '='*30 + '\n')
logging.info('Connecting to %s in order to restart %s...', node_name, service_name)
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.load_system_host_keys()
ssh.connect(node_name)
channel = ssh.get_transport().open_session()
channel.exec_command(command)
while True:
if channel.exit_status_ready():
break
rl, wl, xl = select.select([channel], [], [], 0.0)
if len(rl) > 0:
# Log output
logger.info(channel.recv(1024))
ssh.get_transport().close()
ssh.close()

This way you can have fine grained control about exactly what you want to log and which information is important to you

How to log each SSH session packet with Paramiko?

In addition to enabling logging, call Transport.set_hexdump():

client.get_transport().set_hexdump(True)

Regarding your original problem, see also:

Command executed with Paramiko does not produce any output



Related Topics



Leave a reply



Submit