Using the TelnetSession class

Top  Previous  Next

This section describes how you can run a telnet session in a non-interactive (no user interaction required) mode. The ability to run a telnet session in non-interactive mode is a very powerful feature allowing you to automate tasks that previously required user-interaction.

 

Use Case

A very basic example of a scripted telnet session would be to automate the following procedure:

 

1.Login to telnet server
2.Execute a command
3.Logout

 

Example

 

[C#]

using System;

using Jscape.Telnet;

 

namespace SessionExample {

 

 public class TelnetExample {

 

         public TelnetExample(string hostname) {

 

                 // Create new telnet session

                 TelnetSession session = new TelnetSession(hostname);

 

                 // Connect to telnet server using username and password

                 session.Connect("username", "password");

 

                 // Send command to telnet server

                 session.Send("ls -al");

 

                 // Terminate telnet session without waiting for shell prompt.

                 session.SendNoWait("exit");

 

                 // Disconnect

                 session.Disconnect();

         }

 

         public static void Main() {

                 // Create new TelnetExample instance passing telnet server hostname or IP address

                 TelnetExample example = new TelnetExample("telnet.server.com");

         }

 

 }

}

 

 

[Visual Basic]

Imports System

Imports Jscape.Telnet

 

Public Class TelnetExample

 

  Private mySession As TelnetSession

  Private hostname As String = ""

 

  Public Sub New(ByVal hostname As String)

 

      ' set hostname

       Me.hostname = hostname

 

      ' Create new telnet session

       mySession = New TelnetSession(hostname)

 

      ' Connect to telnet server using username and password

       mySession.Connect("username", "password")

 

      ' Send command to telnet server

       mySession.Send("ls -al")

 

      ' Terminate telnet session without waiting for shell prompt.

       mySession.SendNoWait("exit")

 

      ' Disconnect

       mySession.Disconnect()

 

  End Sub

 

  Public Shared Sub main()

      ' Create new TelnetExample instance passing telnet server hostname or IP address

      Dim example As TelnetExample

       example = New TelnetExample("telnet.server.com")

 

  End Sub

 

End Class





Home | Company | Products | Solutions | Purchase | Support | Services | Blog

© 2021 JSCAPE LLC