SSHNet和MySQL

时间:2016-04-29 14:20:32

标签: mysql vb.net ssh

我正在尝试将SSHNet集成到.Net桌面应用程序中。我能够建立SSH连接,但是当我尝试连接MySql(远程服务器)时,我得到流读取错误。我使用了与Toad中使用的SQL相同的配置,以及我在BitVise中使用的SSH的相同配置。

我的猜测是没有握手确立。我注意到在Bitvise Client中,服务器和客户端之间存在密钥交换,我不知道如何实现这种交换,如果这确实是问题。我使用不同的配置文件(用户名/密码/服务器)进行测试。

如果有人可以帮助我,我会很高兴

这是我使用的代码:

Public Structure utProfileDef
    Dim IP As String
    Dim sshUSR As String
    Dim sshPWD As String
    Dim DBUsr As String
    Dim DBPwd As String
    Dim DBName As String
    Dim DBPort As Integer
    Dim SQL As String
    Dim sshPort As Integer
End Structure

Public Class Form1
    Dim WithEvents oSSHClient As SshClient

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim utProfile(1) As utProfileDef

        With utProfile(0)
            .IP = "myserver1"
            .sshPort = 22
            .sshUSR = "myuser"
            .sshPWD = "mypassword"
            .DBName = "mydatabase"
            .DBUsr = "dbuser"
            .DBPwd = "dbpassword"
            .DBPort = 3308
            .SQL = "SELECT * FROM Categorie"
        End With

        With utProfile(1)
            .IP = "myserver2"
            .sshPort = 22000
            .sshUSR = "myuser1"
            .sshPWD = "mypassword1"
            .DBName = "mydatabase1"
            .DBUsr = "dbuser1"
            .DBPwd = "dbpassword1"
            .DBPort = 22001
            .SQL = "SELECT * FROM Scan"
        End With

        Call sshClientConnect(utProfile(0))
    End Sub

    Protected Sub DBConnect(utCurrentProfile As utProfileDef)
        Dim oCnn As New MySqlConnection
        Dim oCMD As MySqlCommand
        Dim oDBR As MySqlDataReader

        oCnn.ConnectionString = "server=127.0.0.1; port=" & utCurrentProfile.DBPort & _
                                "; user id=" & utCurrentProfile.DBUsr & _
                                "; password=" & utCurrentProfile.DBPwd & _
                                "; database=" & utCurrentProfile.DBName & ";Pooling=true;"

        Try
            oCnn.Open()
        Catch ex As Exception
            MsgBox(ex.Message)
            End
        End Try

        oCMD = New MySqlCommand(utCurrentProfile.SQL, oCnn)
        oDBR = oCMD.ExecuteReader

        While oDBR.Read
            MsgBox(oDBR.Item(0))
        End While

        oDBR.Close()
        oCnn.Close()
    End Sub


    Protected Sub sshClientConnect(utCurrentProfile As utProfileDef)
        Dim oPort As Renci.SshNet.ForwardedPortLocal


        oSSHClient = New SshClient(utCurrentProfile.IP, utCurrentProfile.sshPort, utCurrentProfile.sshUSR, utCurrentProfile.sshPWD)
        oSSHClient.Connect()
        oPort = New Renci.SshNet.ForwardedPortLocal("127.0.0.1", utCurrentProfile.DBPort, "127.0.0.1", utCurrentProfile.DBPort)
        oSSHClient.AddForwardedPort(oPort)
        oPort.Start()

        MsgBox(IIf(oSSHClient.IsConnected, "SSH Connected to " & utCurrentProfile.IP, "SSH not connected"))


        Call DBConnect(utCurrentProfile)

        oPort.Stop()
        oPort.Dispose()
        oSSHClient.Disconnect()
    End Sub

    Private Sub oSSHClient_HostKeyReceived(sender As Object, e As Common.HostKeyEventArgs) Handles oSSHClient.HostKeyReceived
        e.CanTrust = True
    End Sub

由于

0 个答案:

没有答案