The simplest way to use the ExecuteCommandSession class is to start the command and wait for the command to complete.
using System.IO; using Xceed.SSH.Client; using Xceed.SSH.Core; using Xceed.SSH.Protocols; using Xceed.FileSystem; namespace DocumentationExamples.SSH { public class ExecuteCommandSession3 { public void Example() { string host = "localhost"; string username = "normal1"; string password = "normal1"; SSHClient ssh = new SSHClient(); // Connect to the host ssh.Connect( host ); try { // Log in ssh.Authenticate( username, password ); // The remote command to execute string command = "dir"; // Create a session that will execute a remote command using( ExecuteCommandSession executeCommandSession = new ExecuteCommandSession( ssh ) ) { // Execute the command on the remote server, waiting until it completes Nullable<int> exitStatus = executeCommandSession.ExecuteCommand( command ); /* Some servers will return exit information like the return code and the 'signal' if the command fails. */ Console.WriteLine( "Command exited with status {0}", exitStatus.HasValue ? exitStatus.ToString() : "(null)" ); SSHChannelExitSignal exitSignal = executeCommandSession.ExitSignal; if( exitSignal != null ) { Console.WriteLine( "Command exited with signal {0}: {1}", exitSignal.SignalName, exitSignal.ErrorMessage ); } } } catch( SSHChannelRequestFailedException ) { /* This exception is thrown by ExecuteCommandSession.Connect(). The most common * cause is the authenticated user does not have the rights to execute commands * on the server. */ } finally { // Always make sure to disconnect from the server when the connection is no longer needed ssh.Disconnect(); } } } }
Imports System.IO Imports Xceed.SSH.Client Imports Xceed.SSH.Core Imports Xceed.SSH.Protocols Imports Xceed.FileSystem Namespace DocumentationExamples.SSH Public Class ExecuteCommandSession3 Public Sub Example() Dim host As String = "localhost" Dim username As String = "normal1" Dim password As String = "normal1" Dim ssh As New SSHClient() ' Connect to the host ssh.Connect(host) Try ' Log in ssh.Authenticate(username, password) ' The remote command to execute Dim command As String = "dir" ' Create a session that will execute a remote command Using executeCommandSession As New ExecuteCommandSession(ssh) ' Execute the command on the remote server, waiting until it completes Dim exitStatus As Nullable(Of Integer) = executeCommandSession.ExecuteCommand(command) ' Some servers will return exit information like the return code and the 'signal' ' if the command fails. Console.WriteLine("Command exited with status {0}",If(exitStatus.HasValue, exitStatus.ToString(), "(null)")) Dim exitSignal As SSHChannelExitSignal = executeCommandSession.ExitSignal If exitSignal IsNot Nothing Then Console.WriteLine("Command exited with signal {0}: {1}", exitSignal.SignalName, exitSignal.ErrorMessage) End If End Using Catch e1 As SSHChannelRequestFailedException ' This exception is thrown by ExecuteCommandSession.Connect(). The most common ' * cause is the authenticated user does not have the rights to execute commands ' * on the server. Finally ' Always make sure to disconnect from the server when the connection is no longer needed ssh.Disconnect() End Try End Sub End Class End Namespace