0

I am writing a script that will get a list of servers and run service something restart ; service something status commands. I am using paramiko SSHClient in order to do that, with the below function:

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:
            print channel.recv(1024)
    ssh.get_transport().close()
    ssh.close()

My question is: How can I set the paramiko logger to write the output of my commands to the log as well? I don't need to get all the debug info, all I care about it that I'll have the results of service restart and service status commands in the file as well.

The logger configuration I've set is:

logging.basicConfig(filename='./log_restartService.log', level=logging.INFO, format='%(asctime)s  %(levelname)s: %(message)s')
logging.getLogger("paramiko").setLevel(logging.INFO)

I've also tried to use logger = paramiko.util.logging.getLogger() as suggested in other threads I've found, but that didn't help either..

Thanks!

Moshe Vayner
  • 738
  • 1
  • 8
  • 23

1 Answers1

2

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

Ciaran Liedeman
  • 779
  • 5
  • 13
  • Thanks! That's indeed working as expected, however now I don't see the output when running script, which wouldn't be of convenience for real-time monitoring of the output when the script is running. Is there an option for me to keep the previous setting of 'print(channel.recv(1024))' as well as the logger configuration you've suggested? – Moshe Vayner Sep 14 '15 at 18:38
  • Got it! Found the answer here: http://stackoverflow.com/questions/14058453/making-python-loggers-output-all-messages-to-stdout-in-addition-to-log – Moshe Vayner Sep 14 '15 at 19:00