شركة التطبيقات المتكاملة لتصميم النظم البرمجية الخاصة

Integrated Applications Programming Company

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