Telnet Class
Namespace: Rebex.Net
Assembly: Rebex.Telnet.dll (version 8.0.9673)
Provides methods for communication with Telnet servers.
Syntax
public class Telnet : IShellChannelFactory, ILogWriterProvider
Inherited Members
Examples
See https://www.rebex.net/ssh-shell/features/connecting.aspx#connect-telnet for more code snippets.
See https://www.rebex.net/ssh-shell/samples.aspx for ready-to-use samples.
Connecting and authenticating
// initialize new Telnet client instance
// if the server does not support TLS, use SslMode.None
var client = new Rebex.Net.Telnet(hostname, SslMode.Explicit);
// attach logger for diagnostic purposes
client.LogWriter = new Rebex.FileLogWriter(@"C:\MyData\telnet.log");
// initialize scripting object to send multiple commands
var scripting = client.StartScripting();
// wait for "login" prompt, then send the username
scripting.WaitFor(ScriptEvent.FromRegex("([Ll]ogin)|([Uu]sername): "));
scripting.SendCommand(username);
// wait for "password" prompt, then send the password
scripting.WaitFor(ScriptEvent.FromRegex("[Pp]assword: "));
scripting.SendCommand(password);
Simple telnet session
// detect prompt for later use
scripting.DetectPrompt();
// execute a command
scripting.SendCommand("whoami");
// read the response
var response = scripting.ReadUntilPrompt();
Console.WriteLine(response);
// execute another command
scripting.SendCommand("ls");
// print the output line-by-line
while (true)
{
var m = scripting.WaitFor(ScriptEvent.Line | ScriptEvent.Prompt);
if (m.IsLine)
{
Console.Write(scripting.ReceivedData);
}
else
{
break;
}
}
// close the connection
scripting.Close();
Constructors
| Name | Description |
|---|---|
| Telnet(String) | Initializes a new instance of the Telnet class, using default port 23 and no SSL. |
| Telnet(String, SslMode) | Initializes a new instance of the Telnet class, using default port 23 and specified SSL mode. |
| Telnet(String, Int32) | Initializes a new instance of the Telnet class, using specified port and no SSL. |
| Telnet(String, Int32, SslMode) | Initializes a new instance of the Telnet class, using specified port and SSL mode. |
Fields
| Name | Description |
|---|---|
| DefaultPort | Default Telnet port (23). |
Properties
| Name | Description |
|---|---|
| HostName | Gets the host-name of the remote server. |
| LogWriter | Gets or sets the logger used by this object. |
| Port | Gets the port of the remote server. |
| Proxy | Gets or sets the network proxy to use to access a remote server. |
| Settings | Gets or sets Telnet object settings. |
| Timeout | Gets or sets the length of time in milliseconds before the operation times out (specify -1 or 0 to indicate that the request does not time out). |
Methods
| Name | Description |
|---|---|
| SetSocketFactory(ISocketFactory) | Sets the socket factory to be used to create communication sockets. |
| StartScripting() | Starts a scripting session with a virtual terminal. |
| StartScripting(TerminalOptions) | Starts a scripting session with a virtual terminal. |
| StartScripting(TerminalOptions, Int32, Int32) | Starts a scripting session with a virtual terminal. |
| StartScriptingAsync(TerminalOptions, Int32, Int32, Object) | Begins asynchronous StartScripting operation. Starts a scripting session with a virtual terminal. |
| StartScriptingAsync(TerminalOptions, Object) | Begins asynchronous StartScripting operation. Starts a scripting session with a virtual terminal. |
| StartScriptingAsync(Object) | Begins asynchronous StartScripting operation. Starts a scripting session with a virtual terminal. |
| StartShell() | Starts a remote shell. |
| StartVirtualTerminal() | Starts a virtual terminal session. |
| StartVirtualTerminal(TerminalOptions) | Starts a virtual terminal session. |
| StartVirtualTerminal(TerminalOptions, Int32, Int32) | Starts a virtual terminal session. |
| StartVirtualTerminalAsync(TerminalOptions, Int32, Int32, Object) | Begins asynchronous StartVirtualTerminal operation. Starts a virtual terminal session. |
| StartVirtualTerminalAsync(TerminalOptions, Object) | Begins asynchronous StartVirtualTerminal operation. Starts a virtual terminal session. |
| StartVirtualTerminalAsync(Object) | Begins asynchronous StartVirtualTerminal operation. Starts a virtual terminal session. |
Events
| Name | Description |
|---|---|
| ValidatingCertificate | Occurs when a server certificate needs to be validated. |
Explicit Interface Implementations
| Name | Description |
|---|---|
| IShellChannelFactory.CreateShellChannel(TerminalOptions, Int32, Int32) | Creates a new shell channel using the specified options. |