Community discussions

MikroTik App
 
Artnet44
just joined
Topic Author
Posts: 7
Joined: Tue Jan 12, 2021 5:56 pm

Paramiko: Interacting with "source:" within scripts

Thu Sep 29, 2022 11:03 am

hello everyone,

I have hit a dead end with loading a ppp dropping script on my routerboards.
Due to the massive amount of devices that require the script, I need to somehow automate it.

I have tried using Paramiko with python, to load the following script onto the unit:

/system script
add dont-require-permissions=yes name=ppp-dropper owner=admin policy=\
ftp,reboot,read,write,policy,test,password,sniff,sensitive source=":log in\
fo message=\"PPP-DROPPER RUNNING\"\r\
\n/ppp active\r\
\n:foreach i in=[/ppp active find] do={\r\
\n remove \$i\r\
\n}"


As far as I can tell it seems paramiko is having trouble executing the line breaks, I have tried removing them and using multiple different ways to send the command with no luck. (eg. reading it from a file and sending via routeros_api library)

The stdout I am receiving from the routerboard is as follows:
b'syntax error (line 1 column 72)\nexpected closing brace (line 2 column 5)\nsyntax error (line 1 column 8)\nsyntax error (line 1 column 61)\n'

Here is the current python code, although this code has been changed multiple times to try different angles.

import paramiko
ip = r"[redacted]"
iplist=[line.strip() for line in open('ips.txt')]
print(iplist)


def EsxCli(ipaddress):
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy)
client.connect(hostname = ipaddress , port='22', username='redacted',password='redacted',look_for_keys=False)

stdin,stdout,stderr = client.exec_command('''/system script
add dont-require-permissions=yes name=ppp-dropper owner=admin policy=\
ftp,reboot,read,write,policy,test,password,sniff,sensitive source=":log in\
fo message=\"RUNNING PPP-DROPPER SCRIPT\"\r\
\n/ppp active\r\
\n:foreach i in=[/ppp active find] do={\r\
\n remove \$i\r\
\n}"
/system scheduler
add interval=1d name=PPP-Dropper on-event=ppp-dropper policy=\
ftp,reboot,read,write,policy,test,password,sniff,sensitive start-date=\
feb/25/2020 start-time=00:01:00''')

output_read = stdout.read()
print(output_read)


def StopVsfwd():
for j in iplist:
print(j)
EsxCli(j)
StopVsfwd()



Is there another way to interact with the "source" section or any subsection for that matter with paramiko or some other library?
This script executes perfectly when copy + pasted into the terminal on the routerboard, but this is not a viable route to go because I need to change thousands of routers.

Any advice would be greatly appreciated.
 
Artnet44
just joined
Topic Author
Posts: 7
Joined: Tue Jan 12, 2021 5:56 pm

Re: Paramiko: Interacting with "source:" within scripts  [SOLVED]

Thu Sep 29, 2022 12:20 pm

For those having a similar issue, finally resolved it with this:

/system script add dont-require-permissions=yes name=ppp-dropper owner=admin policy=ftp,reboot,read,write,policy,test,password,sniff,sensitive source=":log info message=running script /ppp active :foreach i in=[/ppp active find] do={remove \$i"}

Just note the break infront of $

Seems routerOS works with no breaks or special characters in the code.

Who is online

Users browsing this forum: flapviv and 16 guests