Community discussions

MikroTik App
 
NSimpraga
newbie
Topic Author
Posts: 32
Joined: Fri Sep 27, 2019 2:47 pm

Running command via Python script with Paramiko

Tue Feb 04, 2020 2:23 pm

Hi all,

I am having trouble receiving the complete output of a command run from a Python script with paramiko using SSH to connect to the router.

When I run 'ip firewall connection print' in the terminal on the router GUI it prints the whole connections table along with all the columns with data rates and whatnot.
When I run the same command from the python script, it manages to connect and print the output but only part of it. The last column is TCP-STATE and the rest does not get printed out.

I think I know what the catch is. When I run the command from a Putty terminal while it is not fullscreen it does the same thing - prints only part of the table. When I fullscreen it, it prints out the whole table. So I presume the terminal window (which is of course invisible to me) that the script opens is not big enough to hold all of the columns so only part of it gets printed out.

Here is the script :
 import paramiko

client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect('myDefaultGateway', username='myUsername', password='myPassword')
stdin, stdout, stderr = client.exec_command('ip firewall connection print')

for line in stdout:
    print(line.strip('\n'))
client.close()
 
I run the script via cmd and redirect the output to a file. What can I do to make the script show all the data? I am looking for the data rates for each of the IPs but that part is missing.
 
imaljko4
Member Candidate
Member Candidate
Posts: 248
Joined: Fri Apr 25, 2008 6:52 pm

Re: Running command via Python script with Paramiko

Wed Feb 17, 2021 12:17 am

Maybe you can just add "detail" after "print" command:
'ip firewall connection print detail'
'ip firewall connection print terse'
.
All is the same just with "print detail" or "print terse":
Code: Select all
import paramiko

client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect('myDefaultGateway', username='myUsername', password='myPassword')
stdin, stdout, stderr = client.exec_command('ip firewall connection print terse') 

for line in stdout:
    print(line.strip('\n'))
client.close()

Who is online

Users browsing this forum: No registered users and 22 guests