Community discussions

 
luan
just joined
Posts: 4
Joined: Sun May 31, 2015 9:06 pm

Re: API Links

Mon Jun 01, 2015 1:29 am

Hi folks.
I need to print on a table id and the name of the hostpot users.
and remove the User by his id.
Can anyone help me.
I use C Sharp
 
User avatar
Chupaka
Forum Guru
Forum Guru
Posts: 8273
Joined: Mon Jun 19, 2006 11:15 pm
Location: Minsk, Belarus
Contact:

Re: API Links

Mon Jun 01, 2015 2:02 pm

Hi folks.
I need to print on a table id and the name of the hostpot users.
and remove the User by his id.
Can anyone help me.
/ip/hotspot/user/print
=.proplist=.id,name
then
/ip/hotspot/user/remove
=.id=ID_HERE
I use C Sharp
http://wiki.mikrotik.com/wiki/API_in_C_Sharp
Russian-speaking forum: https://forum.mikrotik.by/. Welcome!

For every complex problem, there is a solution that is simple, neat, and wrong.

MikroTik. Your life. Your routing.
 
kgninfos
Member
Member
Posts: 387
Joined: Thu Jun 21, 2012 7:34 pm
Location: Earth
Contact:

Re: API Links

Sat Jun 13, 2015 6:11 pm

can anyone post a sample project with source on how to use the api in Android app
a simple /ip/address/print with parsing and show the result in textfeild/ table would be enough

Thanks
 
User avatar
danikf
Frequent Visitor
Frequent Visitor
Posts: 65
Joined: Mon Mar 14, 2011 8:57 am

Re: API Links

Fri Aug 28, 2015 1:01 am

Hi all,

please redirect link for tik4net (.NET (C#) - external by danikf) to new location (both in this thread and in wiki links).

New location of tik4net project: https://github.com/danikf/tik4net
And newly created thread here: http://forum.mikrotik.com/viewtopic.php?f=9&t=99954

Thanks,
D
 
nuclide
just joined
Posts: 1
Joined: Thu Nov 05, 2015 8:56 pm

Re: API examples

Thu Nov 05, 2015 9:02 pm

Can anybody make the API for VB (for VB projects and ASP pages)?!



Thanks,
Mladen
It will be wonderful
 
User avatar
danikf
Frequent Visitor
Frequent Visitor
Posts: 65
Joined: Mon Mar 14, 2011 8:57 am

Re: API Links

Sun Nov 08, 2015 6:41 pm

Hi,

why not use C# (.NET) dlls in VB (also .NET) code?

D
 
andredossantos
just joined
Posts: 6
Joined: Thu Jul 21, 2016 9:21 pm

Re: API Links

Thu Jul 21, 2016 11:34 pm

Implementation in Go (Golang):
https://github.com/go-routeros/routeros

To install, run: go get gopkg.in/routeros.v1
 
andredossantos
just joined
Posts: 6
Joined: Thu Jul 21, 2016 9:21 pm

Re: API Links

Fri Jul 22, 2016 1:55 pm

Added Go implementation page to the wiki.
Could someone link to it from the API page?

http://wiki.mikrotik.com/wiki/API_in_Go
 
sosobobo
just joined
Posts: 5
Joined: Sun Sep 11, 2016 1:25 pm
Contact:

Re: API Links

Sun Sep 11, 2016 1:48 pm

Thanks! That's nice
 
User avatar
janisk
MikroTik Support
MikroTik Support
Topic Author
Posts: 6283
Joined: Tue Feb 14, 2006 9:46 am
Location: Riga, Latvia

Re: API Links

Fri Jan 20, 2017 4:06 pm

Added Go implementation page to the wiki.
Could someone link to it from the API page?

http://wiki.mikrotik.com/wiki/API_in_Go
Added,

we really appreciate your input. Hopefully, a lot of our users will find this useful.
 
igorvukotic
just joined
Posts: 8
Joined: Mon Aug 03, 2015 2:00 pm

Re: API Links

Sun Nov 26, 2017 6:50 pm

Hi guys, need help with API/Python27
i follow: https://wiki.mikrotik.com/wiki/Manual:A ... d_examples

can someone show how to pass this terminal command true API:
/ip firewall filter
add action=drop chain=input dst-port=53 in-interface=ether1_WAN protocol=udp
i tried ( with many variations ):
apiros.runCommand("/ip/firewall/filter/add","=chain=input\n=action=drop\n=dst-port=53\n=in-interface=ether1_WAN\n=protocol=udp","")
 
User avatar
Chupaka
Forum Guru
Forum Guru
Posts: 8273
Joined: Mon Jun 19, 2006 11:15 pm
Location: Minsk, Belarus
Contact:

Re: API Links

Mon Nov 27, 2017 8:36 am

.runCommand? I can't find this on the page you linked to...
Russian-speaking forum: https://forum.mikrotik.by/. Welcome!

For every complex problem, there is a solution that is simple, neat, and wrong.

MikroTik. Your life. Your routing.
 
igorvukotic
just joined
Posts: 8
Joined: Mon Aug 03, 2015 2:00 pm

Re: API Links

Mon Nov 27, 2017 11:04 am

class APIError(Exception):
	def __init__(self, message, category = -1):
		self.message = message
		self.category = category
	def __str__(self):
		return repr(self.message)
		
class ApiRos:
	"""
	Routeros api
	"""
	def __init__(self, ip):
		sk = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
		sk.connect((ip, 8728))
		self.sk = sk
		self.currenttag = 0
	def login(self, username, pwd):
		for _, attrs in self.talk(["/login"]):
			chal = binascii.unhexlify(attrs['=ret'])
		md = md5.new()
		md.update('\x00')
		md.update(pwd)
		md.update(chal)
		self.talk(["/login", "=name=" + username,
			"=response=00" + binascii.hexlify(md.digest())])
	def talk(self, words):
		if self.writeSentence(words) == 0:
			return
		r = []
		while 1:
			i = self.readSentence();
			if len(i) == 0:
				continue
			reply = i[0]
			attrs = {}
			for w in i[1:]:
				j = w.find('=', 1)
				if (j == -1):
					attrs[w] = ''
				else:
					attrs[w[:j]] = w[j + 1:]
			r.append((reply, attrs))
			if reply == '!done':
				return r
	def writeSentence(self, words):
		ret = 0
		for w in words:
			self.writeWord(w)
			ret += 1
		self.writeWord('')
		return ret
	def readSentence(self):
		r = []
		while 1:
			w = self.readWord()
			if w == '':
				return r
			r.append(w)
	def writeWord(self, w):
		# print "<<< " + w
		self.writeLen(len(w))
		self.writeStr(w)
	def readWord(self):
		ret = self.readStr(self.readLen())
		# print ">>> " + ret
		return ret
	def writeLen(self, l):
		if l < 0x80:
			self.writeStr(chr(l))
		elif l < 0x4000:
			l |= 0x8000
			self.writeStr(chr((l >> 8) & 0xFF))
			self.writeStr(chr(l & 0xFF))
		elif l < 0x200000:
			l |= 0xC00000
			self.writeStr(chr((l >> 16) & 0xFF))
			self.writeStr(chr((l >> 8) & 0xFF))
			self.writeStr(chr(l & 0xFF))
		elif l < 0x10000000:
			l |= 0xE0000000
			self.writeStr(chr((l >> 24) & 0xFF))
			self.writeStr(chr((l >> 16) & 0xFF))
			self.writeStr(chr((l >> 8) & 0xFF))
			self.writeStr(chr(l & 0xFF))
		else:
			self.writeStr(chr(0xF0))
			self.writeStr(chr((l >> 24) & 0xFF))
			self.writeStr(chr((l >> 16) & 0xFF))
			self.writeStr(chr((l >> 8) & 0xFF))
			self.writeStr(chr(l & 0xFF))
	def readLen(self):
		c = ord(self.readStr(1))
		if (c & 0x80) == 0x00:
			pass
		elif (c & 0xC0) == 0x80:
			c &= ~0xC0
			c <<= 8
			c += ord(self.readStr(1))
		elif (c & 0xE0) == 0xC0:
			c &= ~0xE0
			c <<= 8
			c += ord(self.readStr(1))
			c <<= 8
			c += ord(self.readStr(1))
		elif (c & 0xF0) == 0xE0:
			c &= ~0xF0
			c <<= 8
			c += ord(self.readStr(1))
			c <<= 8
			c += ord(self.readStr(1))
			c <<= 8
			c += ord(self.readStr(1))
		elif (c & 0xF8) == 0xF0:
			c = ord(self.readStr(1))
			c <<= 8
			c += ord(self.readStr(1))
			c <<= 8
			c += ord(self.readStr(1))
			c <<= 8
			c += ord(self.readStr(1))
		return c
	def writeStr(self, str):
		n = 0;
		while n < len(str):
			r = self.sk.send(str[n:])
			if r == 0: raise RuntimeError, "connection closed by remote end"
			n += r
	def readStr(self, length):
		ret = ''
		while len(ret) < length:
			s = self.sk.recv(length - len(ret))
			if s == '': raise RuntimeError, "connection closed by remote end"
			ret += s
		return ret
	def getSocket(self):
		return self.sk
	def runCommand(self, command, cmd2, cmd3, **arguments):
		'''
		Run a command attempting to keep as close to the command line version,
		while maintaining an easy to use library.
		'''
		apiMessage = [command, cmd2, cmd3]
		if arguments != None:
			apiMessage += ["={0}={1}".format(k.replace('__', '-'), v) for k, v in arguments.items()]
		rez = self.talk(apiMessage)
		# print rez
		# Remove the !done at the end of the list.
		if rez[len(rez) - 1][0] == '!done':
			doneVal = rez.pop()
 		# Check for error conditions (Need to make this more efficient).
		trapVal = filter(lambda x: x[0] == '!trap', rez)
		if trapVal != []:
			trapVal = trapVal.pop()
			if 'category' in trapVal:
				category = trapVal[1]['=category']
			else:
				category = -1
			# print "TrapVal = {0}".format(trapVal[1])
			raise APIError(trapVal[1]['=message'], category)
		# Extract the data itself
		data = map(lambda x: x[1], rez)
		if data == []:
			if doneVal[1] != {}:
				data = doneVal[1]['=ret']
		return data
	
 
User avatar
Chupaka
Forum Guru
Forum Guru
Posts: 8273
Joined: Mon Jun 19, 2006 11:15 pm
Location: Minsk, Belarus
Contact:

Re: API Links

Mon Nov 27, 2017 12:31 pm

Try "dst__port=53\n=in__interface=ether1_WAN" (replace 'minus' sign with double underscore)
Russian-speaking forum: https://forum.mikrotik.by/. Welcome!

For every complex problem, there is a solution that is simple, neat, and wrong.

MikroTik. Your life. Your routing.
 
igorvukotic
just joined
Posts: 8
Joined: Mon Aug 03, 2015 2:00 pm

Re: API Links

Mon Nov 27, 2017 4:48 pm

no help..
Screen Shot 2017-11-27 at 15.46.31.jpg
You do not have the required permissions to view the files attached to this post.
 
User avatar
Chupaka
Forum Guru
Forum Guru
Posts: 8273
Joined: Mon Jun 19, 2006 11:15 pm
Location: Minsk, Belarus
Contact:

Re: API Links

Tue Nov 28, 2017 12:35 pm

I'm not familiar with Py, check this: viewtopic.php?p=392367
Russian-speaking forum: https://forum.mikrotik.by/. Welcome!

For every complex problem, there is a solution that is simple, neat, and wrong.

MikroTik. Your life. Your routing.
 
lniconet
just joined
Posts: 1
Joined: Wed Jul 17, 2013 7:45 pm

Re: API Links

Fri Jun 22, 2018 8:10 pm

Hello,

For those interested, my python module to use the API:

https://github.com/lnicolas83/routeros-api
 
SergeS
just joined
Posts: 11
Joined: Fri Mar 25, 2016 6:23 am

Re: API Links

Fri Jul 13, 2018 12:50 am

Does anybody need Labview code for Mikrotik API support?
If yes - I could share it. I wrote it and was using already for few years with good results.
If nobody - I will not bother...
 
Pulido
just joined
Posts: 1
Joined: Tue Aug 14, 2018 11:05 pm

Re: API Links

Tue Aug 14, 2018 11:07 pm

I'm definitely interested in seeing this. Thanks.
 
User avatar
BlackVS
Member Candidate
Member Candidate
Posts: 171
Joined: Mon Feb 04, 2013 7:00 pm
Contact:

Re: API Links

Fri Mar 29, 2019 12:49 pm

One more Python API (still beta but functional) :
https://github.com/BlackVS/smartROS (some description in Russian)
Is developed for my own needs.
Main features:
  • TLS+ADH / TLS+certificates connection supported
  • routers' credentials stored in config file
  • human readable conditions (see below)
  • logging
  • test script "console" included
Conditions in commands:
in console:
/ip/firewall/address-list/print where="list==Blacklist and address==8.8.8.8"
or in Python ( style 1):
import smartROS
router = smartROS.getRouter("Main")
print( router.ip.firewall.address__list.print (where="list==Blacklist and address==8.8.8.8") )
or in Python ( style 2):
import smartROS
router = smartROS.getRouter("Main")
print( router.do("/ip/firewall/address-list/print", where="list==Blacklist and address==8.8.8.8") )
 
Madnessy
just joined
Posts: 3
Joined: Wed May 15, 2019 9:44 pm

Re: API Links

Wed May 15, 2019 9:50 pm

Not really api related , nor scripting related.

But is there an ansible playbook somewhere we can use to configure our devices ?
(Already started building my own cause i could'nt find one on the ansible galaxy nor on the forum)
 
nar6du14
just joined
Posts: 2
Joined: Mon Oct 01, 2018 4:43 pm

Re: API Links

Thu May 23, 2019 11:20 pm

This a link to a python 3 asynchronous version of mikrotik api feel free to share it https://bitbucket.org/wambedu14/splynxp ... krotik_api
 
Madnessy
just joined
Posts: 3
Joined: Wed May 15, 2019 9:44 pm

Re: API Links

Wed May 29, 2019 11:01 am

made a first commit of the ansible role / playbook
https://github.com/Madnessy/ansible-mikrotik

feel free to use / test / share / suggest improvements etc

Who is online

Users browsing this forum: Bing [Bot] and 6 guests