Community discussions

MikroTik App
 
User avatar
vklpt
newbie
Topic Author
Posts: 36
Joined: Mon Feb 18, 2019 1:13 pm
Location: Izhevsk
Contact:

Python scripting platform

Sun Jul 14, 2019 2:06 pm

Open source scripting platform for Windows with RouterOS API support.
Платформа для скриптов с поддержкой ROS

https://github.com/vikilpet/Taskopy
Russian page: https://vikilpet.wordpress.com/taskopy/

Example — add IP from clipboard to address-list "my_list":
def add_ip_to_list(hotkey='alt+ctrl+i'):
    routeros_send(
        [
            '/ip/firewall/address-list/add'
            , '=list=my_list'
            , '=address=' + clip_get()
        ]
        , device_ip='192.168.88.1'
        , device_user='admin'
        , device_pwd='PaSsWoRd'
    )
    msgbox('Done!', timeout=5)

Example — add address to list with HTTP-request:
def address_from_web(data, http=True, submenu='Rare'):
	routeros_send(
		[
			'/ip/firewall/address-list/add'
			, '=list=' + data.listname
			, '=address=' + data.address
		]
		, device_ip='192.168.88.1'
		, device_user='admin'
		, device_pwd='PaSSWorD'
	)
Then you can add address to any address-list with this HTTP request:
http://127.0.0.1/task?address_from_web&listname=my_list&address=1.2.3.4
Last edited by vklpt on Sat Aug 01, 2020 7:45 pm, edited 2 times in total.
 
User avatar
vklpt
newbie
Topic Author
Posts: 36
Joined: Mon Feb 18, 2019 1:13 pm
Location: Izhevsk
Contact:

Re: Scheduler with API support

Mon Aug 19, 2019 10:18 pm

New function - routeros_find_send to simplify editing of existing settings.
Remove static items from address-list:
routeros_find_send(
	cmd_find=[
		'/ip/firewall/address-list/print'
		, '?list=MY_LIST'
		, '?dynamic=false'
	]
	, cmd_send=['/ip/firewall/address-list/remove']
	, device_ip='192.168.88.1'
	, device_user='admin'
	, device_pwd='pA$$w0rd'
)
 
User avatar
vklpt
newbie
Topic Author
Posts: 36
Joined: Mon Feb 18, 2019 1:13 pm
Location: Izhevsk
Contact:

Re: Python script platform

Sat Jan 25, 2020 4:26 pm

It is possible to run commands in parallel (v2020-08-01 update)

Example - remove UDP connections without reply on all routers:
def MikroTik_Remove_UDP_connections_without_reply(submenu='demo'):
	''' Send this command to all routers simultaneously:
		/ip firewall connection remove [find protocol=udp and seen-reply=no]
	'''
	# List of routers and admin credentials
	# You can use encryption, see 'Cryptography' in readme
	routers = [
		('10.0.0.1', 'adm1', 'pwd1')
		, ('10.0.1.1', 'adm2', 'pwd2')
		, ('10.0.2.1', 'adm3', 'pwd3')
	]
	jobs = []
	for ip, user, pwd in routers:
		jobs.append(
			Job(
				routeros_find_send
				, device_ip=ip
				, device_user=user
				, device_pwd=pwd
				, cmd_find=[
					'/ip/firewall/connection/print'
					, '?protocol=udp'
					, '?seen-reply=no'
				]
				, cmd_send=['/ip/firewall/connection/remove']
			)
		)
	# Timeout value doesn't matter here
	for j in job_batch(jobs, timeout=10):
		print(
			'{}\t{}\t{}'.format(
				j.kwargs['device_ip'].ljust(14, ' ')
				, not j.error
				, j.time
			)
		)
	# Count successful jobs:
	good_jobs = sum([0 if j.error else 1 for j in jobs])
	dialog(f'Done: {good_jobs}/{len(jobs)}', timeout=4)
Last edited by vklpt on Sat Aug 01, 2020 7:51 pm, edited 4 times in total.
 
User avatar
vklpt
newbie
Topic Author
Posts: 36
Joined: Mon Feb 18, 2019 1:13 pm
Location: Izhevsk
Contact:

Re: Python script platform

Mon Apr 20, 2020 6:48 pm

Upload TOR nodes to router:
def MikroTik_TOR_nodes_update(submenu='demo'):
	# Only one fetch per 30 minutes allowed
	# by the author of list!
	# Skip IPv6 addresses.

	ROUTER_IP = '192.168.88.1'
	USER = 'admin'
	PWD = 'PaSsW0rD'
	# Exit nodes only:
	URL = 'https://www.dan.me.uk/torlist/?exit'
	LIST_NAME = 'TOR_NODES'

	ans = dialog([
		'Get a new list?'
		, 'Use previous'
		, 'Cancel'
	])
	if ans == 1000:
		ips = page_get(URL)
		if isinstance(ips, Exception):
			dialog('Failed to download new list'
				, content=repr(ips))
			return
		elif 'Umm...' in ips:
			dialog('Failed to download new list'
				, content=ips)
			return
		else:
			var_set('tor nodes', ips)
	elif ans == 1001:
		ips = var_get('tor nodes')
	else:
		return
	# skip ipv6:
	ips = [ip for ip in ips.split() if not ':' in ip]
	cmd = []
	for ip in ips:
		cmd.append([
			'/ip/firewall/address-list/add'
			, '=list=' + LIST_NAME
			, '=address=' + ip
		])
	# remove old nodes:
	status, data = routeros_find_send(
		cmd_find = [
			'/ip/firewall/address-list/print'
			, '?list=' + LIST_NAME
		]
		, cmd_send = ['/ip/firewall/address-list/remove']
		, device_ip=ROUTER_IP
		, device_user=USER
		, device_pwd=PWD
	)
	if not status:
		dialog('Error when removing old nodes'
			, content=data)
		return
	status, data = routeros_send(
		cmd=cmd
		, device_ip=ROUTER_IP
		, device_user=USER
		, device_pwd=PWD
		, info=True
	)
	if status:
		dialog(f'Done: {len(ips)} nodes')
	else:
		dialog('Error when adding new nodes'
			, content=data)
 
User avatar
vklpt
newbie
Topic Author
Posts: 36
Joined: Mon Feb 18, 2019 1:13 pm
Location: Izhevsk
Contact:

Re: Python scripting platform

Sat Aug 01, 2020 7:51 pm

v2020-08-01 changed job_pool and job_batch so that parallel execution is a bit more convient to use.

Who is online

Users browsing this forum: zjobra0013 and 34 guests