//************************************************************************ // author: yuzhengyang // date: 2018.4.27 - 2018.4.27 // desc: CMD 网络工具 // Copyright (c) yuzhengyang. All rights reserved. //************************************************************************ using Azylee.Core.DataUtils.CollectionUtils; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; namespace Azylee.Core.WindowsUtils.CMDUtils { public class CMDNetstatTool { /// /// 根据端口号查询列表,过滤pid0(item1:端口、item2:pid) /// /// 端口号 /// 模糊匹配 /// public static List> FindByPort(int port, bool fuzzy = true) { var list = Find(port.ToString()); if (ListTool.HasElements(list)) { try { if (fuzzy) { return list.Where(x => x.Item1.ToString().Contains(port.ToString()) && x.Item2 != 0).ToList(); } else { return list.Where(x => x.Item1 == port && x.Item2 != 0).ToList(); } } catch { } } return null; } /// /// 查询列表(item1:端口、item2:pid) /// /// 查询内容 /// public static List> Find(string content) { List> result = null; var list = CMDProcessTool.Execute($"netstat -ano|findstr \"{content}\""); if (ListTool.HasElements(list)) { result = new List>(); foreach (var item in list) { if (!string.IsNullOrWhiteSpace(item) && (item.StartsWith("TCP") || item.StartsWith("UDP"))) { try { Regex regex = new Regex(@"\s+"); string[] block = regex.Split(item); if (ListTool.HasElements(block) && block.Length >= 3) { string[] s = block[1].Split(':'); if (ListTool.HasElements(s) && s.Length >= 2) { int _port = int.Parse(s[s.Length - 1]); int _pid = int.Parse(block[block.Length - 1]); if (!result.Any(x => x.Item1 == _port && x.Item2 == _pid)) { Tuple _tuple = new Tuple(_port, _pid); result.Add(_tuple); } } } } catch { } } } } return result; } /// /// 随机获取可用的端口号 /// /// 需要的端口号个数 /// 起始端口 /// public static List GetAvailablePorts(byte count, int start = 52800) { if (count > 0) { List ports = new List(); int startPort = start; List> list = Find(":"); for (var i = 0; i < count; i++) { if (!Ls.Ok(list) || !list.Any(x => x.Item1 == startPort)) { ports.Add(startPort); startPort++; } if (startPort > 65535 || ports.Count() >= count) return ports; } } return null; } } }