Skip to content

Commit

Permalink
doc: Update examples.md (#1421)
Browse files Browse the repository at this point in the history
* doc: Update examples.md

* tweaks

---------

Co-authored-by: Rob Hague <[email protected]>
  • Loading branch information
lupaulus and Rob-Hague authored Jun 4, 2024
1 parent dda27a3 commit bb51335
Showing 1 changed file with 47 additions and 8 deletions.
55 changes: 47 additions & 8 deletions docfx/examples.md
Original file line number Diff line number Diff line change
@@ -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();
using 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"))
{
Expand Down Expand Up @@ -44,23 +62,44 @@ 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();
}
```

### 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();
using 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"
using ShellStream shellStream = client.CreateShellStream("ShellName", 80, 24, 800, 600, 1024);
// Get logged in and get user prompt
string prompt = shellStream.Expect(new Regex(@"[$>]"));
// Send command and expect password or user prompt
shellStream.WriteLine("su - root");
prompt = shellStream.Expect(new Regex(@"([$#>:])"));
// Check to send password
if (prompt.Contains(":"))
{
// Send password
shellStream.WriteLine("password");
prompt = shellStream.Expect(new Regex(@"[$#>]"));
}
client.Disconnect();
}
```

Expand Down

0 comments on commit bb51335

Please sign in to comment.