亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何解析txt文件中的netstat輸出并根據PID獲取所有遠程IP地址作為變量?

如何解析txt文件中的netstat輸出并根據PID獲取所有遠程IP地址作為變量?

PHP
慕標琳琳 2024-01-20 15:39:29
背景我嘗試編碼所需的桌面 WPF 應用程序允許我選擇當前在本地計算機上運行的進程/應用程序,并獲取應用程序名稱和 PID 并將其存儲在變量中。每次用戶在選擇進程后單擊按鈕后加載應用程序時,netstat -ano > C:\test.txt都會執行 a。問題有沒有辦法對其進行編碼,以便可以將之前獲得的應用程序名稱和 PID 與文件中匹配的 PID 行進行比較test.txt,然后將遠程 IP 地址存儲到變量或數組中?謝謝。
查看完整描述

1 回答

?
翻過高山走不出你

TA貢獻1875條經驗 獲得超3個贊

為什么要有一個文本文件?這是我解決問題的方法,使用 DataTable 存儲數據并收集數據:


        //Create the process

        using (Process ns = new Process())

        {


            DataTable dt = new DataTable();

            dt.Columns.AddRange(new DataColumn[] {

                    new DataColumn("Protocol"),

                    new DataColumn("Local Address"),

                    new DataColumn("Foreign Address"),

                    new DataColumn("State"),

                    new DataColumn("PID"),

                    new DataColumn("Process Name"),

                });


            ProcessStartInfo psi = new ProcessStartInfo("netstat.exe", "-ano");

            psi.RedirectStandardOutput = true;

            psi.UseShellExecute = false;

            ns.StartInfo = psi;

            // Run it, and read the results

            ns.Start();

            using (StreamReader r = ns.StandardOutput)

            {

                string output = r.ReadToEnd();

                ns.WaitForExit();


                //Parse those results into a DataTable, polling the Process info

                string[] lines = output.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);



                foreach (string line in lines)

                {

                    string[] elements = line.Split(' ');

                    if (elements.Length < 5) continue;

                    if (elements.Contains("Proto")) continue;


                    DataRow dr = dt.NewRow();


                    List<string> validElements = new List<string>();


                    //Weed out empty elements.

                    foreach (string element in elements)

                    {

                        //skip blanks

                        if (element.Trim() == "") continue;

                        validElements.Add(element);

                    }


                    foreach (string element in validElements)

                    {


                        foreach (DataColumn dc in dt.Columns)

                        {

                            // fill in the buckets. Note that UDP doesn't have a state

                            if (dr["Protocol"].ToString() == "UDP" && dc.ColumnName == "State") continue;


                            if (dr[dc] == DBNull.Value)

                            {

                                dr[dc] = element;

                                break;

                            }

                        }

                    }


                    dr["Process Name"] = Process.GetProcessById(int.Parse(dr["PID"].ToString())).ProcessName;

                    dt.Rows.Add(dr);

                }

            }

        }

這是我的結果數據的簡短屏幕截圖:

https://img1.sycdn.imooc.com/65ab78d30001796506510331.jpg

然后我可以用這些數據在代碼中做任何我想做的事情。至少我會這么做。



查看完整回答
反對 回復 2024-01-20
  • 1 回答
  • 0 關注
  • 160 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號