全局代码里面定义了一个sendCommand函数, 一个WriteStream过程,一个ReadStream函数。代码没问题,要引入Renci.SshNet.dll。(连接linux打指令用的)
怎么在外面执行WriteStream过程中的writer.WriteLine(cmd)。?
Public Function sendCommand(ByVal customCMD As String) As StringBuilder
Dim connInfo As New Renci.SshNet.PasswordConnectionInfo(IP,账户, 密码)
Dim sshClient As New Renci.SshNet.SshClient(connInfo)
sshClient.Connect() '启动连接
Dim stream = sshclient.CreateShellStream("customCommand", 80, 24, 800, 600, 1024)
Dim answer As StringBuilder
Dim reader = New IO.StreamReader(CType(stream,System.IO.stream))
Dim writer = New IO.StreamWriter(CType(stream,System.IO.stream))
writer.AutoFlush = True
WriteStream(customCMD, writer, stream)
answer =ReadStream(reader)
Return answer
End Function
Sub WriteStream(ByVal cmd As String, ByVal writer As IO.StreamWriter, ByVal stream As Renci.SshNet.ShellStream)
writer.WriteLine(cmd)
While stream.Length = 0
Threading.Thread.Sleep(500)
End While
End Sub
Function ReadStream(ByVal reader As IO.StreamReader) As StringBuilder
Dim result As StringBuilder = New StringBuilder()
Dim line As String
While (CSharpImpl.__Assign(line, reader.ReadLine())) IsNot Nothing
result.AppendLine(line)
End While
Return result
End Function
Public Class CSharpImpl
<Obsolete("Please refactor calling code to use normal Visual Basic assignment")>
Shared Function __Assign(Of T)(ByRef target As T, value As T) As T
target = value
Return value
End Function
End Class
[此贴子已经被作者于2021/6/27 11:11:51编辑过]