/* * Program: PowerTCP Telnet Tool - AutoLogin Demo * Original Author: Tony Priest * Date: 1/16/01 * * Converted to ASP .NET by Jeff Cranford 8-6-01 * Purpose: To demonstrate how to use the PowerTCP Telnet Tool to * automate the process of logging into a Telnet server in the .NET environment. */ using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; namespace AutoLogin { /// /// Summary description for WebForm1. /// public class WebForm1 : System.Web.UI.Page { protected System.Web.UI.WebControls.Label lblServer; protected System.Web.UI.WebControls.TextBox txtServer; protected System.Web.UI.WebControls.Label lblLoginPrompt; protected System.Web.UI.WebControls.TextBox txtLoginPrompt; protected System.Web.UI.WebControls.Label lblUsername; protected System.Web.UI.WebControls.TextBox txtUsername; protected System.Web.UI.WebControls.Label lblPassPrompt; protected System.Web.UI.WebControls.TextBox txtPassPrompt; protected System.Web.UI.WebControls.Label lblPass; protected System.Web.UI.WebControls.TextBox txtPass; protected System.Web.UI.WebControls.Label lblCommand; protected System.Web.UI.WebControls.TextBox txtCommand; protected System.Web.UI.WebControls.Button Button1; protected System.Web.UI.WebControls.Label lblResults; // telnet control protected DartTelnetDotnet.Telnet Telnet1 = new DartTelnetDotnet.Telnet(); public WebForm1() { Page.Init += new System.EventHandler(Page_Init); } private void Page_Load(object sender, System.EventArgs e) { // Put user code to initialize the page here } private void Page_Init(object sender, EventArgs e) { // // CODEGEN: This call is required by the ASP.NET Web Form Designer. // InitializeComponent(); } #region Web Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.Button1.Click += new System.EventHandler(this.Button1_Click); this.Load += new System.EventHandler(this.Page_Load); } #endregion private void Button1_Click(object sender, System.EventArgs e) { string data = ""; Telnet1.Timeout = 15000; lblResults.Text = ""; string tempstring = ""; // .NET's way of translating the Trace method, we must // pass a ref obj that has been set equal to a string containing // our filename object tempobj = new Object(); tempstring = "C:\\Telnettrace.txt"; tempobj = tempstring; Telnet1.Trace(ref tempobj, true, true, "\nSent: " , "\nReceived: "); bool connected = false; AddResult("Connecting to " + txtServer.Text + "...\r\n"); try { Telnet1.Connect(txtServer.Text, 23, "", 0); connected = true; } catch(Exception ConnectError) { AddResult("Error: " + ConnectError.Message); } if(connected) { // for the Telnet control's methods (Search, Send, etc) .NET requires we pass a ref obj object objdata = new Object(); object objtoken = new Object(); object objsend = new Object(); objdata = data; objtoken = txtLoginPrompt.Text; objsend = txtUsername.Text + "\r\n"; AddResult("Connect successful, attempting to login...\r\n"); try { // search for login prompt Telnet1.Search(ref objdata, ref objtoken); AddResult(objdata.ToString()); // login prompt found, send username Telnet1.Send(ref objsend, 0); objdata = ""; objtoken = ""; objtoken = txtPassPrompt.Text; // search for password prompt Telnet1.Search(ref objdata, ref objtoken); AddResult(objdata.ToString()); objsend = ""; objsend = txtPass.Text + "\r\n"; // password prompt found, send password Telnet1.Send(ref objsend, 0); objdata = ""; objtoken = ""; objtoken = txtCommand.Text; //retrieve all data up until command prompt and display Telnet1.Search(ref objdata, ref objtoken); AddResult(objdata.ToString()); AddResult("SUCCESS!"); } catch(Exception LoginError) { AddResult("Login Error: " + LoginError.Message); } finally { Telnet1.Close(); } } } private void AddResult(string s) { lblResults.Text = lblResults.Text + ReadyForHtml(s); } string ReadyForHtml(string s) { // convert all text formatting characters to html formatting characters s = s.Replace(">", ">"); s = s.Replace("<", "<"); s = s.Replace(" ", " "); s = s.Replace("\r\n ", "
 "); s = s.Replace("\r\n", "
"); return s; } } }