0

My problem is very similar to this question already asked: No handlers could be found for logger paramiko

The difference is that my script will run perfectly fine in the Python interpreter but will throw the mentioned error when compiled with PyInstaller to an exe file. I have tried a couple different logging handlers with no luck, and I'm shooting for a self-containted executable I can run from any Windows system. Here is my script for reference:

from netmiko import ConnectHandler
net_connect = ConnectHandler(device_type='hp_procurve', ip='10.1.2.20', username='myusername', password='mypassword')  
output = net_connect.send_command("show run")
print output
net_connect.disconnect()
raw_input("Press enter to exit...")

Thanks for the help!

Community
  • 1
  • 1
devdacool
  • 13
  • 5

2 Answers2

1

Add a console handler (for example) to paramiko.transport:

paramiko_logger = logging.getLogger('paramiko.transport')
if not paramiko_logger.handlers:
    console_handler = logging.StreamHandler()
    console_handler.setFormatter(
        logging.Formatter('%(asctime)s | %(levelname)-8s| PARAMIKO: '
                          '%(lineno)03d@%(module)-10s| %(message)s')
        )
    paramiko_logger.addHandler(console_handler)
fernandezcuesta
  • 1,913
  • 1
  • 10
  • 21
0

For anyone who runs across this, here is my full Python script to SSH into an HP ProCurve switch and print the output of a "show run" command. I ran into another error where the cryptography module would not interact properly with PyInstaller after the mentioned error in this post was solved. This link provides that solution: https://github.com/pyinstaller/pyinstaller/issues/2013

def patch_crypto_be_discovery():
    from cryptography.hazmat import backends
    try:
        from cryptography.hazmat.backends.commoncrypto.backend import backend as be_cc
    except ImportError:
        be_cc = None
    try:
        from cryptography.hazmat.backends.openssl.backend import backend as be_ossl
    except ImportError:
        be_ossl = None
    backends._available_backends_list = [
        be for be in (be_cc, be_ossl) if be is not None
]
import logging
paramiko_logger = logging.getLogger('paramiko.transport')
if not paramiko_logger.handlers:
    console_handler = logging.StreamHandler()
    console_handler.setFormatter(
        logging.Formatter('%(asctime)s | %(levelname)-8s| PARAMIKO: '
                      '%(lineno)03d@%(module)-10s| %(message)s')
    )
paramiko_logger.addHandler(console_handler)
from netmiko import ConnectHandler
net_connect = ConnectHandler(device_type='hp_procurve', ip='10.1.2.20', username='myusername', password='mypassword')  
output = net_connect.send_command("show run")
print output
net_connect.disconnect()
raw_input("Press enter to exit...")

I also needed to run this command to compile the script to an exe:

pyinstaller --onefile --hidden-import cryptography.hazmat.backends.openssl --hidden-import cffi HP-ProCurve-ShowRun.py
devdacool
  • 13
  • 5