From 1cca6a9b48f1ef3e1c70b4dc52b8f2fadc1e0129 Mon Sep 17 00:00:00 2001 From: Paulus Lucas Date: Sat, 1 Jun 2024 13:50:29 +0200 Subject: [PATCH 1/2] doc: Update examples.md --- docfx/examples.md | 50 ++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 45 insertions(+), 5 deletions(-) diff --git a/docfx/examples.md b/docfx/examples.md index 76b15369e..73bca8b3e 100644 --- a/docfx/examples.md +++ b/docfx/examples.md @@ -1,7 +1,25 @@ Think this page is lacking? Help wanted! Click "Edit this page" at the bottom to begin contributing more examples. +Getting Started +================= + +### Run a command + +Establish an SSH connection and run a command: + +```cs +using (var client = new SshClient("sftp.foo.com", "guest", new PrivateKeyFile("path/to/my/key"))) +{ + client.Connect(); + SshCommand cmd = client.RunCommand("echo 'Hello World!'"); + Console.WriteLine(cmd.Result); // "Hello World!\n" +} +``` + ### Upload and list files +SFTP Connection / Exchange + ```cs using (var client = new SftpClient("sftp.foo.com", "guest", "pwd")) { @@ -51,19 +69,41 @@ using (var client = new SshClient("sftp.foo.com", "guest", "pwd")) } ``` -### Run a command +### Open a Shell -Establish an SSH connection and run a command: +```cs +using (var client = new SshClient("sftp.foo.com", "user", "password")) +{ + client.Connect(); + ShellStream shellStream = client.CreateShellStream("ShellName", 80, 24, 800, 600, 1024); + client.Disconnect(); +} +``` + +### Switch to root with "su - root" ```cs -using (var client = new SshClient("sftp.foo.com", "guest", new PrivateKeyFile("path/to/my/key"))) +using (var client = new SshClient("sftp.foo.com", "user", "password")) { client.Connect(); - SshCommand cmd = client.RunCommand("echo 'Hello World!'"); - Console.WriteLine(cmd.Result); // "Hello World!\n" + ShellStream shellStream = client.CreateShellStream("ShellName", 80, 24, 800, 600, 1024); + // Get logged in and get user prompt + string prompt = stream.Expect(new Regex(@"[$>]")); + // Send command and expect password or user prompt + stream.WriteLine("su - root"); + prompt = stream.Expect(new Regex(@"([$#>:])")); + // Check to send password + if (prompt.Contains(":")) + { + // Send password + stream.WriteLine("password"); + prompt = stream.Expect(new Regex(@"[$#>]")); + } + client.Disconnect(); } ``` + ### Stream data to a command ```cs From 869b5631f45a45405ee876c3bdc7f189ec79c0a2 Mon Sep 17 00:00:00 2001 From: Rob Hague Date: Tue, 4 Jun 2024 23:18:35 +0200 Subject: [PATCH 2/2] tweaks --- docfx/examples.md | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/docfx/examples.md b/docfx/examples.md index 73bca8b3e..7014b8f21 100644 --- a/docfx/examples.md +++ b/docfx/examples.md @@ -11,7 +11,7 @@ Establish an SSH connection and run a command: using (var client = new SshClient("sftp.foo.com", "guest", new PrivateKeyFile("path/to/my/key"))) { client.Connect(); - SshCommand cmd = client.RunCommand("echo 'Hello World!'"); + using SshCommand cmd = client.RunCommand("echo 'Hello World!'"); Console.WriteLine(cmd.Result); // "Hello World!\n" } ``` @@ -62,9 +62,9 @@ string expectedFingerPrint = "LKOy5LvmtEe17S4lyxVXqvs7uPMy+yF79MQpHeCs/Qo"; using (var client = new SshClient("sftp.foo.com", "guest", "pwd")) { client.HostKeyReceived += (sender, e) => - { - e.CanTrust = expectedFingerPrint.Equals(e.FingerPrintSHA256); - }; + { + e.CanTrust = expectedFingerPrint.Equals(e.FingerPrintSHA256); + }; client.Connect(); } ``` @@ -75,7 +75,7 @@ using (var client = new SshClient("sftp.foo.com", "guest", "pwd")) using (var client = new SshClient("sftp.foo.com", "user", "password")) { client.Connect(); - ShellStream shellStream = client.CreateShellStream("ShellName", 80, 24, 800, 600, 1024); + using ShellStream shellStream = client.CreateShellStream("ShellName", 80, 24, 800, 600, 1024); client.Disconnect(); } ``` @@ -86,24 +86,23 @@ using (var client = new SshClient("sftp.foo.com", "user", "password")) using (var client = new SshClient("sftp.foo.com", "user", "password")) { client.Connect(); - ShellStream shellStream = client.CreateShellStream("ShellName", 80, 24, 800, 600, 1024); + using ShellStream shellStream = client.CreateShellStream("ShellName", 80, 24, 800, 600, 1024); // Get logged in and get user prompt - string prompt = stream.Expect(new Regex(@"[$>]")); + string prompt = shellStream.Expect(new Regex(@"[$>]")); // Send command and expect password or user prompt - stream.WriteLine("su - root"); - prompt = stream.Expect(new Regex(@"([$#>:])")); + shellStream.WriteLine("su - root"); + prompt = shellStream.Expect(new Regex(@"([$#>:])")); // Check to send password if (prompt.Contains(":")) { // Send password - stream.WriteLine("password"); - prompt = stream.Expect(new Regex(@"[$#>]")); + shellStream.WriteLine("password"); + prompt = shellStream.Expect(new Regex(@"[$#>]")); } client.Disconnect(); } ``` - ### Stream data to a command ```cs