Registering event handlers |
The Ssh class may publish one or more events during the lifetime of an SSH session. Any object that subscribes to events published by the Ssh class can receive and process these events.
Capturing the SshConnectedEvent event
The SshConnectedEvent is fired by the Ssh instance once a connection to the SSH server has been established.
Example
[C#]
// create login credentials
SshParameters sshParams = new SshParameters(hostname,username,password);
// create new Ssh instance
Ssh ssh = new Ssh(sshParams);
// subscribe to event
ssh.SshConnectedEvent += new Ssh.SshConnectedEventHandler(OnConnect);
// event handler code
public void OnConnect(object sender, SshConnectedEventArgs e) {
Console.WriteLine("Connected to {0}", e.Hostname);
}
[VB]
' create login credentials
Dim sshParams As SshParameters = New SshParameters(hostname, username, password)
' create new Ssh instance
Dim ssh As Ssh = New Ssh(sshParams)
' subscribe to event
ssh.SshConnectedEvent += New Ssh.SshConnectedEventHandler(OnConnect)
' event handler code
Public Sub OnConnect(ByVal sender As Object, ByVal e As SshConnectedEventArgs)
Console.WriteLine("Connected to {0}", e.Hostname)
End Sub
Capturing the SshDataEvent event
The SshDataEvent is fired by the Ssh instance when data is received by the SSH server.
Example
[C#]
// create login credentials
SshParameters sshParams = new SshParameters(hostname,username,password);
// create new Ssh instance
Ssh ssh = new Ssh(sshParams);
// subscribe to event
ssh.SshDataEvent += new Ssh.SshDataEventHandler(OnData);
// event handler code
public void OnData(object sender, SshDataEventArgs e) {
Console.Write(e.Data);
}
[VB]
' create login credentials
Dim sshParams As SshParameters = New SshParameters(hostname, username, password)
' create new Ssh instance
Dim ssh As Ssh = New Ssh(sshParams)
' subscribe to event
ssh.SshDataEvent += New Ssh.SshDataEventHandler(OnData)
' event handler code
Public Sub OnData(ByVal sender As Object, ByVal e As SshDataEventArgs)
Console.Write(e.Data)
End Sub
Capturing the SshDisconnectedEvent event
The SshDisconnectedEvent is fired by the Ssh instance once the connection to the SSH server has been released.
Example
[C#]
// create login credentials
SshParameters sshParams = new SshParameters(hostname,username,password);
// create new Ssh instance
Ssh ssh = new Ssh(sshParams);
// subscribe to event
ssh.SshDisconnectedEvent += new Ssh.SshDisconnectedEventHandler(OnDisconnect);
// event handler code
public void OnDisconnect(object sender, SshDisconnectedEventArgs e) {
Console.WriteLine("Disconnected from {0}", e.Hostname);
}
[VB]
' create login credentials
Dim sshParams As SshParameters = New SshParameters(hostname, username, password)
' create new Ssh instance
Dim ssh As Ssh = New Ssh(sshParams)
' subscribe to event
ssh.SshDisconnectedEvent += New Ssh.SshDisconnectedEventHandler(OnDisconnect)
' event handler code
Public Sub OnDisconnect(ByVal sender As Object, ByVal e As SshDisconnectedEventArgs)
Console.WriteLine("Disconnected from {0}", e.Hostname)
End Sub