找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 17558|回复: 21

[其它] routeros api执行工具程序及源码

[复制链接]
发表于 2009-5-12 20:29:28 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?注册

×
routeros在3.0版以上,新增了一个api功能。通过开启api功能,可以使用一个外部程序直接控制RouterOS
默认的API 端口是 8728. 默认情况下API功能是关闭的,要使用以下的命令开启:
/ip service enable api

然后再使用api工具就可以直接连到roueros中了。
附件中的程序就是这样的工具,大家可以测试下。

APITest.rar

203.01 KB, 下载次数: 467

routeros
发表于 2009-5-12 21:56:33 | 显示全部楼层
支持。。。
routeros
回复

使用道具 举报

发表于 2009-5-12 22:13:09 | 显示全部楼层
本帖最后由 seignior 于 2009-5-12 22:33 编辑

有没有vc或者vb的源码参考....

P.S:官方的没看明白,希望能求个相对完整的代码,例如这个demo的vc、vb版
routeros
回复

使用道具 举报

发表于 2009-5-12 22:16:01 | 显示全部楼层
routeros
回复

使用道具 举报

 楼主| 发表于 2009-5-12 22:50:27 | 显示全部楼层



API client realization for Delphi7. It implements execution of parallel queries which are easily handled due to database-like interface

APITest.zip

23.86 KB, 下载次数: 97

APITest Demo Application (Source)

RouterOSAPI.zip

3.72 KB, 下载次数: 124

RouterOS API Client Unit

点评

有没有易语言的参考下,谢谢  发表于 2012-4-10 00:07
routeros
回复

使用道具 举报

 楼主| 发表于 2009-5-12 22:51:23 | 显示全部楼层
我自己用c也写了一个,在linux 下调试通过。
routeros
回复

使用道具 举报

发表于 2009-5-12 22:55:28 | 显示全部楼层
本帖最后由 seignior 于 2009-5-12 23:00 编辑

我已经反复看
http://forum.mikrotik.com/viewtopic.php?f=9&t=31555
http://wiki.mikrotik.com/wiki/API_Delphi

但无奈c,basic本身就是半桶水,delphi就更一头雾水~~~~还是求你的c code吧

实际就是不想让基层直接操作ros,倒不是操守上的信任问题,主要是操作技能上的信任不够(那帮小崽子连自己在做什么都不清楚-_-"),想自己写个有限制的client给他们用。
routeros
回复

使用道具 举报

 楼主| 发表于 2009-5-12 23:09:01 | 显示全部楼层
VB.NET版本的RouterOS API程式碼如下
  1. Imports System.Net.Sockets
  2. Imports System.IO
  3. Imports System.Text

  4. Class MK_ROS
  5.     Private connection As Stream
  6.     Private con As TcpClient

  7.     Public Sub New(ByVal ip As String)
  8.         con = New TcpClient()
  9.         con.Connect(ip, 8728)
  10.         connection = DirectCast(con.GetStream(), Stream)
  11.     End Sub
  12.     Public Sub Close()
  13.         connection.Close()
  14.         con.Close()
  15.     End Sub
  16.     Public Function Login(ByVal username As String, ByVal password As String) As Boolean
  17.         Send("/login", True)
  18.         Dim hash As String = Read()(0).Split(New String() {"ret="}, StringSplitOptions.None)(1)
  19.         Send("/login")
  20.         Send("=name=" & username)
  21.         Send("=response=00" & EncodePassword(password, hash), True)
  22.         If Read()(0) = "!done" Then
  23.             Return True
  24.         Else
  25.             Return False
  26.         End If
  27.     End Function
  28.     Public Sub Send(ByVal co As String)
  29.         Dim bajty As Byte() = Encoding.ASCII.GetBytes(co.ToCharArray())
  30.         Dim velikost As Byte() = EncodeLength(bajty.Length)

  31.         connection.Write(velikost, 0, velikost.Length)
  32.         connection.Write(bajty, 0, bajty.Length)
  33.     End Sub
  34.     Public Sub Send(ByVal co As String, ByVal endsentence As Boolean)
  35.         Dim bajty As Byte() = Encoding.ASCII.GetBytes(co.ToCharArray())
  36.         Dim velikost As Byte() = EncodeLength(bajty.Length)
  37.         connection.Write(velikost, 0, velikost.Length)
  38.         connection.Write(bajty, 0, bajty.Length)
  39.         connection.WriteByte(0)
  40.     End Sub
  41.     Public Function Read() As List(Of String)
  42.         Dim output As New List(Of String)()
  43.         Dim o As String = ""
  44.         Dim tmp As Byte() = New Byte(3) {}
  45.         Dim count As Long
  46.         While True
  47.             tmp(3) = CByte(connection.ReadByte())
  48.             'if(tmp[3] == 220) tmp[3] = (byte)connection.ReadByte(); it sometimes happend to me that
  49.             'mikrotik send 220 as some kind of "bonus" between words, this fixed things, not sure about it though
  50.             If tmp(3) = 0 Then
  51.                 output.Add(o)
  52.                 If o.Substring(0, 5) = "!done" Then
  53.                     Exit While
  54.                 Else
  55.                     o = ""
  56.                     Continue While
  57.                 End If
  58.             Else
  59.                 If tmp(3) < &H80 Then
  60.                     count = tmp(3)
  61.                 Else
  62.                     If tmp(3) < &HC0 Then
  63.                         Dim tmpi As Integer = BitConverter.ToInt32(New Byte() {CByte(connection.ReadByte()), tmp(3), 0, 0}, 0)
  64.                         count = tmpi Xor &H8000
  65.                     Else
  66.                         If tmp(3) < &HE0 Then
  67.                             tmp(2) = CByte(connection.ReadByte())
  68.                             Dim tmpi As Integer = BitConverter.ToInt32(New Byte() {CByte(connection.ReadByte()), tmp(2), tmp(3), 0}, 0)
  69.                             count = tmpi Xor &HC00000
  70.                         Else
  71.                             If tmp(3) < &HF0 Then
  72.                                 tmp(2) = CByte(connection.ReadByte())
  73.                                 tmp(1) = CByte(connection.ReadByte())
  74.                                 Dim tmpi As Integer = BitConverter.ToInt32(New Byte() {CByte(connection.ReadByte()), tmp(1), tmp(2), tmp(3)}, 0)
  75.                                 count = tmpi Xor &HE0000000
  76.                             Else
  77.                                 If tmp(3) = &HF0 Then
  78.                                     tmp(3) = CByte(connection.ReadByte())
  79.                                     tmp(2) = CByte(connection.ReadByte())
  80.                                     tmp(1) = CByte(connection.ReadByte())
  81.                                     tmp(0) = CByte(connection.ReadByte())
  82.                                     count = BitConverter.ToInt32(tmp, 0)
  83.                                 Else
  84.                                     'Error in packet reception, unknown length
  85.                                     Exit While
  86.                                 End If
  87.                             End If
  88.                         End If
  89.                     End If
  90.                 End If
  91.             End If

  92.             For i As Integer = 0 To count - 1
  93.                 o += ChrW(connection.ReadByte())
  94.             Next
  95.         End While
  96.         Return output
  97.     End Function
  98.     Private Function EncodeLength(ByVal delka As Integer) As Byte()
  99.         If delka < &H80 Then
  100.             Dim tmp As Byte() = BitConverter.GetBytes(delka)
  101.             Return New Byte(0) {tmp(0)}
  102.         End If
  103.         If delka < &H4000 Then
  104.             Dim tmp As Byte() = BitConverter.GetBytes(delka Or &H8000)
  105.             Return New Byte(1) {tmp(1), tmp(0)}
  106.         End If
  107.         If delka < &H200000 Then
  108.             Dim tmp As Byte() = BitConverter.GetBytes(delka Or &HC00000)
  109.             Return New Byte(2) {tmp(2), tmp(1), tmp(0)}
  110.         End If
  111.         If delka < &H10000000 Then
  112.             Dim tmp As Byte() = BitConverter.GetBytes(delka Or &HE0000000)
  113.             Return New Byte(3) {tmp(3), tmp(2), tmp(1), tmp(0)}
  114.         Else
  115.             Dim tmp As Byte() = BitConverter.GetBytes(delka)
  116.             Return New Byte(4) {&HF0, tmp(3), tmp(2), tmp(1), tmp(0)}
  117.         End If
  118.     End Function

  119.     Public Function EncodePassword(ByVal Password As String, ByVal hash As String) As String
  120.         Dim hash_byte As Byte() = New Byte(hash.Length / 2 - 1) {}
  121.         For i As Integer = 0 To hash.Length - 2 Step 2
  122.             hash_byte(i / 2) = [Byte].Parse(hash.Substring(i, 2), System.Globalization.NumberStyles.HexNumber)
  123.         Next
  124.         Dim heslo As Byte() = New Byte(1 + Password.Length + (hash_byte.Length - 1)) {}
  125.         heslo(0) = 0
  126.         Encoding.ASCII.GetBytes(Password.ToCharArray()).CopyTo(heslo, 1)
  127.         hash_byte.CopyTo(heslo, 1 + Password.Length)

  128.         Dim hotovo As Byte()
  129.         Dim md5 As System.Security.Cryptography.MD5

  130.         md5 = New System.Security.Cryptography.MD5CryptoServiceProvider()

  131.         hotovo = md5.ComputeHash(heslo)

  132.         'Convert encoded bytes back to a 'readable' string
  133.         Dim navrat As String = ""
  134.         For Each h As Byte In hotovo
  135.             navrat += h.ToString("x2")
  136.         Next
  137.         Return navrat
  138.     End Function
  139. End Class
复制代码
使用方式如下:
  1. Dim mikrotik As New MK_ROS("主機位置(可為IP或Domain)")
  2. If Not mikrotik.Login("帳號", "密碼") Then
  3.     TextBox1.Text &= ("連線失敗")
  4.     mikrotik.Close()
  5.     Return
  6. End If
  7. '要下的指令
  8. mikrotik.Send("/system/identity/print", True)
  9. For Each h As String In mikrotik.Read()
  10.     TextBox1.Text &= h
  11. Next
复制代码
routeros
回复

使用道具 举报

发表于 2009-5-12 23:30:11 | 显示全部楼层
谢谢,容易理解很多了,但依然在翻书-_-"
routeros
回复

使用道具 举报

发表于 2009-5-13 01:32:06 | 显示全部楼层
留个记号,慢慢学习!!
routeros
回复

使用道具 举报

发表于 2009-5-13 20:08:17 | 显示全部楼层
先下载了看看
routeros
回复

使用道具 举报

发表于 2009-5-13 20:11:37 | 显示全部楼层
有空研究研究
routeros
回复

使用道具 举报

发表于 2009-5-13 22:48:09 | 显示全部楼层
顶啊
routeros
回复

使用道具 举报

发表于 2009-5-13 22:59:31 | 显示全部楼层
几个月前已经通过api连上ros,下一步主要做应用了。
routeros
回复

使用道具 举报

发表于 2009-5-14 12:18:44 | 显示全部楼层
有VC的源代码就好了
routeros
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|Archiver|手机版|小黑屋|软路由 ( 渝ICP备15001194号-1|渝公网安备 50011602500124号 )

GMT+8, 2024-9-28 01:38 , Processed in 0.137639 second(s), 7 queries , Gzip On, Redis On.

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表