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

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