// --------------------------------- RunCommand ------------------------------ public virtual string[] RunCommand( CommandThread InCmdThread, ReadBuffer InReadBuffer, delWriteEventLog InWriteEventLog, string InCmdText) { StringBuilder accumRead = new StringBuilder(); string[] lines = null; // send command to the telnet server. if (InCmdThread.ShutdownFlag.State == false) { if (InWriteEventLog != null) { InWriteEventLog(null, "exit", "Logout"); } InCmdThread.Conn.WriteLine(InCmdText); } // read back whatever is in the read buffer. if (InCmdThread.ShutdownFlag.State == false) { lines = InReadBuffer.ReadUntilEndsWith(CommandPromptPatterns); ThreadCommon.WriteToEventLog(InWriteEventLog, lines, "Run"); } // return all the response line received from the server. return(lines); }
// --------------------------------- Logout ------------------------------ public virtual string[] Logout( CommandThread InCmdThread, ReadBuffer InReadBuffer, delWriteEventLog InWriteEventLog, string InNullText) { StringBuilder accumRead = new StringBuilder(); string[] lines = null; if (InCmdThread.Conn.IsConnected == false) { InCmdThread.ShutdownFlag.Set(); } // send "exit" command to the telnet server. if (InCmdThread.ShutdownFlag.State == false) { if (InWriteEventLog != null) { InWriteEventLog(null, "exit", "Logout"); } try { InCmdThread.Conn.WriteLine("exit"); } catch (NotConnectedException) { InCmdThread.ShutdownFlag.Set(); } } // read back whatever is in the read buffer. if (InCmdThread.ShutdownFlag.State == false) { lines = InReadBuffer.Read(); ThreadCommon.WriteToEventLog(InWriteEventLog, lines, "Logout"); } // wait for the "socket is disconnected" signal. int waitDur = 5000; while (true) { bool rc = InCmdThread.Conn.IsDisconnectedSignal.WaitOne(waitDur); if (rc == true) { break; } if (waitDur < 3600000) { waitDur *= 2; } InWriteEventLog(null, "waiting for telnet server to disconnect after exit", "Logout"); } // return all the response line received from the server. return(lines); }
// --------------------------------- Login ------------------------------ public virtual string[] Login( CommandThread InCmdThread, ReadBuffer InReadBuffer, delWriteEventLog InWriteEventLog, string InNullText) { StringBuilder accumRead = new StringBuilder(); string[] lines = null; string[] loginPrompts = new string[] { ":" }; // telnet server sends a login prompt. read until it arrives. if (InCmdThread.ShutdownFlag.State == false) { if (InWriteEventLog != null) { InWriteEventLog(null, "Read login prompt ...", "Login"); } lines = InReadBuffer.ReadUntilEndsWith(loginPrompts); ThreadCommon.WriteToEventLog(InWriteEventLog, lines, "Login"); } // send username to the telnet server. if (InCmdThread.ShutdownFlag.State == false) { if (InWriteEventLog != null) { InWriteEventLog(null, "Username " + mLoginProperties.LoginUser, "Login"); } InCmdThread.Conn.WriteLine(mLoginProperties.LoginUser); } // read back the prompt for password. if (InCmdThread.ShutdownFlag.State == false) { if (InWriteEventLog != null) { InWriteEventLog(null, "Read password prompt ...", "Login"); } lines = InReadBuffer.ReadUntilEndsWith(loginPrompts); ThreadCommon.WriteToEventLog(InWriteEventLog, lines, "Login"); } // send password to telnet server. if (InCmdThread.ShutdownFlag.State == false) { if (InWriteEventLog != null) { InWriteEventLog(null, "Password ********", "Login"); } InCmdThread.Conn.WriteLine(mLoginProperties.LoginPass); } // read back until the logged in command prompt. if (InCmdThread.ShutdownFlag.State == false) { if (InWriteEventLog != null) { InWriteEventLog(null, "Read command prompt ...", "Login"); } while (true) { string[] xx = null; xx = InReadBuffer.Read(); ThreadCommon.WriteToEventLog(InWriteEventLog, xx, "Login"); string lastLine = Arrayer.LastItem(xx); if (lastLine != null) { string trimLastLine = lastLine.TrimEnd(new char[] { ' ', '\t', '\r', '\n' }); string lsChar = StringExt.Tail(trimLastLine, 1); if (lsChar == ">") { break; } } } } // return all the response line received from the server. return(lines); }