1. class MK
  2. {
  3. Stream connection;
  4. TcpClient con;
  5.  
  6. public MK(string ip,int port)
  7. {
  8. con = new TcpClient();
  9. con.Connect(ip, port);
  10. connection = (Stream)con.GetStream();
  11. }
  12. public void Close()
  13. {
  14. connection.Close();
  15. con.Close();
  16. }
  17. public bool Login(string username, string password)
  18. {
  19. Send("/login", true);
  20. string hash = Read()[].Split(new string[] { "ret=" }, StringSplitOptions.None)[];
  21. Send("/login");
  22. Send("=name=" + username);
  23. Send("=response=00" + EncodePassword(password, hash), true);
  24. if (Read()[] == "!done")
  25. {
  26. return true;
  27. }
  28. else
  29. {
  30. return false;
  31. }
  32. }
  33. public void Send(string co)
  34. {
  35. byte[] bajty = Encoding.GetEncoding("GB2312").GetBytes(co.ToCharArray());
  36. byte[] velikost = EncodeLength(bajty.Length);
  37.  
  38. connection.Write(velikost, , velikost.Length);
  39. connection.Write(bajty, , bajty.Length);
  40. }
  41. public void Send(string co, bool endsentence)
  42. {
  43. byte[] bajty = Encoding.GetEncoding("GB2312").GetBytes(co.ToCharArray());
  44. byte[] velikost = EncodeLength(bajty.Length);
  45. connection.Write(velikost, , velikost.Length);
  46. connection.Write(bajty, , bajty.Length);
  47. connection.WriteByte();
  48. }
  49. public List<string> Read()
  50. {
  51. List<string> output = new List<string>();
  52. string o = "";
  53. byte[] tmp = new byte[];
  54. long count;
  55. while (true)
  56. {
  57. tmp[] = (byte)connection.ReadByte();
  58. //if(tmp[3] == 220) tmp[3] = (byte)connection.ReadByte(); it sometimes happend to me that
  59. //mikrotik send 220 as some kind of "bonus" between words, this fixed things, not sure about it though
  60. if (tmp[] == )
  61. {
  62. output.Add(o);
  63. if (o.Substring(, ) == "!done")
  64. {
  65. break;
  66. }
  67. else
  68. {
  69. o = "";
  70. continue;
  71. }
  72. }
  73. else
  74. {
  75. if (tmp[] < 0x80)
  76. {
  77. count = tmp[];
  78. }
  79. else
  80. {
  81. if (tmp[] < 0xC0)
  82. {
  83. int tmpi = BitConverter.ToInt32(new byte[] { (byte)connection.ReadByte(), tmp[], , }, );
  84. count = tmpi ^ 0x8000;
  85. }
  86. else
  87. {
  88. if (tmp[] < 0xE0)
  89. {
  90. tmp[] = (byte)connection.ReadByte();
  91. int tmpi = BitConverter.ToInt32(new byte[] { (byte)connection.ReadByte(), tmp[], tmp[], }, );
  92. count = tmpi ^ 0xC00000;
  93. }
  94. else
  95. {
  96. if (tmp[] < 0xF0)
  97. {
  98. tmp[] = (byte)connection.ReadByte();
  99. tmp[] = (byte)connection.ReadByte();
  100. int tmpi = BitConverter.ToInt32(new byte[] { (byte)connection.ReadByte(), tmp[], tmp[], tmp[] }, );
  101. count = tmpi ^ 0xE0000000;
  102. }
  103. else
  104. {
  105. if (tmp[] == 0xF0)
  106. {
  107. tmp[] = (byte)connection.ReadByte();
  108. tmp[] = (byte)connection.ReadByte();
  109. tmp[] = (byte)connection.ReadByte();
  110. tmp[] = (byte)connection.ReadByte();
  111. count = BitConverter.ToInt32(tmp, );
  112. }
  113. else
  114. {
  115. //Error in packet reception, unknown length
  116. break;
  117. }
  118. }
  119. }
  120. }
  121. }
  122. }
  123.  
  124. for (int i = ; i < count; i++)
  125. {
  126. o += (Char)connection.ReadByte();
  127. }
  128. }
  129. return output;
  130. }
  131. byte[] EncodeLength(int delka)
  132. {
  133. if (delka < 0x80)
  134. {
  135. byte[] tmp = BitConverter.GetBytes(delka);
  136. return new byte[] { tmp[] };
  137. }
  138. if (delka < 0x4000)
  139. {
  140. byte[] tmp = BitConverter.GetBytes(delka | 0x8000);
  141. return new byte[] { tmp[], tmp[] };
  142. }
  143. if (delka < 0x200000)
  144. {
  145. byte[] tmp = BitConverter.GetBytes(delka | 0xC00000);
  146. return new byte[] { tmp[], tmp[], tmp[] };
  147. }
  148. if (delka < 0x10000000)
  149. {
  150. byte[] tmp = BitConverter.GetBytes(delka | 0xE0000000);
  151. return new byte[] { tmp[], tmp[], tmp[], tmp[] };
  152. }
  153. else
  154. {
  155. byte[] tmp = BitConverter.GetBytes(delka);
  156. return new byte[] { 0xF0, tmp[], tmp[], tmp[], tmp[] };
  157. }
  158. }
  159.  
  160. public string EncodePassword(string Password, string hash)
  161. {
  162. byte[] hash_byte = new byte[hash.Length / ];
  163. for (int i = ; i <= hash.Length - ; i += )
  164. {
  165. hash_byte[i / ] = Byte.Parse(hash.Substring(i, ), System.Globalization.NumberStyles.HexNumber);
  166. }
  167. byte[] heslo = new byte[ + Password.Length + hash_byte.Length];
  168. heslo[] = ;
  169. Encoding.ASCII.GetBytes(Password.ToCharArray()).CopyTo(heslo, );
  170. hash_byte.CopyTo(heslo, + Password.Length);
  171.  
  172. Byte[] hotovo;
  173. System.Security.Cryptography.MD5 md5;
  174.  
  175. md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
  176.  
  177. hotovo = md5.ComputeHash(heslo);
  178.  
  179. //Convert encoded bytes back to a 'readable' string
  180. string navrat = "";
  181. foreach (byte h in hotovo)
  182. {
  183. navrat += h.ToString("x2");
  184. }
  185. return navrat;
  186. }
  187.  
  188. }

此类操作类,不需要修改,直接引用即可

下面这是一个 ROS  的 activeuser类

  1. class RosMkClass
  2. {
  3. AppSetting _setting;
  4. public RosMkClass(AppSetting setting)
  5. {
  6. _setting = setting;
  7. }
  8.  
  9. /// <summary>
  10. /// 软路由返回的消息类
  11. /// </summary>
  12. class Ros_Message
  13. {
  14. /// <summary>
  15. /// 是否成功,成功为true 不成功为false
  16. /// </summary>
  17. public bool Success { get; set; }
  18. /// <summary>
  19. /// 软路由返回的消息
  20. /// </summary>
  21. public string Message { get; set; }
  22. }
  23.  
  24. private Ros_Message fenxi(List<string> list)
  25. {
  26. Ros_Message message = new Ros_Message();
  27. message.Success = false;
  28. foreach (string item in list)
  29. {
  30. Regex reg1 = new Regex(@"(?<=message=).*(?=.tag)");
  31. Regex reg2 = new Regex(@"(?<=ret=\*).*(?=.tag)");
  32.  
  33. if (item.IndexOf("message") > )
  34. {
  35. message.Message = reg1.Match(item).ToString();
  36. message.Success = false;
  37. break;
  38. }
  39.  
  40. if (item.IndexOf("ret") > )
  41. {
  42. message.Message = reg2.Match(item).ToString();
  43. message.Success = true;
  44. break;
  45. }
  46.  
  47. if (item.IndexOf("done") > )
  48. {
  49. message.Success = true;
  50. break;
  51. }
  52.  
  53. }
  54.  
  55. return message;
  56.  
  57. }
  58.  
  59. public class RosActiveUser
  60. {
  61.  
  62. public RosActiveUser(string RetString)
  63. {
  64. Regex rg = new Regex(@"(?<=.id=\*).*(?==name)");
  65. _ID = rg.Match(RetString).ToString();
  66. rg = new Regex(@"(?<=name=).*(?==service)");
  67. _username = rg.Match(RetString).ToString();
  68. rg = new Regex(@"(?<=caller-id=).*(?==address)");
  69. _macaddress = rg.Match(RetString).ToString();
  70. }
  71.  
  72. private string _ID;
  73.  
  74. public string ID
  75. {
  76. get { return _ID; }
  77. set { _ID = value; }
  78. }
  79.  
  80. private string _username;
  81.  
  82. public string Username
  83. {
  84. get { return _username; }
  85. set { _username = value; }
  86. }
  87.  
  88. private string _macaddress;
  89. public string MacAddress
  90. {
  91. get { return _macaddress; }
  92. set { _macaddress = value; }
  93. }
  94.  
  95. }
  96.  
  97. private List<RosActiveUser> activelist()
  98. {
  99. MK mk = new MK(_setting.Ros_IP, _setting.Ros_Port);
  100. List<string> list = new List<string>();
  101. List<RosActiveUser> activelist = new List<RosActiveUser>();
  102. if (mk.Login(_setting.Ros_Admin, _setting.Ros_Password))
  103. {
  104.  
  105. mk.Send(string.Format("/ppp/active/print"));
  106. mk.Send(".tag=act", true);
  107. list.AddRange(mk.Read());
  108. mk.Close();
  109. }
  110.  
  111. foreach (string item in list)
  112. {
  113. if (item.IndexOf(".tag=act") > )
  114. {
  115. activelist.Add(new RosActiveUser(item));
  116. }
  117. }
  118.  
  119. return activelist;
  120. }
  121.  
  122. private Ros_Message Ros_ActiveRemove(string username)
  123. {
  124. List<RosActiveUser> ActList = activelist();
  125. List<string> list = new List<string>();
  126.  
  127. RosActiveUser act = ActList.Find(a => a.Username == username);
  128.  
  129. if (act != null)
  130. {
  131.  
  132. MK mk = new MK(_setting.Ros_IP, _setting.Ros_Port);
  133. if (mk.Login(_setting.Ros_Admin, _setting.Ros_Password))
  134. {
  135. mk.Send("/ppp/active/remove");
  136. mk.Send(string.Format("=.id=*{0}", act.ID)); //"=.id=张刚"
  137. mk.Send(".tag=ss1", true);
  138. list.AddRange(mk.Read());
  139. mk.Close();
  140. }
  141.  
  142. }
  143.  
  144. return fenxi(list);
  145. }
  146.  
  147. private Ros_Message Ros_SecretRemove(string name)
  148. {
  149. List<string> list = new List<string>();
  150. MK mk = new MK(_setting.Ros_IP, _setting.Ros_Port);
  151. if (mk.Login(_setting.Ros_Admin, _setting.Ros_Password))
  152. {
  153. mk.Send("/ppp/secret/remove");
  154. mk.Send(string.Format("=.id={0}", name)); //"=.id=张刚"
  155. mk.Send(".tag=ss1", true);
  156. list.AddRange(mk.Read());
  157. mk.Close();
  158. }
  159. return fenxi(list);
  160. }
  161.  
  162. private Ros_Message Ros_Create(Teacher teacher)
  163. {
  164. List<string> list = new List<string>();
  165. MK mk = new MK(_setting.Ros_IP, _setting.Ros_Port);
  166. if (mk.Login(_setting.Ros_Admin, _setting.Ros_Password))
  167. {
  168. mk.Send("/ppp/secret/add");
  169. mk.Send(string.Format("=name={0}", teacher.UserName));
  170. mk.Send(string.Format("=password={0}", teacher.Password));
  171. mk.Send(string.Format("=service={0}", "pppoe"));
  172. mk.Send(string.Format("=profile=profile{0}", teacher.Level));
  173. mk.Send(string.Format("=comment={0}", teacher.Name));
  174. mk.Send(".tag=ss2", true);
  175. list.AddRange(mk.Read());
  176. mk.Close();
  177. }
  178. return fenxi(list);
  179. }
  180. /// <summary>
  181. /// 查看 ROS 是不否可以连接成功.检测 ROS 的设置参数.
  182. /// </summary>
  183. /// <returns>是否连接成功!</returns>
  184. public bool Ros_Connected()
  185. {
  186. bool isconn = false;
  187. MK mk = new MK(_setting.Ros_IP, _setting.Ros_Port);
  188. isconn = mk.Login(_setting.Ros_Admin, _setting.Ros_Password);
  189. mk.Close();
  190. return isconn;
  191. }
  192.  
  193. public string UpdateAccount(Teacher teacher) //汉字姓名
  194. {
  195. Ros_Message sec = Ros_SecretRemove(teacher.Name);
  196. Ros_Message sct = Ros_ActiveRemove(teacher.Oldname);
  197. Ros_Message ms = Ros_Create(teacher);
  198. return ms.Success + ":" +sec.Message+"&&"+sct.Message+"&&"+ms.Message;
  199. }
  200.  
  201. public string RemoveActive(string oldname)
  202. {
  203. return Ros_ActiveRemove(oldname).Message;
  204. }
  205.  
  206. }

注意以下几点

修改 class MK 里面的文件

byte[] bajty = Encoding.ASCII.GetBytes(co.ToCharArray());

修改为

byte[] bajty = Encoding.GetEncoding("GB2312").GetBytes(co.ToCharArray());

即可接收汉字了

删除用户的时候,需要 使用 ID编号    如   *212

ROS:ROS操作类MK.cs的更多相关文章

  1. C# 网络常用操作类NetHelper.cs

    一个非常完整的网络操作帮助类,包含20多个常用方法,例如: IP地址的验证以及截取. 端口的验证. 电子邮件的发送. 获取计算机名. IP地址的获取以及TCP. UDP连接的创建和数据发送等. usi ...

  2. 开发C# .net时使用的数据库操作类SqlHelp.cs

    练习开发WPF程序的时候,是这样写的,虽然很简单,相必很多新手会用到,所以拿来共享一下, using System; using System.Collections.Generic; using S ...

  3. C# ADO.NET操作数据库 SqlHelp.cs类

    刚开始练习ADONET的时候,练习的一个SQLHelp.cs  数据库操作类,很简单,但是也很实用 using System; using System.Collections.Generic; us ...

  4. 一个非常好的C#字符串操作处理类StringHelper.cs

    /// <summary> /// 类说明:Assistant /// 编 码 人:苏飞 /// 联系方式:361983679 /// 更新网站:http://www.sufeinet.c ...

  5. Util应用程序框架公共操作类(九):Lambda表达式扩展

    上一篇对Lambda表达式公共操作类进行了一些增强,本篇使用扩展方法对Lambda表达式进行扩展. 修改Util项目的Extensions.Expression.cs文件,代码如下. using Sy ...

  6. Util应用程序框架公共操作类(三):数据类型转换公共操作类(扩展篇)

    上一篇以TDD方式介绍了数据类型转换公共操作类的开发,并提供了单元测试和实现代码,本文将演示通过扩展方法来增强公共操作类,以便调用时更加简化. 下面以字符串转换为List<Guid>为例进 ...

  7. Util应用程序框架公共操作类(二):数据类型转换公共操作类(源码篇)

    上一篇介绍了数据类型转换的一些情况,可以看出,如果不进行封装,有可能导致比较混乱的代码.本文通过TDD方式把数据类型转换公共操作类开发出来,并提供源码下载. 我们在 应用程序框架实战十一:创建VS解决 ...

  8. FlexPaper+SWFTool+操作类=在线预览PDF

    引言 由于客户有在线预览PDF格式的需求,在网上找了一下解决方案,觉得FlexPaper用起来还是挺方便的,flexpaper是将pdf转换为swf格式的文件预览的,所以flexpaper一般和swf ...

  9. 【C#】分享基于Win32 API的服务操作类(解决ManagedInstallerClass.InstallHelper不能带参数安装的问题)

    注:这里的服务是指Windows 服务. ------------------201508250915更新------------------ 刚刚得知TransactedInstaller类是支持带 ...

随机推荐

  1. ECharts学习记录

    一.ECharts在GitHub的地址以及需要引入文件地址: 1.Github地址:https://github.com/ecomfe/echarts 2.官网下载文件地址:http://echart ...

  2. 【Python爬虫实战】 图片爬虫-淘宝图片爬虫--千图网图片爬虫

    所谓图片爬虫,就是从互联网中自动把对方服务器上的图片爬下来的爬虫程序.有些图片是直接在html文件里面,有些是隐藏在JS文件中,在html文件中只需要我们分析源码就能得到如果是隐藏在JS文件中,那么就 ...

  3. 读O目标KR关键结果的一些个人理解

    O目标KR关键结果 为了完成一个目标,需要完成几个或者多个关键的结果来验证. 书的开头写的是一些理论,有印象的东西还是从汉娜和杰克的公司来说,卖茶叶的公司.联系着茶农和可以产生消费的餐馆和供应商,在未 ...

  4. 代码: js: 数值操作

    数值转换: 将 32000 这样的数字,转换为“3.2万” //将32000 这样的数字,转换为 “3.2万” var price = parseInt('31999'); var price2 = ...

  5. EA Data Modeling 显示别名设置

    1.设置 2.效果 

  6. while循环、break、continue

    我们通过while循环让python循环进行操作 break 跳出整个循环 continue 终止当前循环并不再继续往下执行,回到开头开始继续循环 下面会详细解释一下,例如: 1 a = 1 2 wh ...

  7. windows2012系统IE浏览器无法打开加载flashplayer内容

    添加角色和功能,用户界面和基础结构,桌面体检,安装完重启电脑

  8. elk之[logstash-input-file]插件使用详解

    https://www.cnblogs.com/xing901022/p/4805586.html http://www.cnblogs.com/xing901022/p/4802822.html   ...

  9. $tojson和json.stringify的区别

    JSON.stringify(),将value(Object,Array,String,Number...)序列化为JSON字符串 JSON.parse(), 将JSON数据解析为js原生值 toJS ...

  10. tensorflow实战系列(二)TFRecordReader

    前面写了TFRecordWriter的生成.这次写TFRecordReader. 代码附上: def read_and_decode(filename):    #根据文件名生成一个队列    fil ...