Sending data

Top  Previous  Next

Once a connection has been established and user authentication is complete you can then proceed to send data to the SSH server. Sending data to the SSH server is accomplished by getting the Stream from the Ssh instance and then writing data to that stream.

 

Example

 

[C#]

 

// connect to server

ssh.Connect();

 

// get output stream

Stream output = ssh.GetStream();

 

// read data from local console and send to server

string line = null;

while((line = Console.ReadLine()) != null && connected) {        

 byte[] data = System.Text.Encoding.UTF8.GetBytes(line);

 byte[] nl = System.Text.Encoding.UTF8.GetBytes("\r\n");

 output.Write(data,0,data.Length);

 output.Write(nl,0,nl.Length);

 try {

         output.Flush();

 } catch(Exception e) {

         break;

 }

 

}

 

 

[VB]

 

' connect to server

ssh.Connect()

 

' get output stream from server

Dim Output As Stream = ssh.GetStream

 

' read data from local console and send to server

Dim Line As String = Console.ReadLine

While Not (Line Is Nothing) AndAlso connected

 Dim data As Byte() = System.Text.Encoding.UTF8.GetBytes(Line)

 Dim nl As Byte() = System.Text.Encoding.UTF8.GetBytes("\r\n")

 output.Write(data, 0, data.Length)

 output.Write(nl, 0, nl.Length)

 Try

         output.Flush()

 Catch e As Exception

         ' break

 End Try

 Line = Console.ReadLine

End While