Very simple C# SSH Shell Console Terminal
Here’s an example that demonstrates a rough start to creating a C# console SSH shell terminal (where the user can type commands and output from the remote command echos to the console:
using System; using System.Collections.Generic; using System.Text; using System.IO; namespace SshTerminalConsole { class Program { static void Main(string[] args) { Chilkat.Ssh ssh = new Chilkat.Ssh(); ssh.UnlockComponent("Test"); // Hostname may be an IP address or hostname: string hostname = "192.168.1.117"; int port = 22; Console.WriteLine("Connecting..."); //ssh.KeepSessionLog = true; bool success = ssh.Connect(hostname, port); if (success != true) { Console.WriteLine(ssh.LastErrorText + "\r\n"); // Read so we can see the error before the console closes. string x = Console.ReadLine(); return; } // When reading, if no additional data arrives for more than // 5 seconds, then abort: ssh.IdleTimeoutMs = 5000; Console.WriteLine("Authenticating..."); // SSH Server Authentication // If there is no login/password required, you must still call // AuthenticatePw and use any values for login/password. success = ssh.AuthenticatePw("chilkat", "***"); if (success != true) { Console.WriteLine(ssh.LastErrorText + "\r\n"); // Read so we can see the error before the console closes. string x = Console.ReadLine(); return; } Console.WriteLine("Opening Channel..."); // Open a session channel. int channelNum = ssh.OpenSessionChannel(); if (channelNum < 0) { Console.WriteLine(ssh.LastErrorText + "\r\n"); // Read so we can see the error before the console closes. string x = Console.ReadLine(); return; } // Request a pseudo-terminal string termType; termType = "dumb"; int widthInChars; widthInChars = 120; int heightInChars; heightInChars = 40; int pixWidth; pixWidth = 0; int pixHeight; pixHeight = 0; success = ssh.SendReqPty(channelNum, termType, widthInChars, heightInChars, pixWidth, pixHeight); if (success != true) { Console.WriteLine(ssh.LastErrorText + "\r\n"); // Read so we can see the error before the console closes. string x = Console.ReadLine(); return; } Console.WriteLine("Starting a shell..."); // Start a shell on the channel: success = ssh.SendReqShell(channelNum); if (success != true) { Console.WriteLine(ssh.LastErrorText + "\r\n"); // Read so we can see the error before the console closes. string x = Console.ReadLine(); return; } // Loop to read from the SSH channel, output to the console, and read keyboard input from the console. StringBuilder sb = new StringBuilder(); while (true) { if (Console.KeyAvailable) { ConsoleKeyInfo key = Console.ReadKey(true); Console.Write(key.KeyChar); switch (key.Key) { case ConsoleKey.Enter: Console.WriteLine(""); sb.Append("\n"); success = ssh.ChannelSendString(channelNum, sb.ToString(), "ansi"); if (success != true) { Console.WriteLine(ssh.LastErrorText + "\r\n"); // Read so we can see the error before the console closes. string x = Console.ReadLine(); return; } sb.Length = 0; break; default: //Console.Write(key.KeyChar); sb.Append(key.KeyChar); break; } } // Now check for incoming data from the SSH channel. int retval = ssh.ChannelPoll(channelNum, 10); if (retval == -1) { Console.Write(ssh.LastErrorText); Console.WriteLine(""); // Read so we can see the error before the console closes. string x = Console.ReadLine(); return; } if (retval > 0) { Console.Write(ssh.GetReceivedText(channelNum, "ansi")); } else { // If data arrived, loop around and get more immediately. // Otherwise wait 20ms. System.Threading.Thread.Sleep(20); } } } } }
admin
0
Tags :