Community discussions

MikroTik App
 
Jusufs
just joined
Topic Author
Posts: 10
Joined: Thu Sep 12, 2019 1:13 pm

Running RouterOS script via API command line- Wifi ON/OFF

Thu Sep 16, 2021 10:23 am

Hi,
is it poossible to run a internal defined RouterOS script via API (Python3) external script (command line) ?
I'm trying to enable or disable wireless interface on demand (triggered on external condition).
Return information about successfully switching would be also great.
No SSH needed because all devices (RPi4 and wifi switches) are on local network.

I found some approach with paramiko module using SSH.
https://github.com/muhammedessa/python_ ... pymikrotik
https://www.youtube.com/watch?v=wDd8XA4XlYY
Thanks.
 
Pigsfoot
Frequent Visitor
Frequent Visitor
Posts: 84
Joined: Sat Oct 25, 2014 1:16 am

Re: Running RouterOS script via API command line- Wifi ON/OFF

Thu Sep 16, 2021 12:30 pm

We use some scripts so the RB can control an RPI... so in essence the other way round from what i understand you want to do but I cant see why it wouldn’t work by issuing command from the RPI to the RB

In order to do this you have to give permission to the RB to communicate with the RPI via SSH. I did get some help on this in the past so may be worth taking a look at an old post that helped me a lot.

viewtopic.php?f=2&t=175414
 
Jusufs
just joined
Topic Author
Posts: 10
Joined: Thu Sep 12, 2019 1:13 pm

Re: Running RouterOS script via API command line- Wifi ON/OFF

Thu Sep 16, 2021 5:39 pm

Thanks, I found a solution using python3 library paramiko
http://www.paramiko.org/

Python3 script example:
---------------------------------
import paramiko

client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect('routerIP', username='myuser', password='mypass')
stdin, stdout, stderr = client.exec_command('/system script run EnableWLAN')

#for line in stdout:
#    print(line.strip('\n'))
client.close()
On Mikrotik don't forget to enable SSH port 22 in IP/Services avaliable from a local IP address (pool)
or only from device running above python scripts

Who is online

Users browsing this forum: rextended and 26 guests