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
/ip/hotspot/user/remove
=.id=ID_HERE
http://wiki.mikrotik.com/wiki/API_in_C_SharpI use C Sharp
It will be wonderfulCan anybody make the API for VB (for VB projects and ASP pages)?!
Thanks,
Mladen
Added,Added Go implementation page to the wiki.
Could someone link to it from the API page?
http://wiki.mikrotik.com/wiki/API_in_Go
/ip firewall filter
add action=drop chain=input dst-port=53 in-interface=ether1_WAN protocol=udp
apiros.runCommand("/ip/firewall/filter/add","=chain=input\n=action=drop\n=dst-port=53\n=in-interface=ether1_WAN\n=protocol=udp","")
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
or in Python ( style 1):/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.ip.firewall.address__list.print (where="list==Blacklist and address==8.8.8.8") )
import smartROS
router = smartROS.getRouter("Main")
print( router.do("/ip/firewall/address-list/print", where="list==Blacklist and address==8.8.8.8") )
It supports only RouterOS versions 6.43 and later, right?I have written a C++ API connector/wrapper: https://github.com/zx2512/MikrotikPlus
That is correct. I didn't want to bother with the outdated authentication method which is removed in the latest versions.It supports only RouterOS versions 6.43 and later, right?
I didn't really want to spend additional time on it. Updating consumes less time.Supporting both is not difficult
But some other API articles are still in Wiki... Normis?..13:51, 3 January 2019 Normis (talk | contribs) deleted page RouterOS PHP class
13:51, 3 January 2019 Normis (talk | contribs) deleted page API PHP class
Please update the post with the last library in the Wiki API page.API implementations in different programming languages from users of this forum:
in Perl - forum thread by cheesegrits
in Delphi - forum thread and wiki by rodolfo
in Delphi #2 - forum thread by Chupaka
in PHP - external by Denis Basta and contributors
Java sample methods - forum post
in Python - wiki link by Mikrotik staff
in C# - wiki link by wiki user Gregy
Ruby on rails wiki and discussion on forum(v1.9) - by astounding
in Adobe Flash - wiki by haakon
in C - wiki by webasdf
in C (GPL2 license) -wiki by octo
.NET (C#) discussion on forum - external by danikf
in NodeJS - by trakkasure
API related information links:
http://wiki.mikrotik.com/wiki/API
http://wiki.mikrotik.com/wiki/API_command_notes
Hello,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...
SSH. Simply setup SSH access with key (instead of password, so you don't need to type it) - and you can run any command from .bat by simply executing something likei have a few queues and i would like to create a shortcurt to my desktop PC (Win 10) so i could enable/disable a particular queue, without user interaction.
I have basic programming training, nothing special. So even a bat file would do the trick. Is there a way to do this please?
ssh admin@router_IP "/queue simple disable queue1"
thanksi do not know if /ppp/l2tp/ exist, but the command on terminal are:
/interface l2tp-server server set enabled=no
/interface l2tp-server server set enabled=yes
$API->write("/interface/l2tp-server/server/getall",true);
I try<<< [36] /interface/l2tp-server/server/getall >>> [3/3] bytes read. >>> [3, 304]!re >>> [13/13] bytes read. >>> [13, 290]=enabled=true >>> [13/13] bytes read. >>> [13, 276]=max-mtu=1450 >>> [13/13] bytes read. >>> [13, 262]=max-mru=1450 >>> [14/14] bytes read. >>> [14, 247]=mrru=disabled >>> [36/36] bytes read. >>> [36, 210]=authentication=chap,mschap1,mschap2 >>> [21/21] bytes read. >>> [21, 188]=keepalive-timeout=30 >>> [23/23] bytes read. >>> [23, 164]=max-sessions=unlimited >>> [35/35] bytes read. >>> [35, 128]=default-profile=default-encryption >>> [14/14] bytes read. >>> [14, 113]=use-ipsec=yes >>> [26/26] bytes read. >>> [26, 86]=ipsec-secret=xxxxxxxxxx >>> [26/26] bytes read. >>> [26, 59]=caller-id-type=ip-address >>> [27/27] bytes read. >>> [27, 31]=one-session-per-host=false >>> [22/22] bytes read. >>> [22, 8]=allow-fast-path=false >>> [5/5] bytes read. >>> [5, 1]!done OkDisconnected...
$API->write("/interface/l2tp-server/server/set",false);
$API->write("=enabled=false",true);
$API->write("/interface/l2tp-server/server/set",false);
$API->write("=enabled=true",true);
Work Fine, thanks