Search code examples
paramiko

Paramiko stdout stuck at router banner message


I am fairly new to Paramiko so my apologies if this question has been answered already

I am trying to automate login and execution to a PAN Firewall. The FW output is as follows:

Last login: Wed Apr 27 11:54:01 2022 from 10.54.90.24



Number of failed attempts since last successful login: 0



admin@PA-5440-F03_31> show transceiver-detail all

My code is below:

>>> import paramiko
>>> command = "show transceiver-detail all"
>>> client = paramiko.client.SSHClient()
>>> client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
>>> client.connect(hostname, username=username, password=password)
>>> _stdin, _stdout, _stderr = client.exec_command(command)
>>> client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
>>> client.connect(hostname, username=username, password=password)
>>> _stdin, _stdout, _stderr = client.exec_command(command)

My problem is that when I read _stdout:

>>> print(stdout.read().decode())

I get only the banner:

'\n\n\nNumber of failed attempts since last successful login: 0\n\n\n\n'

How do I get to the point where exec_command returns an output that is captured by stdout?

NOTE : I had to explicitly close the _stdout channel (_stdout.channel.close() ) before the read command worked. Not sure if this is standard procedure or if there is something I need to do to ensure the channel closes automatically.


Solution

  • Turns out that PAN has its own Python module - pandevice - that handles this issue.

    from pandevice.firewall import Firewall
    import xml.etree.ElementTree as ET
    
    fw = Firewall(hostname, api_username=<username>, api_password=<password>)
    result = fw.op(cmd='show transceiver-detail all')
    
    >>> type(result)
    <class 'xml.etree.ElementTree.Element'>