ชุมชนคนรักภาษาเบสิค - Visual Basic Community

 ลืมรหัสผ่าน
 ลงทะเบียน
ค้นหา
ดู: 3778|ตอบกลับ: 1

[VB.NET] โค้ดโปรแกรมการเชื่อมต่อกับ MikroTik และการสั่งรัน Command

[คัดลอกลิงก์]

309

กระทู้

500

โพสต์

6030

เครดิต

ผู้ดูแลระบบ

ทองก้อน ทับทิมกรอบ

Rank: 9Rank: 9Rank: 9

เครดิต
6030




ปฐมบทชุดแรกที่แอดมินก็พึ่งได้เคยทำการเขียนโค้ดโปรแกรมด้วย VB.NET เพื่อทำการเชื่อมต่อเข้ากับ MikroTik และทำการรันทดสอบคอมมานด์ เพื่อดูคุณสมบัติต่างๆภายในตัว MikroTik


มาดูโค้ดกันเถอะ ...
  1. Public Class frmConnectMikroTik

  2.     Private Sub frmConnectMikroTik_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
  3.         txtIPAddress.Text = "IP ADDRESS"
  4.         txtPort.Text = "8728"
  5.         txtUsername.Text = "admin"
  6.         txtPassword.Text = "PASSWORD"
  7.         Call OnOff(False)
  8.     End Sub

  9.     Sub OnOff(ByVal blnState As Boolean)
  10.         If blnState Then
  11.             GroupBox1.Text = " Connect to MikroTik ... "
  12.             txtIPAddress.Enabled = False
  13.             txtPort.Enabled = False
  14.             txtUsername.Enabled = False
  15.             txtPassword.Enabled = False
  16.             btnConnect.Enabled = False
  17.             btnClear.Enabled = True
  18.             btnSend.Enabled = True
  19.             txtCmd.Enabled = True
  20.         Else
  21.             GroupBox1.Text = " Log in "
  22.             txtIPAddress.Enabled = True
  23.             txtPort.Enabled = True
  24.             txtUsername.Enabled = True
  25.             txtPassword.Enabled = True
  26.             btnConnect.Enabled = True
  27.             btnClear.Enabled = False
  28.             btnSend.Enabled = False
  29.             txtCmd.Enabled = False
  30.         End If
  31.     End Sub

  32.     Function LoginMikrotik(ByVal IP As String, ByVal UN As String, ByVal PW As String) As Boolean
  33.         Dim IPAddress As System.Net.IPAddress = System.Net.IPAddress.Parse(IP)
  34.         Dim mk = New Mikrotik(IPAddress, 8728)
  35.         If Not mk.Login(Trim(txtUsername.Text), Trim(txtPassword.Text)) Then
  36.             Return False
  37.         Else
  38.             Return True
  39.         End If
  40.     End Function

  41.     Private Sub btnConnect_Click(sender As System.Object, e As System.EventArgs) Handles btnConnect.Click
  42.         If Not LoginMikrotik(txtIPAddress.Text.Trim, (txtUsername.Text), Trim(txtPassword.Text)) Then
  43.             OnOff(False)
  44.             MessageBox.Show("Sorry !!! You can't log in.", "Report Status", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
  45.             Return
  46.         Else
  47.             'MessageBox.Show("Welcome to Mikrotik system.", "Report Status", MessageBoxButtons.OK, MessageBoxIcon.Information)
  48.             OnOff(True)
  49.             txtCmd.Focus()
  50.             '// SAMPLE
  51.             ' /interface/print
  52.             '/ip/arp/print
  53.             '/system/resource/getall
  54.             txtCmd.Text = "/system/resource/print"
  55.         End If
  56.     End Sub

  57.     Private Sub txtData_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtResult.KeyPress
  58.         e.Handled = True
  59.     End Sub

  60.     Private Sub btnSend_Click(sender As System.Object, e As System.EventArgs) Handles btnSend.Click
  61.         If txtCmd.Text.Trim.Length = 0 Then Return
  62.         '//
  63.         Dim IPAddress As System.Net.IPAddress = System.Net.IPAddress.Parse(Trim(txtIPAddress.Text))
  64.         Dim mk = New Mikrotik(IPAddress, 8728)
  65.         Try
  66.             If mk.Login(Trim(txtUsername.Text), Trim(txtPassword.Text)) Then
  67.                 txtResult.Clear()
  68.                 mk.Send(Trim(txtCmd.Text), True)
  69.                 Dim strLog As String = String.Empty
  70.                 For Each row In mk.Read
  71.                     strLog = strLog & row & vbCrLf
  72.                 Next
  73.                 txtResult.Text = strLog
  74.             End If

  75.         Catch ex As Exception
  76.             MessageBox.Show(ex.Message)
  77.         End Try
  78.     End Sub

  79.     Private Sub btnClear_Click(sender As System.Object, e As System.EventArgs) Handles btnClear.Click
  80.         OnOff(False)
  81.         txtResult.Clear()
  82.     End Sub
  83. End Class
คัดลอกไปที่คลิปบอร์ด



API in VB dot NET ... (https://wiki.mikrotik.com/wiki/API_in_VB_dot_NET)
  1. '// ORIGINAL ... https://wiki.mikrotik.com/wiki/API_in_VB_dot_NET

  2. Public Class Mikrotik
  3.     Dim tcpStream As IO.Stream
  4.     Dim tcpCon As New Net.Sockets.TcpClient

  5.     Public Sub New(ByVal IPAddr As System.Net.IPAddress, Optional ByVal port As Integer = -1)
  6.         tcpCon.Connect(IPAddr, If(port = -1, 8728, port))
  7.         tcpStream = tcpCon.GetStream()
  8.     End Sub

  9.     Public Sub Close()
  10.         tcpStream.Close()
  11.         tcpCon.Close()
  12.     End Sub

  13.     Public Function Login(ByVal user As String, ByVal pass As String) As Boolean
  14.         Send("/login", True)
  15.         Dim hash = Read()(0).Split(New String() {"ret="}, StringSplitOptions.None)(1)
  16.         Send("/login")
  17.         Send("=name=" + user)
  18.         Send("=response=00" + EncodePassword(pass, hash), True)
  19.         Dim res = Read()
  20.         If (res(0) = "!done") Then Return True Else Return False
  21.     End Function

  22.     Function EncodePassword(ByVal pass As String, ByVal challange As String) As String
  23.         Dim hash_byte(challange.Length / 2 - 1) As Byte
  24.         For i = 0 To challange.Length - 2 Step 2
  25.             hash_byte(i / 2) = Byte.Parse(challange.Substring(i, 2), Globalization.NumberStyles.HexNumber)
  26.         Next
  27.         Dim response(pass.Length + hash_byte.Length) As Byte
  28.         response(0) = 0
  29.         System.Text.Encoding.ASCII.GetBytes(pass.ToCharArray()).CopyTo(response, 1)
  30.         hash_byte.CopyTo(response, 1 + pass.Length)


  31.         Dim md5 = New System.Security.Cryptography.MD5CryptoServiceProvider()

  32.         Dim hash = md5.ComputeHash(response)

  33.         Dim hashStr As New System.Text.StringBuilder()
  34.         For Each h In hash
  35.             hashStr.Append(h.ToString("x2"))
  36.         Next
  37.         Return hashStr.ToString()
  38.     End Function

  39.     Public Sub Send(ByVal command As String, Optional ByVal EndSentence As Boolean = False)
  40.         Dim bytes = System.Text.Encoding.ASCII.GetBytes(command.ToCharArray())
  41.         Dim size = EncodeLength(bytes.Length)

  42.         tcpStream.Write(size, 0, size.Length)
  43.         tcpStream.Write(bytes, 0, bytes.Length)
  44.         If EndSentence Then tcpStream.WriteByte(0)
  45.     End Sub

  46.     Public Function Read() As List(Of String)
  47.         Dim output As New List(Of String)
  48.         Dim o = ""
  49.         Dim tmp(4) As Byte
  50.         Dim count As Long

  51.         While True
  52.             tmp(3) = tcpStream.ReadByte()
  53.             Select Case tmp(3)
  54.                 Case 0
  55.                     output.Add(o)
  56.                     If o.Substring(0, 5) = "!done" Then
  57.                         Exit While
  58.                     Else
  59.                         o = ""
  60.                         Continue While
  61.                     End If
  62.                 Case Is < &H80
  63.                     count = tmp(3)
  64.                 Case Is < &HC0
  65.                     count = BitConverter.ToInt32(New Byte() {tcpStream.ReadByte(), tmp(3), 0, 0}, 0) ^ &H8000
  66.                 Case Is < &HE0
  67.                     tmp(2) = tcpStream.ReadByte()
  68.                     count = BitConverter.ToInt32(New Byte() {tcpStream.ReadByte(), tmp(2), tmp(3), 0}, 0) ^ &HC00000
  69.                 Case Is < &HF0
  70.                     tmp(2) = tcpStream.ReadByte()
  71.                     tmp(1) = tcpStream.ReadByte()
  72.                     count = BitConverter.ToInt32(New Byte() {tcpStream.ReadByte(), tmp(1), tmp(2), tmp(3)}, 0) ^ &HE0000000
  73.                 Case &HF0
  74.                     tmp(3) = tcpStream.ReadByte()
  75.                     tmp(2) = tcpStream.ReadByte()
  76.                     tmp(1) = tcpStream.ReadByte()
  77.                     tmp(0) = tcpStream.ReadByte()
  78.                     count = BitConverter.ToInt32(tmp, 0)
  79.                 Case Else
  80.                     Exit While   'err
  81.             End Select

  82.             For i = 0 To count - 1
  83.                 o += ChrW(tcpStream.ReadByte())
  84.             Next
  85.         End While
  86.         Return output
  87.     End Function

  88.     Function EncodeLength(ByVal l As Integer) As Byte()
  89.         If l < &H80 Then
  90.             Dim tmp = BitConverter.GetBytes(l)
  91.             Return New Byte() {tmp(0)}
  92.         ElseIf l < &H4000 Then
  93.             Dim tmp = BitConverter.GetBytes(l Or &H8000)
  94.             Return New Byte() {tmp(1), tmp(0)}
  95.         ElseIf l < &H200000 Then
  96.             Dim tmp = BitConverter.GetBytes(l Or &HC00000)
  97.             Return New Byte() {tmp(2), tmp(1), tmp(0)}
  98.         ElseIf l < &H10000000 Then
  99.             Dim tmp = BitConverter.GetBytes(l Or &HE0000000)
  100.             Return New Byte() {tmp(3), tmp(2), tmp(1), tmp(0)}
  101.         Else
  102.             Dim tmp = BitConverter.GetBytes(l)
  103.             Return New Byte() {&HF0, tmp(3), tmp(2), tmp(1), tmp(0)}
  104.         End If
  105.     End Function
  106. End Class
คัดลอกไปที่คลิปบอร์ด


ดาวน์โหลดโค้ดฉบับเต็ม VB.NET (2010) ได้ที่นี่ ...

ขออภัย! โพสต์นี้มีไฟล์แนบหรือรูปภาพที่ไม่ได้รับอนุญาตให้คุณเข้าถึง

คุณจำเป็นต้อง ลงชื่อเข้าใช้ เพื่อดาวน์โหลดหรือดูไฟล์แนบนี้ คุณยังไม่มีบัญชีใช่ไหม? ลงทะเบียน

x
สิ่งที่ดีกว่าการให้ คือการให้แบบไม่มีที่สิ้นสุด

0

กระทู้

58

โพสต์

10

เครดิต

Member

Rank: 2

เครดิต
10
โพสต์ 2022-10-25 15:38:11 | ดูโพสต์ทั้งหมด

ขอบคุณครับ
ขออภัย! คุณไม่ได้รับสิทธิ์ในการดำเนินการในส่วนนี้ กรุณาเลือกอย่างใดอย่างหนึ่ง ลงชื่อเข้าใช้ | ลงทะเบียน

รายละเอียดเครดิต

ข้อความล้วน|อุปกรณ์พกพา|ประวัติการแบน|G2GNet.com  

GMT+7, 2024-4-19 22:02 , Processed in 0.225427 second(s), 4 queries , File On.

Powered by Discuz! X3.4, Rev.62

Copyright © 2001-2020 Tencent Cloud.

ตอบกระทู้ ขึ้นไปด้านบน ไปที่หน้ารายการกระทู้