)>}]
شركة التطبيقات المتكاملة لتصميم وبرمجة البرمجيات الخاصة ش.ش.و.
Integrated Applications Programming Company
Home » Code Library » Telnet (Ia.Cl.Models)

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.Net.Sockets;
    3: using System.Text;
    4:  
    5: namespace Ia.Cl.Models
    6: {
    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.Telnet.Telnet telnet = new Dart.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.Models.Telnet.Telnet(): " + ex.ToString();
   74: #else
   75:                 result = "Ia.Cl.Models.Telnet.Telnet(): " + ex.Message;
   76: #endif
   77:                 op = -1;
   78:             }
   79:             catch (InvalidOperationException ex)
   80:             {
   81: #if DEBUG
   82:                 result = "Ia.Cl.Models.Telnet.Telnet(): " + ex.ToString();
   83: #else
   84:                 result = "Ia.Cl.Models.Telnet.Telnet(): " + ex.Message;
   85: #endif
   86:                 op = -1;
   87:             }
   88:             catch (Exception ex)
   89:             {
   90: #if DEBUG
   91:                 result = "Ia.Cl.Models.Telnet.Telnet(): " + ex.ToString();
   92: #else
   93:                 result = "Ia.Cl.Models.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 false; //// 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:             /*
  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.Models.Telnet.Send(): " + ex.ToString();
  173: #else
  174:                     result = "Ia.Cl.Models.Telnet.Send(): " + ex.Message;
  175: #endif
  176:                     op = -1;
  177:                 }
  178:             }
  179:             else
  180:             {
  181:                 result = "Ia.Cl.Models.Telnet.Send(): Not connected";
  182:                 op = -1;
  183:             }
  184:             */
  185:  
  186:             return op;
  187:         }
  188:  
  189:         ////////////////////////////////////////////////////////////////////////////
  190:  
  191:         /// <summary>
  192:         ///
  193:         /// </summary>
  194:         public static string FormatAndCleanData(string s)
  195:         {
  196:             // convert any \n\r to \r\n
  197:             s = s.Replace("\n\r", "\r\n");
  198:  
  199:             // convert all naked \n to \r\n (2-step process)
  200:             s = s.Replace("\r\n", "\n");
  201:             s = s.Replace("\n", "\r\n");
  202:  
  203:             // replace all Tabs with spaces
  204:             s = s.Replace("\t", "     ");
  205:  
  206:             // remove <esc>[0m (all attributes off)
  207:             s = s.Replace("\x1b[0m", "");
  208:  
  209:             // remove <esc>[m (all attributes off)
  210:             s = s.Replace("\x1b[m", "");
  211:  
  212:             return s;
  213:         }
  214:  
  215:         ////////////////////////////////////////////////////////////////////////////
  216:  
  217:         /// <summary>
  218:         ///
  219:         /// </summary>
  220:         public void Disconnect()
  221:         {
  222:             //// if (telnet.Connected) telnet.Close();
  223:         }
  224:  
  225:         ////////////////////////////////////////////////////////////////////////////
  226:  
  227:         /// <summary>
  228:         /// Clean up any resources being used.
  229:         /// </summary>
  230:         public void Dispose()
  231:         {
  232:             Disconnect();
  233:  
  234:             telnet.Dispose();
  235:         }
  236:  
  237:         ////////////////////////////////////////////////////////////////////////////
  238:         ////////////////////////////////////////////////////////////////////////////
  239:     }
  240: }