Community discussions

MikroTik App
 
NSimpraga
newbie
Topic Author
Posts: 36
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: 250
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()
 
nbctcp
Frequent Visitor
Frequent Visitor
Posts: 77
Joined: Tue Sep 16, 2014 7:32 pm

Re: Running command via Python script with Paramiko

Tue Feb 15, 2022 10:13 am

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.
I don't have problem with ur script
TCP state still shown in ROS 7.1.2
import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname="192.168.88.1", username="admin", password="admin")
stdin, stdout, stderr = ssh.exec_command("ip firewall connection print")
print(stdout.read().decode("ascii").strip("\n"))
ssh.close()
output
C:\Users\nbctc\AppData\Local\Programs\Python\Python310\python.exe D:/TES/cisco2/coba2.py
Flags: E - expected; S - seen-reply; A - assured; C - confirmed; D - dying; 
F - fasttrack; H - hw-offload; s - srcnat; d - dstnat 
 #           PR.. SRC-ADDRESS           DST-ADDRESS           TCP-STATE  
 0  SAC F s  tcp  192.168.88.137:33536  23.52.40.177:443      established
 1  SAC F s  tcp  192.168.88.134:40378  199.232.46.73:443     established
 2  SAC F s  tcp  192.168.88.137:39548  119.28.123.116:1883   established

Who is online

Users browsing this forum: navigator and 25 guests