Overview
This article will demonstrate how to commnicate with SSH servers using SSH Factory for .NET.
Code Example
Download license.txt | Download SSHEx.cs
| Download SSHEx.vb
1: using System;
2: using System.Collections.Generic;
3: using System.Collections;
4: using System.Text;
5: using Jscape.Sftp;
6: using Jscape.Ssh;
7: using System.Net;
8: using Jscape.Ssh.Util;
9: using Jscape.Sftp.Packets;
10: using System.IO;
11:
12: namespace SSH
13: {
14: class SSHEx
15: {
16: public delegate void SSHExEmitDataEventHandler(String data);
17: public event SSHExEmitDataEventHandler SSHExEmitData;
18:
19: public void OnSSHExEmitData(String data)
20: {
21: if (SSHExEmitData != null)
22: SSHExEmitData(data);
23: }
24:
25: public SSHEx(string server, string username, string password,
26: bool validateHostKeys, string hostkey,
27: bool usePublicKeyAuthentication, string privateKeyFile,
28: string privateKeyPassword)
29: {
30: SshParameters sp = new SshParameters(server, username, password);
31: if (validateHostKeys == true)
32: {
33: SshHostKeys keys = new SshHostKeys();
34: keys.AddKey(Dns.GetHostEntry(server), hostkey);
35: sp.SetHostKeys(keys, false);
36: }
37: else if (usePublicKeyAuthentication == true)
38: {
39: sp.SetPrivateKey(new System.IO.FileInfo(privateKeyFile),
40: privateKeyPassword);
41: }
42:
43: this.Ssh = new Jscape.Ssh.Ssh(sp);
44: this.Ssh.LicenseKey = null; // copy license key from license.txt here
45: this.Ssh.SshConnectedEvent +=
46: new Ssh.SshConnectedEventHandler(Ssh_SshConnectedEvent);
47: this.Ssh.SshDisconnectedEvent +=
48: new Ssh.SshDisconnectedEventHandler(Ssh_SshDisconnectedEvent);
49: this.Ssh.SshDataEvent +=
50: new Ssh.SshDataEventHandler(Ssh_SshDataEvent);
51: }
52:
53: private void Ssh_SshDataEvent(object source, SshDataEventArgs args)
54: {
55: OnSSHExEmitData(args.Data);
56: }
57:
58: private void Ssh_SshDisconnectedEvent(object source, SshDisconnectedEventArgs args)
59: {
60: Console.WriteLine("Disconnected to : " + args.Hostname);
61: }
62:
63: private void Ssh_SshConnectedEvent(object source, SshConnectedEventArgs args)
64: {
65: Console.WriteLine("Connected to : " + args.Hostname);
66: }
67:
68: public void openConnection()
69: {
70: this.Ssh.Connect();
71: }
72:
73: public void closeConnection()
74: {
75: this.Ssh.Disconnect();
76: }
77:
78: public ArrayList getHostKeys()
79: {
80: ArrayList ret = new ArrayList();
81: SshHostKeys keys = keys = this.Ssh.HostKeys;
82: IEnumerator iHosts = keys.GetHosts();
83: while (iHosts.MoveNext())
84: {
85: DictionaryEntry entry = (DictionaryEntry)iHosts.Current;
86: ret.Add(entry);
87: }
88: return ret;
89: }
90:
91: public void sendData(string line)
92: {
93: Stream output = this.Ssh.GetStream();
94: byte[] data = System.Text.Encoding.UTF8.GetBytes(line);
95: byte[] nl = System.Text.Encoding.UTF8.GetBytes("\r\n");
96: output.Write(data, 0, data.Length);
97: output.Write(nl, 0, nl.Length);
98: output.Flush();
99: }
100:
101: private Jscape.Ssh.Ssh _Ssh;
102: public Jscape.Ssh.Ssh Ssh
103: {
104: get
105: {
106: return _Ssh;
107: }
108: set
109: {
110: _Ssh = value;
111: }
112: }
113:
114: static void Main(string[] args)
115: {
116: try
117: {
118: string server = "server";
119: string username = "username";
120: string password = "password";
121: SSHEx e = new SSHEx(server, username, password, false,
122: "hostkey", false, "privateKeyFile",
123: "privateKeyPassword");
124: e.openConnection();
125: e.sendData("data");
126: e.closeConnection();
127: }
128: catch (Exception e)
129: {
130: Console.WriteLine(e.Message);
131: }
132:
133: Console.WriteLine("Press any key to continue ... ");
134: Console.Read();
135: }
136: }
137: }
Lines 1-10 : Necessary import statements.
Lines 16-23 : Event/Delegate for emitted data received from the server.
Lines 25-51 : Initialize Ssh instance. Connect event handlers.
Lines 53-56 : Event handler for receiving data from the server.
Lines 58-61 : Event handler when disconnected from the server.
Lines 63-66 : Event handler when connected to the server.
Lines 68-76 : Methods to open and close the connection.
Lines 78-89 : Get host keys. Each entry is of type KeyValuePair.
Lines 91-99 : Send data to the server.
Line 121 : Initialize SShEx instance.