Previous Tip  |  Next Tip  |  Design Tips   | [Bill's Home]

232. Creating an SSH client

[Click here to download install file]

SSH replaces TELNET, as TELNET is considered as unsecure (as it sends the characters as plaintext). The following code is based on:

[SharpSSH]

using System;
using Tamir.SharpSsh;
using System.Threading;
using System.IO;
// based on http://www.tamirgal.com/home/dev.aspx?Item=sharpSsh#download

namespace sshTest
{
public class sshTest
{
public struct SshConnectionInfo
{
public string Host;
public string User;
public string Pass;
}
public static void Main()
{
GoShell();
}
public static void GoShell()
{
try
{
SshConnectionInfo input = GetInput();
SshShell shell = new SshShell(input.Host, input.User);
if(input.Pass != null) shell.Password = input.Pass;

shell.RedirectToConsole();

Console.Write("Connecting...");
shell.Connect();
Console.WriteLine("Details: \r\nMAC-"+shell.Mac+
"\r\nPort: "+shell.Port+
"\r\nCipher: "+shell.Cipher);

while(shell.ShellOpened)
{
System.Threading.Thread.Sleep(500);
}
Console.Write("Disconnecting...");
shell.Close();
Console.WriteLine("OK");
}
catch(Exception e)
{
Console.WriteLine(e.Message);
}
}
public static SshConnectionInfo GetInput()
{
SshConnectionInfo info = new SshConnectionInfo();
Console.Write("Enter Remote Host: ");
info.Host = Console.ReadLine();
Console.Write("Enter Username: ");
info.User = Console.ReadLine();

Console.Write("Enter Password: ");
info.Pass = Console.ReadLine();

Console.WriteLine();
return info;
}

}
}

A sample run gives:

Enter Remote Host: ftp1.dcs.napier.ac.uk
Enter Username: **
Enter Password: ******
Connecting...Details:
MAC-hmac-md5
Port: 22
Cipher: 3des-cbc
Last login: Tue Jan 23 21:15:43 2007 from user-514da235.l2.c1.dsl.pol.co.uk
Directory: /home/cs72
Tue Jan 23 21:19:30 GMT 2007
socweb1:~> ls