Ayuda con este código !!

(1/1)

TrashAmbishion:
Holas,

Con este codigo puedo hacer una conexión a un servidor SSH y abrir un PORT FORWARDING DYNAMIC asi navegar por ese Tunnel, todo eso trabaja perfecto lo único que necesito implementar es un metodo para que reconnecte en caso de que falle la conexión, hasta ahora logré manejar el evento OnDisconnect y activar un Timer cada 10 segundos pero tengo mis dudas para llamar al Main del Modulo pues cuando genero un error de conexión el se queda esperando en la consola a que se presione ESC para entonces ejecutar

Código
client.Disconnect(New ProgressHandler)

y

terminar con

Código
SshNet.Shutdown()

supongo que primero tengo que cerrar bien la conexion osea ejecutar esas lineas antes de volver a llamar al Main quizas declarandolas como global y ejecutarlas en el manejador seria la solución?

Saludos y gracias cualquier ayuda...

Código
Imports Bitvise.FlowSshNet
 
 
Public Enum ExitCodes
   Success = 0
   UsageError = 1
   SessionError = 2
   FatalError = 3
End Enum
 
Public Enum DisconnectReason
   Exception = 0
   FlowError = 1
   ConnectionError = 2
   ConnectionLost = 3
   ByServer = 4
   ByClient = 5
End Enum
 
Public Class MyClient
   Inherits Client
 
 
   Dim aTimer As New System.Timers.Timer
 
   Private Sub tick(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs)
       aTimer.Stop()
       FlowSshNet_VbTnl.Main()
   End Sub
 
   Public Sub New()
       AddHandler OnHostKey, AddressOf OnMyHostKey
       AddHandler OnForwardingLog, AddressOf OnMyForwardingLog
       AddHandler OnDisconnect, AddressOf OnMyDisconnect
 
       AddHandler aTimer.Elapsed, AddressOf tick
   End Sub
 
   Sub Main()
       aTimer.AutoReset = True
       aTimer.Interval = 10000 '10 seconds
   End Sub
 
   Public Sub OnMyDisconnect(ByVal sender As Object, ByVal reason As DisconnectReason, ByVal desc As String)
       aTimer.Start()
 
   End Sub
 
   Public Function OnMyHostKey(ByVal sender As Object, ByVal publicKey As PublicKey) As Boolean
       Console.WriteLine("Received the following host key: ")
       Console.WriteLine("  MD5 Fingerprint: {0}", publicKey.GetMd5())
       Console.WriteLine("  Bubble-Babble: {0}", publicKey.GetBubbleBabble())
       Console.WriteLine("  SHA-256: {0}", publicKey.GetSha256())
       Return True
   End Function
 
   Public Sub OnMyForwardingLog(ByVal sender As Object, ByVal log As ForwardingLog)
       Console.WriteLine(log.Desc)
   End Sub
 
End Class
 
 
Module FlowSshNet_VbTnl
 
   Sub OnUncaughtExceptionInEvent(ByVal sender As Object, ByVal fatal As Boolean, ByVal e As System.Exception)
       Console.WriteLine("Error: {0}", e.ToString())
       Environment.Exit(ExitCodes.FatalError)
   End Sub
 
   Function Main() As Integer
       Try
 
           AddHandler SshNet.OnExceptionInEvent, AddressOf OnUncaughtExceptionInEvent
 
           Dim client As MyClient = New MyClient
           client.SetAppName("FlowSshNet_VbTnl")
           client.SetHost("1.1.1.1")
           client.SetPort(22)
           client.SetUserName("user")
           client.SetPassword("password")
 
           client.SetProxyType(ProxyType.HttpConnect)
           client.SetProxyHost("1.1.1.5")
           client.SetProxyPort("8080")
           client.SetProxyUserName("userproxy")
           client.SetProxyPassword("userpass")
 
           Dim progress As ProgressHandler = New ProgressHandler
           client.Connect(progress)
           progress.WaitDone()
 
           If Not progress.Success() Then
               Dim connectStep As UInt32 = progress.GetTaskSpecificStep()
               Dim auxInfo As String = progress.GetAuxInfo()
               Dim connErr As String = ProgressHandler.DescribeConnectError(connectStep, auxInfo)
               Console.WriteLine("{0}", connErr)
           Else
 
               Dim proxy As ProxyForwarding = New ProxyForwarding
               proxy.ListInterface = "127.0.0.1"
               proxy.ListPort = 1080
 
               Dim enableProxyHandler As ForwardingHandler = New ForwardingHandler
               client.EnableProxyForwarding(proxy, enableProxyHandler)
 
               enableProxyHandler.WaitDone()
               If (enableProxyHandler.Success()) Then
                   Console.WriteLine("Proxy forwarding enabled, listening port: {0}", enableProxyHandler.GetListPort().ToString())
               Else
                   Console.WriteLine("Error enabling proxy forwarding: {0}", enableProxyHandler.GetError().Desc)
               End If
 
               Console.WriteLine("Press Esc to stop forwarding")
               While Console.ReadKey(True).Key <> ConsoleKey.Escape
               End While
 
               If (enableProxyHandler.Success()) Then
                   Dim handler As ForwardingHandler = New ForwardingHandler
                   client.DisableProxyForwarding(handler)
 
                   handler.WaitDone()
                   If (handler.Success()) Then
                       Console.WriteLine("Proxy forwarding disabled")
                   Else
                       Console.WriteLine("Error disabling proxy forwarding: {0}", handler.GetError().Desc)
                   End If
               End If
 
               client.Disconnect(New ProgressHandler)
 
               Console.WriteLine("Press Esc to exit")
               While Console.ReadKey(True).Key <> ConsoleKey.Escape
               End While
 
           End If
 
       Catch e As System.Exception
           Console.WriteLine(e.Message)
           Return ExitCodes.FatalError
       Finally
           SshNet.Shutdown()
       End Try
 
       Return ExitCodes.Success
   End Function
 
End Module
 
 

TrashAmbishion:
Ok lo solucioné  :xD :xD :xD ;-) ;-) ;-) ;-)

Aquí esta como lo hice, imagino esta chapuza diganme la forma correcta....

Estuve tratando de migrarlo para una aplicación Form y tuve varios problemas a la hora de capturar los datos del servidor, parece que la clase principal espera entregar esos datos a una consola, alguna idea....

Nota: La libreria es libre siempre y cuando se respete los acuerdos del autor la pueden descargar de http://www.bitvise.com busquen SshLibrary


Código
Imports System.Threading
Imports Bitvise.FlowSshNet
 
 
Public Enum ExitCodes
   Success = 0
   UsageError = 1
   SessionError = 2
   FatalError = 3
End Enum
 
Public Enum DisconnectReason
   Exception = 0
   FlowError = 1
   ConnectionError = 2
   ConnectionLost = 3
   ByServer = 4
   ByClient = 5
End Enum
 
Public Class MyClient
   Inherits Client
 
   Public Sub New()
       AddHandler OnHostKey, AddressOf OnMyHostKey
       AddHandler OnForwardingLog, AddressOf OnMyForwardingLog
   End Sub
 
   Public Function OnMyHostKey(ByVal sender As Object, ByVal publicKey As PublicKey) As Boolean
       Console.WriteLine("Received the following host key: ")
       Console.WriteLine("  MD5 Fingerprint: {0}", publicKey.GetMd5())
       Console.WriteLine("  Bubble-Babble: {0}", publicKey.GetBubbleBabble())
       Console.WriteLine("  SHA-256: {0}", publicKey.GetSha256())
       Return True
   End Function
 
   Public Sub OnMyForwardingLog(ByVal sender As Object, ByVal log As ForwardingLog)
       Console.WriteLine(log.Desc)
   End Sub
 
End Class
 
 
Module FlowSshNet_VbTnl
 
   Dim client As MyClient = New MyClient
   Dim progress As ProgressHandler
 
   Sub OnUncaughtExceptionInEvent(ByVal sender As Object, ByVal fatal As Boolean, ByVal e As System.Exception)
       Console.WriteLine("Error: {0}", e.ToString())
       Environment.Exit(ExitCodes.FatalError)
   End Sub
 
   Public Sub OnMyDisconnect(ByVal sender As Object, ByVal reason As DisconnectReason, ByVal desc As String)
       If reason <> 5 Then     'Exepto cuando sea una desconexión voluntaria, reconecto...
           Console.WriteLine("Reconectando...")
 
           Reconectar()     'Espero 10 segundos
       End If
   End Sub
 
   Sub New()
       AddHandler SshNet.OnExceptionInEvent, AddressOf OnUncaughtExceptionInEvent
       AddHandler client.OnDisconnect, AddressOf OnMyDisconnect
   End Sub
 
   Function Main() As Integer
       Try
 
           If IsNothing(client) Then
               client = New MyClient
           End If
 
           client.SetAppName("FlowSshNet_VbTnl")
           client.SetHost("1.1.1.1")
           client.SetPort(22)
           client.SetUserName("user")
           client.SetPassword("pass")
 
           client.SetProxyType(ProxyType.HttpConnect)
           client.SetProxyHost("1.1.1.5")
           client.SetProxyPort("8080")
           client.SetProxyUserName("userproxy")
           client.SetProxyPassword("proxypass")
 
           progress = New ProgressHandler
           client.Connect(progress)
           progress.WaitDone()
 
           If Not progress.Success() Then
               Dim connectStep As UInt32 = progress.GetTaskSpecificStep()
               Dim auxInfo As String = progress.GetAuxInfo()
               Dim connErr As String = ProgressHandler.DescribeConnectError(connectStep, auxInfo)
               Console.WriteLine("{0}", connErr)
 
               Reconectar()
 
           Else
 
               Dim proxy As ProxyForwarding = New ProxyForwarding
               proxy.ListInterface = "127.0.0.1"
               proxy.ListPort = 1080
 
               Dim enableProxyHandler As ForwardingHandler = New ForwardingHandler
               client.EnableProxyForwarding(proxy, enableProxyHandler)
 
               enableProxyHandler.WaitDone()
               If (enableProxyHandler.Success()) Then
                   Console.WriteLine("Proxy forwarding enabled, listening port: {0}", enableProxyHandler.GetListPort().ToString())
               Else
                   Console.WriteLine("Error enabling proxy forwarding: {0}", enableProxyHandler.GetError().Desc)
               End If
 
               Console.WriteLine("Press Esc to stop forwarding")
               While Console.ReadKey(True).Key <> ConsoleKey.Escape
               End While
 
               If (enableProxyHandler.Success()) Then
                   Dim handler As ForwardingHandler = New ForwardingHandler
                   client.DisableProxyForwarding(handler)
 
                   handler.WaitDone()
                   If (handler.Success()) Then
                       Console.WriteLine("Proxy forwarding disabled")
                   Else
                       Console.WriteLine("Error disabling proxy forwarding: {0}", handler.GetError().Desc)
                   End If
               End If
 
               client.Disconnect(New ProgressHandler)
 
           End If
 
       Catch e As System.Exception
           Console.WriteLine(e.Message)
           Return ExitCodes.FatalError
       Finally
           SshNet.Shutdown()
       End Try
 
       Return ExitCodes.Success
   End Function
 
   Sub Reconectar()
 
       Dim cursT As Integer
       Dim cursL As Integer
       cursT = Console.CursorTop
       cursL = Console.CursorLeft
       Const countDownFrom As Integer = 10
       Dim cdfW As Integer = countDownFrom.ToString.Length + 1
       For x As Integer = countDownFrom To 0 Step -1
           Console.SetCursorPosition(cursL, cursT)
           Console.Write(x.ToString.PadLeft(cdfW, " "c))
           Thread.Sleep(1000) 'for example only
       Next
 
       client.Disconnect(New ProgressHandler)
 
       progress = Nothing
 
       client = Nothing
 
       Main()
 
   End Sub
 
End Module
 

Navegación

[0] Índice de Mensajes