Creating an interactive SSH session |
To create an interactive SSH session you must first understand how to establish a connection, send data to the SSH server and receive data from the SSH server. Each of these is covered in the sections titled Establishing a connection , Receiving data and Sending data respectively.
Example
[C#]
using System;
using System.IO;
using Jscape.Ssh;
namespace SshExample
{
/// <summary>
/// Demonstrates creating an interactive SSH client.
/// </summary>
///
class SshExample
{
private const string NEWLINE = "\n";
private bool connected = false;
public SshExample() {
// get hostname and login information
Console.Write("Enter hostname: ");
string hostname = Console.ReadLine();
Console.Write("Enter username: ");
string username = Console.ReadLine();
Console.Write("Enter password: ");
string password = Console.ReadLine();
// create login credentials
SshParameters sshParams = new SshParameters(hostname,username,password);
// create new Ssh instance
Ssh ssh = new Ssh(sshParams);
// register event handlers
ssh.SshDataEvent += new Ssh.SshDataEventHandler(OnData);
ssh.SshConnectedEvent += new Ssh.SshConnectedEventHandler(OnConnected);
ssh.SshDisconnectedEvent += new Ssh.SshDisconnectedEventHandler(OnDisconnected);
// establish connection
ssh.Connect();
// get output stream
Stream output = ssh.GetStream();
// read data from console and write 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(NEWLINE);
output.Write(data,0,data.Length);
output.Write(nl,0,nl.Length);
try {
output.Flush();
} catch(Exception e) {
break;
}
}
// disconnect
ssh.Disconnect();
}
/// <summary>
/// Invoked when data is recevied from SSH server
/// </summary>
/// <param name="sender">the source of this event</param>
/// <param name="e">the event</param>
public void OnData(object sender, SshDataEventArgs e) {
Console.Write(e.Data);
}
/// <summary>
/// Invoked when a connection is established with SSH server
/// </summary>
/// <param name="sender">the source of this event</param>
/// <param name="e">the event</param>
public void OnConnected(object sender, SshConnectedEventArgs e) {
connected = true;
Console.WriteLine("Connected to host " + e.Hostname);
}
/// <summary>
/// Invoked when connection is released from SSH server.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void OnDisconnected(object sender, SshDisconnectedEventArgs e) {
connected = false;
Console.WriteLine("Disconnected from host " + e.Hostname);
Console.WriteLine("");
Console.WriteLine("Press ENTER to exit.");
}
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
SshExample example = new SshExample();
}
}
}
[VB]
Imports System
Imports System.IO
Imports Jscape.Ssh
Namespace SshExample
Class SshExample
Private Const NEWLINE As String = "" & Microsoft.VisualBasic.Chr(10) & ""
Private connected As Boolean = False
Public Sub New()
' get hostname and login information
Console.Write("Enter hostname: ")
Dim hostname As String = Console.ReadLine
Console.Write("Enter username: ")
Dim username As String = Console.ReadLine
Console.Write("Enter password: ")
Dim password As String = Console.ReadLine
' create login credentials
Dim sshParams As SshParameters = New SshParameters(hostname, username, password)
Dim ssh As Ssh = New Ssh(sshParams)
' register event handlers
AddHandler ssh.SshDataEvent, AddressOf OnData
AddHandler ssh.SshConnectedEvent, AddressOf OnConnected
AddHandler ssh.SshDisconnectedEvent, AddressOf OnDisconnected
' 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(NEWLINE)
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
' disconnect
ssh.Disconnect()
End Sub
Public Sub OnData(ByVal sender As Object, ByVal e As SshDataEventArgs)
Console.Write(e.Data)
End Sub
Public Sub OnConnected(ByVal sender As Object, ByVal e As SshConnectedEventArgs)
connected = True
Console.WriteLine("Connected to host " + e.Hostname)
End Sub
Public Sub OnDisconnected(ByVal sender As Object, ByVal e As SshDisconnectedEventArgs)
connected = False
Console.WriteLine("Disconnected from host " + e.Hostname)
Console.WriteLine("")
Console.WriteLine("Press ENTER to exit.")
End Sub
<STAThread()> _
Shared Sub Main(ByVal args As String())
Dim example As SshExample = New SshExample
End Sub
End Class
End Namespace