Professional Applications Programmers/Consultants برمجة واستشارات تطبيقات الإنترنت
Skip Navigation LinksHome » Code Library » Telnet

Public general use code classes and xml files that we've compiled and used over the years:

Telnet communication support class.

   1:  using System;
   2:  using System.Web;
   3:  using System.Xml;
   4:  using System.Xml.XPath;
   5:  using System.Xml.Xsl;
   6:  using System.Configuration;
   7:  using System.IO;
   8:  using System.Text;
   9:  using System.Text.RegularExpressions;
  10:  using Dart.PowerTCP.Telnet;
  11:  using System.Collections;
  12:  using System.Net.Sockets;
  13:   
  14:  namespace Ia.Cl.Model
  15:  {
  16:      /// <summary publish="true">
  17:      /// Telnet communication support class.
  18:      /// </summary>
  19:      /// <remarks> 
  20:      /// Copyright © 2001-2015 Jasem Y. Al-Shamlan (info@ia.com.kw), Internet Applications - Kuwait. All Rights Reserved.
  21:      ///
  22:      /// This library is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
  23:      /// the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
  24:      ///
  25:      /// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  26:      /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  27:      /// 
  28:      /// You should have received a copy of the GNU General Public License along with this library. If not, see http://www.gnu.org/licenses.
  29:      /// 
  30:      /// Copyright notice: This notice may not be removed or altered from any source distribution.
  31:      /// </remarks> 
  32:      public class Telnet
  33:      {
  34:          private string hostname, username, password, unixPrompt;
  35:          private Dart.PowerTCP.Telnet.Telnet telnet = new Dart.PowerTCP.Telnet.Telnet();
  36:   
  37:          ////////////////////////////////////////////////////////////////////////////
  38:   
  39:          /// <summary>
  40:          ///
  41:          /// </summary>
  42:          public Telnet()
  43:          {
  44:              // enable KeepAlive socket option
  45:              //telnet.KeepAlive = true;
  46:   
  47:              // allow addresses to be reused
  48:              //telnet.ReuseAddress = true;
  49:   
  50:              // do blocking connect
  51:              telnet.ReceiveTimeout = 20000;
  52:          }
  53:   
  54:          ////////////////////////////////////////////////////////////////////////////
  55:   
  56:          /// <summary>
  57:          ///
  58:          /// </summary>
  59:          public int Connect(string _hostname, string _username, string _password, string _prompt, out string result)
  60:          {
  61:              int op;
  62:   
  63:              op = 0;
  64:              result = "";
  65:   
  66:              hostname = _hostname;
  67:              username = _username;
  68:              password = _password;
  69:              unixPrompt = _prompt;
  70:   
  71:              try
  72:              {
  73:                  Segment value = telnet.Login(hostname, username, password, unixPrompt);
  74:                  result = value.ToString();
  75:   
  76:                  op = 1;
  77:              }
  78:              catch (SocketException ex)
  79:              {
  80:  #if DEBUG
  81:                  result = "Ia.Cl.Model.Telnet.Telnet(): " + ex.ToString();
  82:  #else
  83:                  result = "Ia.Cl.Model.Telnet.Telnet(): " + ex.Message;
  84:  #endif
  85:                  op = -1;
  86:              }
  87:              catch (InvalidOperationException ex)
  88:              {
  89:  #if DEBUG
  90:                  result = "Ia.Cl.Model.Telnet.Telnet(): " + ex.ToString();
  91:  #else
  92:                  result = "Ia.Cl.Model.Telnet.Telnet(): " + ex.Message;
  93:  #endif
  94:                  op = -1;
  95:              }
  96:              catch (Exception ex)
  97:              {
  98:  #if DEBUG
  99:                  result = "Ia.Cl.Model.Telnet.Telnet(): " + ex.ToString();
 100:  #else
 101:                  result = "Ia.Cl.Model.Telnet.Telnet(): " + ex.Message;
 102:  #endif
 103:                  op = -1;
 104:              }
 105:   
 106:              return op;
 107:          }
 108:   
 109:          ////////////////////////////////////////////////////////////////////////////
 110:   
 111:          /// <summary>
 112:          ///
 113:          /// </summary>
 114:          public bool IsConnected
 115:          {
 116:              get
 117:              {
 118:                  return telnet.Connected;
 119:              }
 120:          }
 121:   
 122:          ////////////////////////////////////////////////////////////////////////////
 123:   
 124:          /// <summary>
 125:          ///
 126:          /// </summary>
 127:          public void SendLine(string line)
 128:          {
 129:              string result;
 130:              StringBuilder sb;
 131:   
 132:              SendLine(line, out sb, out result);
 133:          }
 134:   
 135:          ////////////////////////////////////////////////////////////////////////////
 136:   
 137:          /// <summary>
 138:          ///
 139:          /// </summary>
 140:          public int SendLine(string line, out StringBuilder data, out string result)
 141:          {
 142:              int i;
 143:              string[] promptList;
 144:   
 145:              promptList = new string[1];
 146:              promptList[0] = unixPrompt;
 147:   
 148:              i = SendLine(line, promptList, out data, out result);
 149:   
 150:              return i;
 151:          }
 152:   
 153:          ////////////////////////////////////////////////////////////////////////////
 154:   
 155:          /// <summary>
 156:          ///
 157:          /// </summary>
 158:          public int SendLine(string line, string[] prompt, out StringBuilder data, out string result)
 159:          {
 160:              int op;
 161:   
 162:              op = 0;
 163:              result = "";
 164:              data = new StringBuilder(1000);
 165:              data.Length = 0;
 166:   
 167:              if (telnet.Connected)
 168:              {
 169:                  try
 170:                  {
 171:                      telnet.Send(line + "\r\n");
 172:                      data.Append(telnet.WaitFor(prompt));
 173:                      op = 1;
 174:                  }
 175:                  catch (Exception ex)
 176:                  {
 177:                      data.Length = 0;
 178:  #if DEBUG
 179:                      result = "Ia.Cl.Model.Telnet.Send(): " + ex.ToString();
 180:  #else
 181:                      result = "Ia.Cl.Model.Telnet.Send(): " + ex.Message;
 182:  #endif
 183:                      op = -1;
 184:                  }
 185:              }
 186:              else
 187:              {
 188:                  result = "Ia.Cl.Model.Telnet.Send(): Not connected";
 189:                  op = -1;
 190:              }
 191:   
 192:              return op;
 193:          }
 194:   
 195:          ////////////////////////////////////////////////////////////////////////////
 196:   
 197:          /// <summary>
 198:          ///
 199:          /// </summary>
 200:          public static string FormatAndCleanData(string s)
 201:          {
 202:              // convert any \n\r to \r\n
 203:              s = s.Replace("\n\r", "\r\n");
 204:   
 205:              // convert all naked \n to \r\n (2-step process)
 206:              s = s.Replace("\r\n", "\n");
 207:              s = s.Replace("\n", "\r\n");
 208:   
 209:              // replace all Tabs with spaces
 210:              s = s.Replace("\t", "     ");
 211:   
 212:              // remove <esc>[0m (all attributes off)
 213:              s = s.Replace("\x1b[0m", "");
 214:   
 215:              // remove <esc>[m (all attributes off)
 216:              s = s.Replace("\x1b[m", "");
 217:   
 218:              return s;
 219:          }
 220:   
 221:          ////////////////////////////////////////////////////////////////////////////
 222:   
 223:          /// <summary>
 224:          ///
 225:          /// </summary>
 226:          public void Disconnect()
 227:          {
 228:              if (telnet.Connected) telnet.Close();
 229:          }
 230:   
 231:          ////////////////////////////////////////////////////////////////////////////
 232:   
 233:          /// <summary>
 234:          /// Clean up any resources being used.
 235:          /// </summary>
 236:          public void Dispose()
 237:          {
 238:              Disconnect();
 239:   
 240:              telnet.Dispose();
 241:          }
 242:   
 243:          ////////////////////////////////////////////////////////////////////////////
 244:          ////////////////////////////////////////////////////////////////////////////
 245:      }
 246:  }