Overview
This article will demonstrate how to communicate via telnet session in .NET using Telnet Factory for .NET.
Code Example
Download TelnetLicense.txt
| Download TS.cs
| Download TS.vb
1: using System;
2: using System.Collections.Generic;
3: using System.Text;
4: using Jscape.Telnet;
5: using Jscape.Telnet.Options;
6:
7: namespace TelnetSession_NET
8: {
9: class TS
10: {
11: TelnetSession ts = null;
12: public TS(string host)
13: {
14: ts = new TelnetSession(host);
15: }
16:
17: public void openConnection(string username, string password)
18: {
19: ts.Connect(username, password);
20: }
21:
22: public void closeConnection()
23: {
24: ts.Disconnect();
25: }
26:
27: public string sendCommand(string send)
28: {
29: return ts.Send(send);
30: }
31:
32: public void setShellPrompt(string prompt, bool expr)
33: {
34: ts.ShellPrompt = prompt;
35: ts.ShellPromptRegex = true;
36: }
37:
38: public void setLoginPrompt(string prompt, bool expr)
39: {
40: ts.LoginPrompt = prompt;
41: ts.LoginPromptRegex = true;
42: }
43:
44: public void setPasswordPrompt(string prompt, bool expr)
45: {
46: ts.PasswordPrompt = prompt;
47: ts.PasswordPromptRegex = expr;
48: }
49:
50: static void Main(string[] args)
51: {
52: try
53: {
54: TS t = new TS("telnetserver");
55: t.openConnection("username", "password");
56: t.closeConnection();
57: }
58: catch (Exception e)
59: {
60: Console.WriteLine(e.Message);
61: }
62:
63: Console.WriteLine("Press any key to exit ... ");
64: Console.Read();
65: }
66: }
67: }
Lines 1-5 : Necessary import statements.
Lines 11-15 : Initialize TelnetSession instance.
Lines 17-19 : Open connection to server.
Lines 22-25 : Close connection to server.
Lines 27-30 : Send command to server.
Lines 32-36 : Set shell prompt.
Lines 38-42 : Set login prompt.
Lines 44-48 : Set password prompt.
Line 54 : Initialize TS connection.
Comments