This question already has answers here:
Get output from a Paramiko SSH exec_command continuously
(6 answers)
Closed 11 months ago.
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.
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.
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
Can you share what is the interval time of getting an output in your Fortigate device for this command? Depending on the interval time, netmiko should have also worked.
What I have also tested in a different vendor using Netmiko for an dynamic output that I tried to get data in 10 seconds interval (Totally 100 seconds), it worked without an issue, I was able to get the output properly. However, when I have increased time of the interval of getting data as 11 or 12, I get some errors so it did not work.
Can you also try with Netmiko "timing" method to get your data? If the interval is shorter than 1-2 seconds, this method should also help. (For example, I can get ping output with 100 count without an error using timing method. Did you also try to get ping output in your network box if it works?)
I think that the size of the data you expect from your output is also important in your case. If the size is too big to be shown in a CLI screen, this may cause also a problem. In that case, you might need to open a file to save your data and use it from that file might be another option.
Better if you can advise if there is a regular time interval of this command along with the expected size of the data.
In addition, what I have also figure it out that in case the output takes more than 100 seconds, it might cause the problem. Here is the sample of how to this option following:
{
'device_type': 'device_type',
'ip': '135.243.92.119',
'username': 'admin',
'password': 'admin',
'port': port,
"fast_cli": False, # fast_cli
"global_delay_factor": 2 # if the outputs takes more than 100 seconds.
}
Below is the streaming data that i get when I connect from a command line. Yet..quit shocking...I get no data from from python. Why cant I print to screen the output? One would think I should just connect to to server, ready very eager.
tn = telnetlib.Telnet()
tn.open(HOST, TRADE_PORT)
while True:
print '-',tn.read_very_eager()
-
-
-
-
Below us a working telnet session on same machine.
ubuntu#ubuntu:~$ telnet 222.222.222.222 2000
Trying 222.222.222.222...
Connected to 222.222.222.222.
Escape character is '^]'.
S=SXL,P=157.00,Q=3221,T=130621,N=40111759,E=371993|S=SXL,P=157.00,Q=45,T=130621,N=40111760,E=371994|S=SXL,P=157.00,Q=1401,T=130621,N=40111761,E=371995|S=SXL,P=157.00,Q=46,T=130621,N=40111762,E=371996|S=SXL,P=157.00,Q=37,T=130621,N=40111763,E=371997|S=SXL,P=157.00,Q=60,T=130621,N=40111764,E=371998|S=RIO,P=6514.50,Q=57,T=130621,N=10085304,E=371999|S=SXL,P=157.00,Q=39,T=130621,N=40111765,E=372000|S=SXL,P=157.00,Q=701,T=130621,N=40111766,E=372001|S=SXL,P=157.00,Q=119,T=130621,N=40111767,E=372002|S=SXL,P=157.00,Q=120,T=130621,N=40111768,E=372003|S=SXL,P=157.00,Q=47,T=130621,N=40111769,E=372004|S=SXL,P=157.00,Q=22,T=130621,N=40111770,E=372005,C=XT|S=SXL,P=157.00,Q=98,T=130621,N=40111771,E=372006|S=SXL,P=157.00,Q=924,T=130621,N=40111772,E=372007|S=SXL,P=157.00,Q=381,T=130621,N=40111773,E=372008|S=SXL,P=157.00,Q=221,T=130621,N=40111774,E=372009|S=SXL,P=157.00,Q=220,T=130621,N=40111775,E=372010|S=SXL,P=157.00,Q=313,T=130621,N=40111776,E=372011|S=RIO,P=6514.00,Q=61,T=130621,N=10085305,E=372012|S=RIO,P=6514.00,Q=32,T=130621,N=10085306,E=372013|S=PMV,P=762.00,Q=17,T=130621,N=40111777,E=372014|S=RIO,P=6514.00,Q=8,T=130621,N=10085307,E=372015|S=RIO,P=6514.00,Q=18,T=130621,N=10085308,E=372016|S=RIO,P=6514.00,Q=65,T=130621,N=10085309,E=372017|S=AMP,P=456.50,Q=129,T=130621,N=20091811,E=372018|S=AMP,P=456.50,Q=482,T=130621,N=20091812,E=372019|S=GPT,P=353.50,Q=109,T=130621,N=20091813,E=372020|S=RSG,P=47.50,Q=194,T=130621,N=10085310,E=372021|S=AGK,P=1499.00,Q=147,T=130621,N=40111778,E=372022|S=AGK,P=1499.00,Q=183,T=130621,N=40111779,E=372023|S=AGK,P=1499.00,Q=1,T=130621,N=40111780,E=372024|S=AGK,P=1499.00,Q=183,T=130621,N=40111781,E=372025|S=AGK,P=1499.00,Q=29,T=130621,N=40111782,E=372026|S=AGK,P=1499.00,Q=77,T=130621,N=40111783,E=372027|S=AGK,P=1499.00,Q=21,T=130621,N=40111784,E=372028|S=AGK,P=1499.00,Q=492,T=130621,N=40111785,E=372029|S=AGK,P=1498.50,Q=68,T=130621,N=40111786,E=372030|S=AGK,P=1499.00,Q=7,T=130621,N=40111787,E=372031|S=AGK,P=1499.00,Q=25,T=130621,N=40111788,E=372032|S=SYDDA,P=388.00,Q=50,T=130621,N=30083122,E=372033|S=PCL,P=6.10,Q=145,T=130621,N=30083123,E=372034|S=VAS,P=6727.00,Q=744,T=130622,N=30083124,E=372035|S=REA,P=4081.00,Q=2,T=130622,N=40111789,E=372036|S=MMS,P=1269.00,Q=2,T=130622,N=30083125,E=372037|S=DMP,P=1488.00,Q=2,T=130622,N=40111790,E=372038|S=WPL,P=3733.00,Q=24,T=130622,N=30083126,E=372039|S=NWS,P=1928.00,Q=1,T=130622,N=40111791,E=372040|S=WBC,P=3218.00,Q=118,T=130622,N=20091814,E=372041|S=WOR,P=1610.00,Q=9,T=130622,N=30083127,E=372042|S=NWS,P=1928.00,Q=3,T=130622,N=40111792,E=372043|S=SUN,P=1298.00,Q=50,T=130622,N=20091815,E=372044|S=WES,P=4264.00,Q=1,T=130622,N=30083128,E=372045|S=RIO,P=6515.00,Q=1,T=130622,N=10085311,E=372046|S=SUL,P=1360.00,Q=6,T=130622,N=40111793,E=372047|S=WES,P=4264.00,Q=43,T=130622,N=30083129,E=372048|S=QBE,P=1566.00,Q=46,T=130622,N=20091816,E=372049|S=RIO,P=6515.00,Q=16,T=130622,N=10085312,E=372050|S=WOW,P=3347.00,Q=48,T=130622,N=40111794,E=372051|S=ORI,P=2302.00,Q=13,T=130622,N
Use tn.read_some() (blocking) instead of tn.read_very_eager() : it will wait for data to be available before returning