C# Telnet 類庫代碼 今天給筆記本裝了固態(tài),速度提升很明顯,很高興,發(fā)個(gè)微博留作紀(jì)念。 自學(xué)的C#,干網(wǎng)絡(luò)的,想弄個(gè)工具方便日常工作,想自己實(shí)現(xiàn)批量操作的工具。 想起來很簡單做起來很費(fèi)勁。網(wǎng)上一頓亂查。 這里感謝“Telnet 非常實(shí)用的類庫 - 王小壯的博客 - CSDN博客” https://blog.csdn.net/weixin_42183571/article/details/80783268 這個(gè)文章給了我方向,代碼看了N遍,有了很多啟發(fā)。 那里面的異步?jīng)]看懂,當(dāng)時(shí)不懂異步,看著和天數(shù)一樣。。。。 什么協(xié)議都是有協(xié)商的過程: telnet協(xié)商代碼 取自 王小壯 ,有一小點(diǎn)的修改。 多了不BB,上代碼。using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Collections; using System.IO; using System.Net; using System.Net.Sockets; using System.Threading; namespace 項(xiàng)目名字 { public class Telnet { #region // 協(xié)商的命令碼直接復(fù)制 // private byte[] receivebytes = new byte[1024]; //接受 readonly Char IAC = Convert.ToChar(255); readonly Char DO = Convert.ToChar(253); readonly Char DONT = Convert.ToChar(254); readonly Char WILL = Convert.ToChar(251); readonly Char WONT = Convert.ToChar(252); readonly Char SB = Convert.ToChar(250); readonly Char SE = Convert.ToChar(240); const Char IS = '0'; const Char SEND = '1'; const Char INFO = '2'; const Char VAR = '0'; const Char VALUE = '1'; const Char ESC = '2'; const Char USERVAR = '3'; private ArrayList m_ListOptions = new ArrayList(); enum Verbs { WILL = 251, WONT = 252, DO = 253, DONT = 254, IAC = 255 } enum Options { RD = 1, SGA = 3 } private Socket sock = null; // 定義全局變量,方便調(diào)用 //定義事件 private static ManualResetEvent connectDone = new ManualResetEvent(false); private static ManualResetEvent receiveDone = new ManualResetEvent(false); private static ManualResetEvent sendDone = new ManualResetEvent(false); // private static ManualResetEvent waitDone = new ManualResetEvent(false); // private string str_data = "";// 接受到的數(shù)據(jù) private StringBuilder str_data = new StringBuilder();// 所有收到的數(shù)據(jù) private StringBuilder Str_Temp = new StringBuilder();//單個(gè)命令的執(zhí)行結(jié)果,不完整實(shí)際沒有用 public bool Connected //連接狀態(tài) { get { if (sock != null) { return sock.Connected; } else { return false; } } // get return = sock.Connected; // set; } public string RecData //取得執(zhí)行的log { //get //{ return str_data; //} get { return str_data.ToString(); } } public string TempData //取得每個(gè)命令執(zhí)行的log,有問題,沒有用 { //get //{ return str_data; //} get { return Str_Temp.ToString(); } } #endregion public Telnet(string IP) : this(IP, 23) { } //構(gòu)造函數(shù)鏈 public Telnet(string IP, int port)// 工作主函數(shù) { //IPAddress ipAddress = IPAddress.Parse("192.168.56.2"); IPAddress ipAddress = IPAddress.Parse(IP); IPEndPoint remoteEP = new IPEndPoint(ipAddress, port); sock = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp); Console.WriteLine("connectDone.Rese : 設(shè)置為無信號(hào)開始連接"); connectDone.Reset(); sock.BeginConnect(remoteEP, new AsyncCallback(ConnectCallback), sock);// 異步連接 connectDone.WaitOne(10000, false);//異步阻斷,連接成功就進(jìn)入下異步,加入連接超時(shí)間10S, if (!connectDone.WaitOne(10000, false)) { sock.Close();//連接失敗,超時(shí)退出; Console.WriteLine("連接失敗,超時(shí)退出"); } else // 如果連接成功,開始接受數(shù)據(jù)。 { //開始異步接受的線程 Thread threadread = new Thread(new ThreadStart(Receive)); threadread.Start(); } } /// /// 用戶名密碼默認(rèn)回顯為“:” /// /// /// /// /// public Telnet(string IP, int port,string username,string passwd)// 工作主函數(shù) :this(IP, port, username, passwd, ":", ":") //這里的兩個(gè)冒號(hào)是登錄連接設(shè)備的回顯, { } public Telnet(string IP, int port, string username, string passwd,string usernamewait,string passwdwait)// 工作主函數(shù) { //IPAddress ipAddress = IPAddress.Parse("192.168.56.2"); IPAddress ipAddress = IPAddress.Parse(IP); IPEndPoint remoteEP = new IPEndPoint(ipAddress, port); sock = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp); Console.WriteLine("connectDone.Rese : 設(shè)置為無信號(hào)開始連接"); connectDone.Reset(); sock.BeginConnect(remoteEP, new AsyncCallback(ConnectCallback), sock);// 異步連接 connectDone.WaitOne(10000, false); if (!connectDone.WaitOne(10000, false)) { sock.Close();//連接失敗,超時(shí)退出; Console.WriteLine("連接失敗,超時(shí)退出"); } else // 如果連接成功,開始接受數(shù)據(jù)。 { //開始異步接受的線程 Thread threadread = new Thread(new ThreadStart(Receive)); threadread.Start(); Send(usernamewait, username "\r"); //自動(dòng)連接配置,可以手動(dòng) Send(passwdwait, passwd "\r"); } } public Telnet(string IP, int port,int timeout)// 工作主函數(shù) { //IPAddress ipAddress = IPAddress.Parse("192.168.56.2"); IPAddress ipAddress = IPAddress.Parse(IP); IPEndPoint remoteEP = new IPEndPoint(ipAddress, port); sock = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp); Console.WriteLine("connectDone.Rese : 設(shè)置為無信號(hào)開始連接"); connectDone.Reset(); sock.BeginConnect(remoteEP, new AsyncCallback(ConnectCallback), sock);// 異步連接 connectDone.WaitOne(timeout*1000, false); if (!connectDone.WaitOne(timeout * 1000, false)) { sock.Close();//連接失敗,超時(shí)退出; Console.WriteLine("連接失敗,超時(shí)退出"); } else // 如果連接成功,開始接受數(shù)據(jù)。 { //開始異步接受的線程 Thread threadread = new Thread(new ThreadStart(Receive)); threadread.Start(); } } /// /// 異步連接,異步回調(diào) /// /// private void ConnectCallback(IAsyncResult ar) { try { // Retrieve the socket from the state object. Socket client = (Socket)ar.AsyncState; // Complete the connection. client.EndConnect(ar); //Console.WriteLine("Socket connected to {0}", // client.RemoteEndPoint.ToString()); //// Signal that the connection has been made. Console.WriteLine(" connectDone.Set:設(shè)置為有信號(hào)"); connectDone.Set(); // 這里是把連接的事件作為 有信號(hào),主函數(shù)中的阻斷會(huì)繼續(xù)執(zhí)行,否則10s超時(shí)了 } catch (Exception e) { Console.WriteLine("異步連接:" e.ToString()); } } //這里是接受數(shù)據(jù)的方法 private void Receive() { try { // receiveDone.Reset(); //接受沒有用阻斷,這里用的是循環(huán)接受,是在回調(diào)中實(shí)現(xiàn) // Create the state object. // 這里是查了msdn文檔,有這個(gè),主要進(jìn)是傳遞 這個(gè)輔助類中主要是傳遞 socke 與 state.buffer StateObject state = new StateObject(); state.workSocket = sock; // Begin receiving the data from the remote device. sock.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallback), state); // receiveDone.WaitOne(10000,false); //string ddd = string.Empty; } catch (Exception e) { Console.WriteLine(e.ToString()); } } //接受數(shù)據(jù)異步回調(diào) private void ReceiveCallback(IAsyncResult ar) { try { StateObject state = (StateObject)ar.AsyncState; Socket client = state.workSocket; //取得 sock 其實(shí)沒必要,因?yàn)閟ocket 是全局的變量 int bytesRead = client.EndReceive(ar); if (bytesRead > 0) { ////創(chuàng)建一個(gè)與接受 數(shù)據(jù)大小一致的 byte[] Byte[] mToProcess = new Byte[bytesRead]; Array.Copy(state.buffer, mToProcess, bytesRead);//數(shù)組復(fù)制 Str_Temp = ProcessOptions(mToProcess);//清洗掉命令碼,取得回顯的實(shí)際數(shù)據(jù) // Console.WriteLine("*********************************************"); Console.Write(Str_Temp);//回顯本次的數(shù)據(jù) // Console.WriteLine("*********************************************"); // response = mOutText; str_data.Append(Str_Temp); //所有的數(shù)據(jù),把每次的接受的數(shù)據(jù)添加到 str_data.用于后期保存log string temps = str_data.ToString();//這里是測試每次取得數(shù)據(jù)的斷點(diǎn),懶得刪 client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallback), state); //下面進(jìn)是循環(huán)取得數(shù)據(jù)的關(guān)鍵,每次接受完數(shù)據(jù),又開始新的接受數(shù)據(jù)。 } else { //這里是接受數(shù)據(jù)為0,正常異步接受到的數(shù)據(jù)為0時(shí)應(yīng)該沒有數(shù)據(jù)阻斷狀態(tài),不會(huì)到這個(gè)地方。 Console.WriteLine("接受數(shù)據(jù)小于0:" "發(fā)生異常"); client.Close();//關(guān)閉連接 } } catch (Exception e) { Console.WriteLine("接受數(shù)據(jù)異常" e.ToString());//這里是抓取異常的代碼。一般telnet結(jié)束時(shí),異步還處理阻斷狀態(tài),有時(shí)會(huì)發(fā)送,沒啥用。 } } // 發(fā)送命令的方法,這里的data 是沒有 \r 回車的, public void Send(String data) { try { byte[] byteData = Encoding.ASCII.GetBytes(data); //異步發(fā)送 sock.BeginSend(byteData, 0, byteData.Length, 0, new AsyncCallback(SendCallback), sock); //異步阻斷,發(fā)送完成后繼續(xù)執(zhí)行,未完成進(jìn)一只阻斷,可以加超時(shí)時(shí)間 sendDone.WaitOne(10000,false); sendDone.WaitOne(); } catch { Console.WriteLine("出現(xiàn)異常:{0}", data "\r\n"); } finally { //client.Shutdown(SocketShutdown); } } public void Send(char data) { char[] char_data = { data }; try { byte[] byteData = Encoding.ASCII.GetBytes(char_data); sock.BeginSend(byteData, 0, byteData.Length, 0, new AsyncCallback(SendCallback), sock); sendDone.WaitOne(); } catch { Console.WriteLine("出現(xiàn)異常:{0}", data "\r\n"); } finally { //client.Shutdown(SocketShutdown); } } public void Send(char[] data) { try { byte[] byteData = Encoding.ASCII.GetBytes(data); sock.BeginSend(byteData, 0, byteData.Length, 0, new AsyncCallback(SendCallback), sock); sendDone.WaitOne(); } catch { Console.WriteLine("出現(xiàn)異常:{0}", data "\r\n"); } finally { //client.Shutdown(SocketShutdown); } } public void Send(byte[] byteData) { try { //byte[] byteData = Encoding.ASCII.GetBytes(data); sock.BeginSend(byteData, 0, byteData.Length, 0, new AsyncCallback(SendCallback), sock); sendDone.WaitOne(); } catch { Console.WriteLine("發(fā)送出現(xiàn)異常bytedata:{0}", byteData.ToString() "\r\n"); } finally { //client.Shutdown(SocketShutdown); } } /// /// 執(zhí)行命令的方法,判斷回顯字符,執(zhí)行命令 /// /// 回顯字符串判斷 /// 判定成功執(zhí)行的命令 /// 執(zhí)行命令的延遲 public void Send(string expect, string command, int delay_time) { // string local_data = string.Empty ; int i = 0; while (true) { //這里主要是判斷 str_data 中的數(shù)據(jù),接受的數(shù)據(jù)會(huì)持續(xù)寫入到 這個(gè)中,接受可能有延遲,加循環(huán)判斷, if (str_data.ToString().TrimEnd().EndsWith(expect)) { Thread.Sleep(delay_time);//執(zhí)行命令的延遲 Send(command); break; } else {// 增加命令執(zhí)行代碼判斷失敗的懲罰值,8次后,當(dāng)前接受的數(shù)據(jù)中還沒有出現(xiàn)判斷的字符串,socket關(guān)閉 i ; } Thread.Sleep(1000);// 如果當(dāng)前的接受數(shù)據(jù)中沒有判斷的字符出現(xiàn),等待1s后再次判斷 if(i==8) { //Console.WriteLine("命令執(zhí)行錯(cuò)誤,錯(cuò)誤命令" command "命令執(zhí)行結(jié)果:");// str_data.ToString()); //Console.WriteLine(); // Console.WriteLine("命令執(zhí)行錯(cuò)誤,錯(cuò)誤命令" command "命令執(zhí)行結(jié)果:" str_data.ToString()); // str_data.Append("異常終結(jié)"); sock.Close (); break; } // } } } /// /// 執(zhí)行命令的方法,默認(rèn)延遲500ms。 /// /// 回顯字符串判斷 /// 判定成功執(zhí)行的命令 public void Send(string expect, string command) { Send(expect, command, 100); } private static void SendCallback(IAsyncResult ar) {//異步發(fā)送數(shù)據(jù) try { Socket client = (Socket)ar.AsyncState; int bytesSent = client.EndSend(ar); sendDone.Set(); } catch (Exception e) { Console.WriteLine(e.ToString()); } } /// /// 清洗命令碼。telnet的核心代碼 /// /// /// private StringBuilder ProcessOptions(byte[] resbyte) {//網(wǎng)絡(luò)所得 string m_DISPLAYTEXT = ""; string m_strTemp = ""; //string m_strOption = ""; //string m_strNormalText = ""; StringBuilder m_strNormalText = new StringBuilder(); bool bScanDone = false; int ndx = 0; int ldx = 0; char ch, canshu; try { //把數(shù)據(jù)從byte[] 轉(zhuǎn)化成string for (int i = 0; i < resbyte.Length; i ) { Char ss = Convert.ToChar(resbyte[i]); m_strTemp = m_strTemp Convert.ToString(ss); } //此處意義為,當(dāng)沒描完數(shù)據(jù)前,執(zhí)行掃描 while (bScanDone != true) { //獲得長度 int lensmk = m_strTemp.Length; //之后開始分析指令,因?yàn)槊織l指令為255 開頭,故可以用此來區(qū)分出每條指令 ndx = m_strTemp.IndexOf(Convert.ToString(IAC));//首次出現(xiàn)IAC的位置 //此處為出錯(cuò)判斷,本無其他含義 if (ndx > lensmk) ndx = m_strTemp.Length; //此處為,如果搜尋到IAC標(biāo)記的telnet 指令,則執(zhí)行以下步驟 if (ndx != -1) { #region 如果存在IAC標(biāo)志位 // 將 標(biāo)志位IAC 的字符 賦值給最終顯示文字 m_DISPLAYTEXT = m_strTemp.Substring(0, ndx); // 此處獲得命令碼 ch = m_strTemp[ndx 1];//獲取命令碼 canshu = m_strTemp[ndx 2];//獲取命令碼 //如果命令碼是253(DO) 254(DONT) 521(WILL) 252(WONT) 的情況下 if (ch == DO || ch == DONT || ch == WILL || ch == WONT) { //將以IAC 開頭3個(gè)字符組成的整個(gè)命令存儲(chǔ)起來 //m_strOption = m_strTemp.Substring(ndx, 3); //m_ListOptions.Add(m_strOption); // 將 標(biāo)志位IAC 的字符 賦值給最終顯示文字 //m_DISPLAYTEXT = m_strTemp.Substring(0, ndx); //將處理過的字符串刪去 string txt = m_strTemp.Substring(ndx 3); m_strTemp = txt; telnetproceess(ch, canshu); } //如果IAC后面又跟了個(gè)IAC (255) else if (ch == IAC) { //則顯示從輸入的字符串頭開始,到之前的IAC 結(jié)束 //m_DISPLAYTEXT = m_strTemp.Substring(0, ndx); //之后將處理過的字符串排除出去 m_strTemp = m_strTemp.Substring(ndx 1); string xxc = m_strTemp; } //如果IAC后面跟的是SB(250) else if (ch == SB) { //m_DISPLAYTEXT = m_strTemp.Substring(0, ndx); ldx = m_strTemp.IndexOf(Convert.ToString(SE)); // m_strOption = m_strTemp.Substring(ndx, ldx); //m_ListOptions.Add(m_strOption); m_strTemp = m_strTemp.Substring(ldx 1); telnetproceess(ch, canshu); } #endregion } //若字符串里已經(jīng)沒有IAC標(biāo)志位了 else { //顯示信息累加上m_strTemp存儲(chǔ)的字段 m_DISPLAYTEXT = m_DISPLAYTEXT m_strTemp; bScanDone = true; } } //輸出人看到的信息 m_strNormalText.Append(m_DISPLAYTEXT); } catch (Exception eP) { throw new Exception("解析傳入的字符串錯(cuò)誤:" eP.Message); } return m_strNormalText; } //異步委托調(diào)用,這里是協(xié)商代碼,測試很多設(shè)備 占時(shí)沒發(fā)現(xiàn)什么問題 private void telnetproceess(char ch, char canshu) { //如果命令碼是253(DO) 254(DONT) 251(WILL) 252(WONT) 的情況下 if (ch == DO)//253 對(duì)端設(shè)備對(duì)本端設(shè)備的發(fā)出參數(shù)請(qǐng)求(如果支持對(duì)方的,則DO確認(rèn),不支持則DONT) { if (canshu == 32 || canshu == 35 || canshu == 39 || canshu == 36) { //發(fā)送命令碼 byte[] sendcom = new byte[3]; sendcom[0] = 255; sendcom[1] = 252; sendcom[2] = Convert.ToByte(canshu); //tcpSocket.Send(sendcom); //steam.Write(sendcom, 0, sendcom.Length); Send(sendcom); } else if (canshu == 24) { //byte[] sendcom = { 255, 240, 78, 65, 87, 83, 32, 8, 0, 32, 2, 5, 255, 240 }; byte[] sendcom = { 255, 251, 24 }; //steam.Write(sendcom, 0, sendcom.Length); Send(sendcom); } else if (canshu == 31) {/* 255 250 31 window size 255 240 開始 recv SB N78A65W87S83 8(56)0(48) 2(50)5(53) */ byte[] sendcom1 = { 255, 251, 31 };//同意窗口大小請(qǐng)求 Send(sendcom1); //steam.Write(sendcom1, 0, sendcom1.Length); byte[] sendcom = { 255, 250, 31, 0, 80, 0, 25, 255, 240 };//發(fā)送窗口大小 //steam.Write(sendcom, 0, sendcom.Length); Send(sendcom); //byte[] sendcoms = { 255, 250, 31,78, 65,87,83, 32,56,48,32,50,53,255,240}; //byte[] sendcoms = { 255, 250, 31, 78, 65, 87, 83, 255, 240 }; //sock.Send(sendcoms); } else if (canshu == 33) { } else if (canshu == 1 || canshu == 34) { // 255 252 1 echo byte[] sendcom = { 255, 252, Convert.ToByte(canshu) }; //steam.Write(sendcom, 0, sendcom.Length); Send(sendcom); } else { string cuowu = canshu.ToString();// string xx;//拍錯(cuò)用 } } else if (ch == DONT)//254 協(xié)商對(duì)端設(shè)備的參數(shù)(對(duì)端發(fā)出對(duì)本端參數(shù)的請(qǐng)求) { } else if (ch == WILL)//251 本端設(shè)備的參數(shù)(發(fā)送給對(duì)方) { if (canshu == 3) { } else if (canshu == 1 || canshu == 3) { //255 253 echo; byte[] sendcom = { 255, 253, Convert.ToByte(canshu) }; //steam.Write(sendcom, 0, sendcom.Length); Send(sendcom); } else { //255 254 status{ } byte[] sendcom = { 255, 254, Convert.ToByte(canshu) }; //steam.Write(sendcom, 0, sendcom.Length); Send(sendcom); } } else if (ch == WONT)//252 本端設(shè)備的參數(shù)(發(fā)送給對(duì)方) { } else if (ch == 250) { if (canshu == 24) { //byte[] sendcom = { 255, 254, Convert.ToByte(canshu) }; // recv SB 24 0 ANSI byte[] sendcom = { 255, 250, 24, 0, 65, 78, 83, 73, 255, 240 };//發(fā)送終端編碼send SB 24 0 ANSI Send(sendcom); //steam.Write(sendcom, 0, sendcom.Length); // recv SB 65 87 S[Unrecognized] //recv SB NAWS 80 25 // IAC,SB,24,0,'I','B','M','P','C', IAC,SE } } } /// /// 保存log文件.這里個(gè)人需要,操作的就是本次的 str_data /// /// 絕對(duì)文件路徑 public void Save_File(string FilePathName) { string[] Str_dir = FilePathName.Split('\\'); string FilePath_Dir = string.Empty; for (int i = 0; i < Str_dir.Length - 1; i ) { FilePath_Dir = Str_dir[i] "\\"; } string FileName = Str_dir[Str_dir.Length - 1]; Save_File(FilePath_Dir, FileName); } /// /// 判斷文件或目錄是否存在 /// 這是保存到當(dāng)前程序運(yùn)行到目錄以今天日期生成目錄 /// 目錄 /// 文件名稱 private void File_Exists(string FilePath_Dir, string FileName) { string FilePathName = FilePath_Dir "\\" FileName; // string path = FilePath_Dir "\\" DateTime.Now.ToString("yyyy-MM-dd") "\\" FilePathName ".txt"; if (!Directory.Exists(FilePath_Dir)) { Directory.CreateDirectory(FilePath_Dir); } if (!File.Exists(FilePathName)) { FileStream fs = File.Create(FilePathName); fs.Close(); } } /// /// 保存文件 /// /// 文件的目錄 /// 文件的名稱 public void Save_File(string FilePath_Dir, string FileName)//保存文件函數(shù) { string FilePathName = FilePath_Dir "\\" FileName; File_Exists(FilePath_Dir, FileName); StreamWriter steam = new StreamWriter(FilePathName, true, Encoding.UTF8); steam.Write(str_data); steam.Close(); } public void close() { try { sock.Shutdown(SocketShutdown.Both); sock.Close(); } catch { } } //輔助類傳遞 接收數(shù)據(jù),與socket public class StateObject { // Client socket. public Socket workSocket = null; // Size of receive buffer. public const int BufferSize = 1024; // Receive buffer. public byte[] buffer = new byte[BufferSize]; // Received data string. public StringBuilder sb = new StringBuilder(); public string waitfor = string.Empty; } } }來源:https://www./content-1-463551.html |
|