LOGO OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 开发文档 其他文档  
 
网站管理员

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

admin
2021年2月2日 15:24 本文热度 2961

创建端口转发

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; }

该文章在 2021/2/2 15:24:41 编辑过
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2024 ClickSun All Rights Reserved