sany_ok

Untitled

Nov 7th, 2014
367
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 2.56 KB | None | 0 0
  1. (C) Alexander B. Prokopyev, 2014, aulix.com
  2.  
  3. Module Test
  4.     Function Run()
  5.         AddHandler MainModule.Bash.DataReceived, AddressOf Test.Bash_DataReceived
  6.         Bash.ExecAsyncCommand("dstat 1 5; echo stop$[1+2]", "stop3")
  7.     End Function
  8.  
  9.  
  10.     Private Sub Bash_DataReceived(ByVal S As String)
  11.         Console.WriteLine(S)
  12.     End Sub
  13.  
  14. End Module
  15.  
  16. Public Class AutoPenguin
  17.     Public WithEvents Shell As Renci.SshNet.ShellStream
  18.     Public SR As IO.StreamReader
  19.     Public SW As IO.StreamWriter
  20.     Public Function OpenShell()
  21.         If SSHClient.IsConnected Then
  22.             'Dim sshOutput As New Text.StringBuilder
  23.             Try
  24.                 Shell = SSHClient.CreateShellStream("dumb", 80, 24, 800, 600, 1024)
  25.  
  26.                 SR = New IO.StreamReader(Shell)
  27.  
  28.                 SW = New IO.StreamWriter(Shell)
  29.                 SW.AutoFlush = True
  30.             Catch ex As Exception
  31.                 Console.WriteLine(ex.ToString)
  32.             End Try
  33.         End If
  34.     End Function
  35.  
  36.     Public Function F1(ByVal S As String)
  37.     End Function
  38.     Public StopOnText As String
  39.     Public AR As IAsyncResult
  40.     Function ExecAsyncCommand(ByVal CmdText As String, ByVal StopOnText_ As String)
  41.         OpenShell()
  42.         Shell.WriteLine(CmdText)
  43.         StopOnText = StopOnText_
  44.         AR = Shell.BeginExpect(New Renci.SshNet.ExpectAction(StopOnText, AddressOf F1))
  45.         AR.AsyncWaitHandle.WaitOne()
  46.         'Shell.Expect(StopOnText)
  47.     End Function
  48.     Public Event DataReceived(ByVal S As String)
  49.     Private Sub Shell_DataReceived(ByVal sender As Object, ByVal e As Renci.SshNet.Common.ShellDataEventArgs) Handles Shell.DataReceived
  50.         Dim S As String
  51.         'While Not Shell.DataAvailable
  52.         '    'Shell.WaitHandle.WaitOne()
  53.         'End While
  54.         If Shell.DataAvailable Then
  55.             S = Shell.Read()
  56.             Debug.Print("Shell_DataReceived: " & S)
  57.             'RaiseEvent DataReceived(S)
  58.  
  59.             If InStr(S, StopOnText, CompareMethod.Text) > 0 Then
  60.                 'Dim AR = New Renci.SshNet.ExpectAsyncResult
  61.  
  62.                 AR.AsyncWaitHandle.Close()
  63.                 Shell.EndExpect(AR)
  64.                 'Shell.Close()
  65.             End If
  66.         End If
  67.     End Sub
  68.  
  69.     Public Event ErrorOccurred(ByVal e As Renci.SshNet.Common.ExceptionEventArgs)
  70.     Private Sub ShStream_ErrorOccurred(ByVal sender As Object, ByVal e As Renci.SshNet.Common.ExceptionEventArgs) Handles Shell.ErrorOccurred
  71.         Debug.Print("ErrorOccurred: " & e.ToString)
  72.         RaiseEvent ErrorOccurred(e)
  73.     End Sub
  74.  
  75. end class
Advertisement
Add Comment
Please, Sign In to add comment