Home > Enterprise >  Cannot Print output for Fortigate Command with Python
Cannot Print output for Fortigate Command with Python

Time:04-05

I am having an issue with getting my python script to return or print any output for the following command run on my fortigate firewall

'diagnose sniffer packet any "host X.X.X.X and port 53" 4 0 a

def dns_pcap():
    device = ConnectHandler(device_type="fortinet", ip="X.X.X.X", username="xxxxxxxx", password="xxxxxxxxx")
    lines = []
    gi_pcap = device.send_command('diagnose sniffer packet GI "host X.X.X>X and port 53" 4 0 a')
    output = device.read_channel()

    print(output)

dns_pcap()

The script outputs nothing to my terminal, anyone have any idea how to get the output of this to command to print to my screen?

(Also please note I am using python2.7)

I have many scripts running to both fortinet and cisco devices, and they all print outputs from variable assigned commands to screen when i execute my scripts, but not in this case

I am assuming it is because the output is not static like a 'show system interface' but I am not sure how to handle dynamic output from a command.

CodePudding user response:

The output is the result of the method device.send_command. output = device.read_channel() is not necessary. Replace print(output) with print(gi_pcap) and you'll be on the right track.

If the command requires any sort of interaction, that can also cause it to freeze up because the script is waiting for a prompt that will never come. You can see if this is happening by saving the session log output. You can do this by adding session_log="device.txt" to your session arguments, with device.txt being any filename relevant to you. The contents of that file will be whatever is being sent back and forth in the SSH session to the device.

CodePudding user response:

The answer was indeed found in this post

Get output from a Paramiko SSH exec_command continuously

using paramiko and the get_pty=True argument in the function allowed me to print the pseudu terminal

Thanks very much to Richard Dodson for the help

  • Related