Skip to content

Commit bb51335

Browse files
lupaulusRob-Hague
andauthored
doc: Update examples.md (#1421)
* doc: Update examples.md * tweaks --------- Co-authored-by: Rob Hague <[email protected]>
1 parent dda27a3 commit bb51335

File tree

1 file changed

+47
-8
lines changed

1 file changed

+47
-8
lines changed

docfx/examples.md

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,25 @@
11
Think this page is lacking? Help wanted! Click "Edit this page" at the bottom to begin contributing more examples.
22

3+
Getting Started
4+
=================
5+
6+
### Run a command
7+
8+
Establish an SSH connection and run a command:
9+
10+
```cs
11+
using (var client = new SshClient("sftp.foo.com", "guest", new PrivateKeyFile("path/to/my/key")))
12+
{
13+
client.Connect();
14+
using SshCommand cmd = client.RunCommand("echo 'Hello World!'");
15+
Console.WriteLine(cmd.Result); // "Hello World!\n"
16+
}
17+
```
18+
319
### Upload and list files
420

21+
SFTP Connection / Exchange
22+
523
```cs
624
using (var client = new SftpClient("sftp.foo.com", "guest", "pwd"))
725
{
@@ -44,23 +62,44 @@ string expectedFingerPrint = "LKOy5LvmtEe17S4lyxVXqvs7uPMy+yF79MQpHeCs/Qo";
4462
using (var client = new SshClient("sftp.foo.com", "guest", "pwd"))
4563
{
4664
client.HostKeyReceived += (sender, e) =>
47-
{
48-
e.CanTrust = expectedFingerPrint.Equals(e.FingerPrintSHA256);
49-
};
65+
{
66+
e.CanTrust = expectedFingerPrint.Equals(e.FingerPrintSHA256);
67+
};
5068
client.Connect();
5169
}
5270
```
5371

54-
### Run a command
72+
### Open a Shell
5573

56-
Establish an SSH connection and run a command:
74+
```cs
75+
using (var client = new SshClient("sftp.foo.com", "user", "password"))
76+
{
77+
client.Connect();
78+
using ShellStream shellStream = client.CreateShellStream("ShellName", 80, 24, 800, 600, 1024);
79+
client.Disconnect();
80+
}
81+
```
82+
83+
### Switch to root with "su - root"
5784

5885
```cs
59-
using (var client = new SshClient("sftp.foo.com", "guest", new PrivateKeyFile("path/to/my/key")))
86+
using (var client = new SshClient("sftp.foo.com", "user", "password"))
6087
{
6188
client.Connect();
62-
SshCommand cmd = client.RunCommand("echo 'Hello World!'");
63-
Console.WriteLine(cmd.Result); // "Hello World!\n"
89+
using ShellStream shellStream = client.CreateShellStream("ShellName", 80, 24, 800, 600, 1024);
90+
// Get logged in and get user prompt
91+
string prompt = shellStream.Expect(new Regex(@"[$>]"));
92+
// Send command and expect password or user prompt
93+
shellStream.WriteLine("su - root");
94+
prompt = shellStream.Expect(new Regex(@"([$#>:])"));
95+
// Check to send password
96+
if (prompt.Contains(":"))
97+
{
98+
// Send password
99+
shellStream.WriteLine("password");
100+
prompt = shellStream.Expect(new Regex(@"[$#>]"));
101+
}
102+
client.Disconnect();
64103
}
65104
```
66105

0 commit comments

Comments
 (0)