ROS:ROS操作类MK.cs
- class MK
- {
- Stream connection;
- TcpClient con;
- public MK(string ip,int port)
- {
- con = new TcpClient();
- con.Connect(ip, port);
- connection = (Stream)con.GetStream();
- }
- public void Close()
- {
- connection.Close();
- con.Close();
- }
- public bool Login(string username, string password)
- {
- Send("/login", true);
- string hash = Read()[].Split(new string[] { "ret=" }, StringSplitOptions.None)[];
- Send("/login");
- Send("=name=" + username);
- Send("=response=00" + EncodePassword(password, hash), true);
- if (Read()[] == "!done")
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- public void Send(string co)
- {
- byte[] bajty = Encoding.GetEncoding("GB2312").GetBytes(co.ToCharArray());
- byte[] velikost = EncodeLength(bajty.Length);
- connection.Write(velikost, , velikost.Length);
- connection.Write(bajty, , bajty.Length);
- }
- public void Send(string co, bool endsentence)
- {
- byte[] bajty = Encoding.GetEncoding("GB2312").GetBytes(co.ToCharArray());
- byte[] velikost = EncodeLength(bajty.Length);
- connection.Write(velikost, , velikost.Length);
- connection.Write(bajty, , bajty.Length);
- connection.WriteByte();
- }
- public List<string> Read()
- {
- List<string> output = new List<string>();
- string o = "";
- byte[] tmp = new byte[];
- long count;
- while (true)
- {
- tmp[] = (byte)connection.ReadByte();
- //if(tmp[3] == 220) tmp[3] = (byte)connection.ReadByte(); it sometimes happend to me that
- //mikrotik send 220 as some kind of "bonus" between words, this fixed things, not sure about it though
- if (tmp[] == )
- {
- output.Add(o);
- if (o.Substring(, ) == "!done")
- {
- break;
- }
- else
- {
- o = "";
- continue;
- }
- }
- else
- {
- if (tmp[] < 0x80)
- {
- count = tmp[];
- }
- else
- {
- if (tmp[] < 0xC0)
- {
- int tmpi = BitConverter.ToInt32(new byte[] { (byte)connection.ReadByte(), tmp[], , }, );
- count = tmpi ^ 0x8000;
- }
- else
- {
- if (tmp[] < 0xE0)
- {
- tmp[] = (byte)connection.ReadByte();
- int tmpi = BitConverter.ToInt32(new byte[] { (byte)connection.ReadByte(), tmp[], tmp[], }, );
- count = tmpi ^ 0xC00000;
- }
- else
- {
- if (tmp[] < 0xF0)
- {
- tmp[] = (byte)connection.ReadByte();
- tmp[] = (byte)connection.ReadByte();
- int tmpi = BitConverter.ToInt32(new byte[] { (byte)connection.ReadByte(), tmp[], tmp[], tmp[] }, );
- count = tmpi ^ 0xE0000000;
- }
- else
- {
- if (tmp[] == 0xF0)
- {
- tmp[] = (byte)connection.ReadByte();
- tmp[] = (byte)connection.ReadByte();
- tmp[] = (byte)connection.ReadByte();
- tmp[] = (byte)connection.ReadByte();
- count = BitConverter.ToInt32(tmp, );
- }
- else
- {
- //Error in packet reception, unknown length
- break;
- }
- }
- }
- }
- }
- }
- for (int i = ; i < count; i++)
- {
- o += (Char)connection.ReadByte();
- }
- }
- return output;
- }
- byte[] EncodeLength(int delka)
- {
- if (delka < 0x80)
- {
- byte[] tmp = BitConverter.GetBytes(delka);
- return new byte[] { tmp[] };
- }
- if (delka < 0x4000)
- {
- byte[] tmp = BitConverter.GetBytes(delka | 0x8000);
- return new byte[] { tmp[], tmp[] };
- }
- if (delka < 0x200000)
- {
- byte[] tmp = BitConverter.GetBytes(delka | 0xC00000);
- return new byte[] { tmp[], tmp[], tmp[] };
- }
- if (delka < 0x10000000)
- {
- byte[] tmp = BitConverter.GetBytes(delka | 0xE0000000);
- return new byte[] { tmp[], tmp[], tmp[], tmp[] };
- }
- else
- {
- byte[] tmp = BitConverter.GetBytes(delka);
- return new byte[] { 0xF0, tmp[], tmp[], tmp[], tmp[] };
- }
- }
- public string EncodePassword(string Password, string hash)
- {
- byte[] hash_byte = new byte[hash.Length / ];
- for (int i = ; i <= hash.Length - ; i += )
- {
- hash_byte[i / ] = Byte.Parse(hash.Substring(i, ), System.Globalization.NumberStyles.HexNumber);
- }
- byte[] heslo = new byte[ + Password.Length + hash_byte.Length];
- heslo[] = ;
- Encoding.ASCII.GetBytes(Password.ToCharArray()).CopyTo(heslo, );
- hash_byte.CopyTo(heslo, + Password.Length);
- Byte[] hotovo;
- System.Security.Cryptography.MD5 md5;
- md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
- hotovo = md5.ComputeHash(heslo);
- //Convert encoded bytes back to a 'readable' string
- string navrat = "";
- foreach (byte h in hotovo)
- {
- navrat += h.ToString("x2");
- }
- return navrat;
- }
- }
此类操作类,不需要修改,直接引用即可
下面这是一个 ROS 的 activeuser类
- class RosMkClass
- {
- AppSetting _setting;
- public RosMkClass(AppSetting setting)
- {
- _setting = setting;
- }
- /// <summary>
- /// 软路由返回的消息类
- /// </summary>
- class Ros_Message
- {
- /// <summary>
- /// 是否成功,成功为true 不成功为false
- /// </summary>
- public bool Success { get; set; }
- /// <summary>
- /// 软路由返回的消息
- /// </summary>
- public string Message { get; set; }
- }
- private Ros_Message fenxi(List<string> list)
- {
- Ros_Message message = new Ros_Message();
- message.Success = false;
- foreach (string item in list)
- {
- Regex reg1 = new Regex(@"(?<=message=).*(?=.tag)");
- Regex reg2 = new Regex(@"(?<=ret=\*).*(?=.tag)");
- if (item.IndexOf("message") > )
- {
- message.Message = reg1.Match(item).ToString();
- message.Success = false;
- break;
- }
- if (item.IndexOf("ret") > )
- {
- message.Message = reg2.Match(item).ToString();
- message.Success = true;
- break;
- }
- if (item.IndexOf("done") > )
- {
- message.Success = true;
- break;
- }
- }
- return message;
- }
- public class RosActiveUser
- {
- public RosActiveUser(string RetString)
- {
- Regex rg = new Regex(@"(?<=.id=\*).*(?==name)");
- _ID = rg.Match(RetString).ToString();
- rg = new Regex(@"(?<=name=).*(?==service)");
- _username = rg.Match(RetString).ToString();
- rg = new Regex(@"(?<=caller-id=).*(?==address)");
- _macaddress = rg.Match(RetString).ToString();
- }
- private string _ID;
- public string ID
- {
- get { return _ID; }
- set { _ID = value; }
- }
- private string _username;
- public string Username
- {
- get { return _username; }
- set { _username = value; }
- }
- private string _macaddress;
- public string MacAddress
- {
- get { return _macaddress; }
- set { _macaddress = value; }
- }
- }
- private List<RosActiveUser> activelist()
- {
- MK mk = new MK(_setting.Ros_IP, _setting.Ros_Port);
- List<string> list = new List<string>();
- List<RosActiveUser> activelist = new List<RosActiveUser>();
- if (mk.Login(_setting.Ros_Admin, _setting.Ros_Password))
- {
- mk.Send(string.Format("/ppp/active/print"));
- mk.Send(".tag=act", true);
- list.AddRange(mk.Read());
- mk.Close();
- }
- foreach (string item in list)
- {
- if (item.IndexOf(".tag=act") > )
- {
- activelist.Add(new RosActiveUser(item));
- }
- }
- return activelist;
- }
- private Ros_Message Ros_ActiveRemove(string username)
- {
- List<RosActiveUser> ActList = activelist();
- List<string> list = new List<string>();
- RosActiveUser act = ActList.Find(a => a.Username == username);
- if (act != null)
- {
- MK mk = new MK(_setting.Ros_IP, _setting.Ros_Port);
- if (mk.Login(_setting.Ros_Admin, _setting.Ros_Password))
- {
- mk.Send("/ppp/active/remove");
- mk.Send(string.Format("=.id=*{0}", act.ID)); //"=.id=张刚"
- mk.Send(".tag=ss1", true);
- list.AddRange(mk.Read());
- mk.Close();
- }
- }
- return fenxi(list);
- }
- private Ros_Message Ros_SecretRemove(string name)
- {
- List<string> list = new List<string>();
- MK mk = new MK(_setting.Ros_IP, _setting.Ros_Port);
- if (mk.Login(_setting.Ros_Admin, _setting.Ros_Password))
- {
- mk.Send("/ppp/secret/remove");
- mk.Send(string.Format("=.id={0}", name)); //"=.id=张刚"
- mk.Send(".tag=ss1", true);
- list.AddRange(mk.Read());
- mk.Close();
- }
- return fenxi(list);
- }
- private Ros_Message Ros_Create(Teacher teacher)
- {
- List<string> list = new List<string>();
- MK mk = new MK(_setting.Ros_IP, _setting.Ros_Port);
- if (mk.Login(_setting.Ros_Admin, _setting.Ros_Password))
- {
- mk.Send("/ppp/secret/add");
- mk.Send(string.Format("=name={0}", teacher.UserName));
- mk.Send(string.Format("=password={0}", teacher.Password));
- mk.Send(string.Format("=service={0}", "pppoe"));
- mk.Send(string.Format("=profile=profile{0}", teacher.Level));
- mk.Send(string.Format("=comment={0}", teacher.Name));
- mk.Send(".tag=ss2", true);
- list.AddRange(mk.Read());
- mk.Close();
- }
- return fenxi(list);
- }
- /// <summary>
- /// 查看 ROS 是不否可以连接成功.检测 ROS 的设置参数.
- /// </summary>
- /// <returns>是否连接成功!</returns>
- public bool Ros_Connected()
- {
- bool isconn = false;
- MK mk = new MK(_setting.Ros_IP, _setting.Ros_Port);
- isconn = mk.Login(_setting.Ros_Admin, _setting.Ros_Password);
- mk.Close();
- return isconn;
- }
- public string UpdateAccount(Teacher teacher) //汉字姓名
- {
- Ros_Message sec = Ros_SecretRemove(teacher.Name);
- Ros_Message sct = Ros_ActiveRemove(teacher.Oldname);
- Ros_Message ms = Ros_Create(teacher);
- return ms.Success + ":" +sec.Message+"&&"+sct.Message+"&&"+ms.Message;
- }
- public string RemoveActive(string oldname)
- {
- return Ros_ActiveRemove(oldname).Message;
- }
- }
注意以下几点
修改 class MK 里面的文件
将
byte[] bajty = Encoding.ASCII.GetBytes(co.ToCharArray());
修改为
byte[] bajty = Encoding.GetEncoding("GB2312").GetBytes(co.ToCharArray());
即可接收汉字了
删除用户的时候,需要 使用 ID编号 如 *212
ROS:ROS操作类MK.cs的更多相关文章
- C# 网络常用操作类NetHelper.cs
一个非常完整的网络操作帮助类,包含20多个常用方法,例如: IP地址的验证以及截取. 端口的验证. 电子邮件的发送. 获取计算机名. IP地址的获取以及TCP. UDP连接的创建和数据发送等. usi ...
- 开发C# .net时使用的数据库操作类SqlHelp.cs
练习开发WPF程序的时候,是这样写的,虽然很简单,相必很多新手会用到,所以拿来共享一下, using System; using System.Collections.Generic; using S ...
- C# ADO.NET操作数据库 SqlHelp.cs类
刚开始练习ADONET的时候,练习的一个SQLHelp.cs 数据库操作类,很简单,但是也很实用 using System; using System.Collections.Generic; us ...
- 一个非常好的C#字符串操作处理类StringHelper.cs
/// <summary> /// 类说明:Assistant /// 编 码 人:苏飞 /// 联系方式:361983679 /// 更新网站:http://www.sufeinet.c ...
- Util应用程序框架公共操作类(九):Lambda表达式扩展
上一篇对Lambda表达式公共操作类进行了一些增强,本篇使用扩展方法对Lambda表达式进行扩展. 修改Util项目的Extensions.Expression.cs文件,代码如下. using Sy ...
- Util应用程序框架公共操作类(三):数据类型转换公共操作类(扩展篇)
上一篇以TDD方式介绍了数据类型转换公共操作类的开发,并提供了单元测试和实现代码,本文将演示通过扩展方法来增强公共操作类,以便调用时更加简化. 下面以字符串转换为List<Guid>为例进 ...
- Util应用程序框架公共操作类(二):数据类型转换公共操作类(源码篇)
上一篇介绍了数据类型转换的一些情况,可以看出,如果不进行封装,有可能导致比较混乱的代码.本文通过TDD方式把数据类型转换公共操作类开发出来,并提供源码下载. 我们在 应用程序框架实战十一:创建VS解决 ...
- FlexPaper+SWFTool+操作类=在线预览PDF
引言 由于客户有在线预览PDF格式的需求,在网上找了一下解决方案,觉得FlexPaper用起来还是挺方便的,flexpaper是将pdf转换为swf格式的文件预览的,所以flexpaper一般和swf ...
- 【C#】分享基于Win32 API的服务操作类(解决ManagedInstallerClass.InstallHelper不能带参数安装的问题)
注:这里的服务是指Windows 服务. ------------------201508250915更新------------------ 刚刚得知TransactedInstaller类是支持带 ...
随机推荐
- ECharts学习记录
一.ECharts在GitHub的地址以及需要引入文件地址: 1.Github地址:https://github.com/ecomfe/echarts 2.官网下载文件地址:http://echart ...
- 【Python爬虫实战】 图片爬虫-淘宝图片爬虫--千图网图片爬虫
所谓图片爬虫,就是从互联网中自动把对方服务器上的图片爬下来的爬虫程序.有些图片是直接在html文件里面,有些是隐藏在JS文件中,在html文件中只需要我们分析源码就能得到如果是隐藏在JS文件中,那么就 ...
- 读O目标KR关键结果的一些个人理解
O目标KR关键结果 为了完成一个目标,需要完成几个或者多个关键的结果来验证. 书的开头写的是一些理论,有印象的东西还是从汉娜和杰克的公司来说,卖茶叶的公司.联系着茶农和可以产生消费的餐馆和供应商,在未 ...
- 代码: js: 数值操作
数值转换: 将 32000 这样的数字,转换为“3.2万” //将32000 这样的数字,转换为 “3.2万” var price = parseInt('31999'); var price2 = ...
- EA Data Modeling 显示别名设置
1.设置 2.效果
- while循环、break、continue
我们通过while循环让python循环进行操作 break 跳出整个循环 continue 终止当前循环并不再继续往下执行,回到开头开始继续循环 下面会详细解释一下,例如: 1 a = 1 2 wh ...
- windows2012系统IE浏览器无法打开加载flashplayer内容
添加角色和功能,用户界面和基础结构,桌面体检,安装完重启电脑
- elk之[logstash-input-file]插件使用详解
https://www.cnblogs.com/xing901022/p/4805586.html http://www.cnblogs.com/xing901022/p/4802822.html ...
- $tojson和json.stringify的区别
JSON.stringify(),将value(Object,Array,String,Number...)序列化为JSON字符串 JSON.parse(), 将JSON数据解析为js原生值 toJS ...
- tensorflow实战系列(二)TFRecordReader
前面写了TFRecordWriter的生成.这次写TFRecordReader. 代码附上: def read_and_decode(filename): #根据文件名生成一个队列 fil ...