Table of Contents

Class Session

Namespace
Renci.SshNet
Assembly
Renci.SshNet.dll

Provides functionality to connect and interact with SSH server.

public class Session : IDisposable

Inheritance

Implements

Inherited Members

Properties

ClientInitMessage

Gets the client init message.

public Message ClientInitMessage { get; }

Property Value

Message

ClientVersion

Gets the client version string.

public string ClientVersion { get; }

Property Value

string

ConnectionInfo

Gets the connection info.

public ConnectionInfo ConnectionInfo { get; }

Property Value

ConnectionInfo

IsConnected

Gets a value indicating whether the session is connected.

public bool IsConnected { get; }

Property Value

bool

Remarks

This methods returns true in all but the following cases:

  • The Renci.SshNet.Session is disposed.
  • The SSH_MSG_DISCONNECT message - which is used to disconnect from the server - has been sent.
  • The client has not been authenticated successfully.
  • The listener thread - which is used to receive messages from the server - has stopped.
  • The socket used to communicate with the server is no longer connected.

ServerVersion

Gets the server version string.

public string ServerVersion { get; }

Property Value

string

SessionId

Gets the session id.

public byte[] SessionId { get; }

Property Value

byte[]

SessionSemaphore

Gets the session semaphore that controls session channels.

public SemaphoreSlim SessionSemaphore { get; }

Property Value

SemaphoreSlim

Methods

Connect()

Connects to the server.

public void Connect()

Exceptions

SocketException

Socket connection to the SSH server or proxy server could not be established, or an error occurred while resolving the hostname.

SshConnectionException

SSH session could not be established.

SshAuthenticationException

Authentication of SSH session failed.

ProxyException

Failed to establish proxy connection.

ConnectAsync(CancellationToken)

Asynchronously connects to the server.

public Task ConnectAsync(CancellationToken cancellationToken)

Parameters

cancellationToken CancellationToken

The System.Threading.CancellationToken to observe.

Returns

Task

A System.Threading.Tasks.Task that represents the asynchronous connect operation.

Exceptions

SocketException

Socket connection to the SSH server or proxy server could not be established, or an error occurred while resolving the hostname.

SshConnectionException

SSH session could not be established.

SshAuthenticationException

Authentication of SSH session failed.

ProxyException

Failed to establish proxy connection.

Disconnect()

Disconnects from the server.

public void Disconnect()

Remarks

This sends a SSH_MSG_DISCONNECT message to the server, waits for the server to close the socket on its end and subsequently closes the client socket.

Dispose()

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

public void Dispose()

Dispose(bool)

Releases unmanaged and - optionally - managed resources.

protected virtual void Dispose(bool disposing)

Parameters

disposing bool

true to release both managed and unmanaged resources; false to release only unmanaged resources.

RegisterMessage(string)

Registers SSH message with the session.

public void RegisterMessage(string messageName)

Parameters

messageName string

The name of the message to register with the session.

UnRegisterMessage(string)

Unregister SSH message from the session.

public void UnRegisterMessage(string messageName)

Parameters

messageName string

The name of the message to unregister with the session.

ChannelCloseReceived

Occurs when Renci.SshNet.Messages.Connection.ChannelCloseMessage message received

public event EventHandler<MessageEventArgs<ChannelCloseMessage>> ChannelCloseReceived

Event Type

EventHandler<MessageEventArgs<ChannelCloseMessage>>

ChannelDataReceived

Occurs when Renci.SshNet.Messages.Connection.ChannelDataMessage message received

public event EventHandler<MessageEventArgs<ChannelDataMessage>> ChannelDataReceived

Event Type

EventHandler<MessageEventArgs<ChannelDataMessage>>

ChannelEofReceived

Occurs when Renci.SshNet.Messages.Connection.ChannelEofMessage message received

public event EventHandler<MessageEventArgs<ChannelEofMessage>> ChannelEofReceived

Event Type

EventHandler<MessageEventArgs<ChannelEofMessage>>

ChannelExtendedDataReceived

Occurs when Renci.SshNet.Messages.Connection.ChannelExtendedDataMessage message received

public event EventHandler<MessageEventArgs<ChannelExtendedDataMessage>> ChannelExtendedDataReceived

Event Type

EventHandler<MessageEventArgs<ChannelExtendedDataMessage>>

ChannelFailureReceived

Occurs when Renci.SshNet.Messages.Connection.ChannelFailureMessage message received

public event EventHandler<MessageEventArgs<ChannelFailureMessage>> ChannelFailureReceived

Event Type

EventHandler<MessageEventArgs<ChannelFailureMessage>>

ChannelOpenConfirmationReceived

Occurs when Renci.SshNet.Messages.Connection.ChannelOpenConfirmationMessage message received

public event EventHandler<MessageEventArgs<ChannelOpenConfirmationMessage>> ChannelOpenConfirmationReceived

Event Type

EventHandler<MessageEventArgs<ChannelOpenConfirmationMessage>>

ChannelOpenFailureReceived

Occurs when Renci.SshNet.Messages.Connection.ChannelOpenFailureMessage message received

public event EventHandler<MessageEventArgs<ChannelOpenFailureMessage>> ChannelOpenFailureReceived

Event Type

EventHandler<MessageEventArgs<ChannelOpenFailureMessage>>

ChannelOpenReceived

Occurs when Renci.SshNet.Messages.Connection.ChannelOpenMessage message received

public event EventHandler<MessageEventArgs<ChannelOpenMessage>> ChannelOpenReceived

Event Type

EventHandler<MessageEventArgs<ChannelOpenMessage>>

ChannelRequestReceived

Occurs when Renci.SshNet.Messages.Connection.ChannelRequestMessage message received

public event EventHandler<MessageEventArgs<ChannelRequestMessage>> ChannelRequestReceived

Event Type

EventHandler<MessageEventArgs<ChannelRequestMessage>>

ChannelSuccessReceived

Occurs when Renci.SshNet.Messages.Connection.ChannelSuccessMessage message received

public event EventHandler<MessageEventArgs<ChannelSuccessMessage>> ChannelSuccessReceived

Event Type

EventHandler<MessageEventArgs<ChannelSuccessMessage>>

ChannelWindowAdjustReceived

Occurs when Renci.SshNet.Messages.Connection.ChannelWindowAdjustMessage message received

public event EventHandler<MessageEventArgs<ChannelWindowAdjustMessage>> ChannelWindowAdjustReceived

Event Type

EventHandler<MessageEventArgs<ChannelWindowAdjustMessage>>

Disconnected

Occurs when session has been disconnected from the server.

public event EventHandler<EventArgs> Disconnected

Event Type

EventHandler<EventArgs>

ErrorOccured

Occurs when an error occurred.

public event EventHandler<ExceptionEventArgs> ErrorOccured

Event Type

EventHandler<ExceptionEventArgs>

HostKeyReceived

Occurs when host key received.

public event EventHandler<HostKeyEventArgs> HostKeyReceived

Event Type

EventHandler<HostKeyEventArgs>

RequestFailureReceived

Occurs when Renci.SshNet.Messages.Connection.RequestFailureMessage message received

public event EventHandler<MessageEventArgs<RequestFailureMessage>> RequestFailureReceived

Event Type

EventHandler<MessageEventArgs<RequestFailureMessage>>

RequestSuccessReceived

Occurs when Renci.SshNet.Messages.Connection.RequestSuccessMessage message received

public event EventHandler<MessageEventArgs<RequestSuccessMessage>> RequestSuccessReceived

Event Type

EventHandler<MessageEventArgs<RequestSuccessMessage>>

ServerIdentificationReceived

Occurs when server identification received.

public event EventHandler<SshIdentificationEventArgs> ServerIdentificationReceived

Event Type

EventHandler<SshIdentificationEventArgs>

UserAuthenticationBannerReceived

Occurs when Renci.SshNet.Messages.Authentication.BannerMessage message is received from the server.

public event EventHandler<MessageEventArgs<BannerMessage>> UserAuthenticationBannerReceived

Event Type

EventHandler<MessageEventArgs<BannerMessage>>