利用C#代码创建、查看、删除端口转发

来源:互联网 发布:金山数据恢复软件多大? 编辑:程序博客网 时间:2024/05/23 19:20

创建端口转发

   private static bool CreateChannle(out string msg)        {            msg = "";            Random random = new Random();            int port = 0;            int times = 0;            do            {                port = random.Next(1000, 50000);                times++;            } while (!CheckPortAvailable(port) && times < 10);            if (times >= 10)            {                msg = "无可用端口";                return false;            }            string cmd = string.Format("netsh interface portproxy add v4tov4 listenport={0} connectaddress=dtdl.channel.lebaoba.com connectport=11808",port);            string r = ExecuteCmd(cmd);            string result = r.Substring(r.IndexOf("exit") + 4);            if (result.Replace("\r\n", "").Length == 0)            {                msg = "127.0.0.1:" + port.ToString();                return true;            }            else            {                msg = result;                return false;            }        }

检查端口是否可用

private static bool CheckPortAvailable(int port)        {            string cmd = string.Format("netstat -ano | findstr \"{0}\"", port);            string r = ExecuteCmd(cmd);            string result = r.Substring(r.IndexOf("exit") + 4);            if (result.Replace("\r\n", "").Length == 0)            {                return true;            }            else            {                return false;            }        }

查看创建的所有转发通道

private static List<string> SearchChannle()        {            List<string> list = new List<string>();            string cmd = "netsh interface portproxy show v4tov4";            string r = ExecuteCmd(cmd);            r = r.Substring(r.IndexOf("exit") + 4);            string[] lines = r.Replace("\r\n", "^").Split('^');            for (int i = 6; i < lines.Length; i++)            {                if (!string.IsNullOrEmpty(lines[i]))                {                    string channelString = "";                   string[] temp = lines[i].Split(' ');                    foreach (var item in temp)                    {                        if (!string.IsNullOrEmpty(item))                        {                            channelString += item + "|";                        }                    }                    list.Add(channelString);                }            }            return list;        }

删除一条转发通道

private static void DeleteChannel(string ip,string port)        {            string cmd = "";            if (ip == "*")            {                cmd = string.Format("netsh interface portproxy delete v4tov4 listenaddress={0} listenport={1}", ip, port);            }            else            {                cmd = string.Format("netsh interface portproxy delete v4tov4 listenport={0}", port);            }            ExecuteCmd(cmd);        }

删除所有转发通道

private static void DeleteAllChannels()        {            List<string> channcelList = SearchChannle();            foreach (var channcel in channcelList)            {                DeleteChannel(channcel.Split('|')[0], channcel.Split('|')[1]);            }        }

执行cmd指令的方法

private static string ExecuteCmd(string strInput)        {            Process p = new Process();            //设置要启动的应用程序            p.StartInfo.FileName = "cmd.exe";            //是否使用操作系统shell启动            p.StartInfo.UseShellExecute = false;            // 接受来自调用程序的输入信息            p.StartInfo.RedirectStandardInput = true;            //输出信息            p.StartInfo.RedirectStandardOutput = true;            // 输出错误            p.StartInfo.RedirectStandardError = true;            //不显示程序窗口            p.StartInfo.CreateNoWindow = true;            //启动程序            p.Start();            //向cmd窗口发送输入信息            p.StandardInput.WriteLine(strInput + "&exit");            p.StandardInput.AutoFlush = true;            //获取输出信息            string strOuput = p.StandardOutput.ReadToEnd();            //等待程序执行完退出进程            p.WaitForExit();            p.Close();            return strOuput;        }
原创粉丝点击