CMDNetstatTool.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. //************************************************************************
  2. // author: yuzhengyang
  3. // date: 2018.4.27 - 2018.4.27
  4. // desc: CMD 网络工具
  5. // Copyright (c) yuzhengyang. All rights reserved.
  6. //************************************************************************
  7. using Azylee.Core.DataUtils.CollectionUtils;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Text.RegularExpressions;
  13. namespace Azylee.Core.WindowsUtils.CMDUtils
  14. {
  15. public class CMDNetstatTool
  16. {
  17. /// <summary>
  18. /// 根据端口号查询列表,过滤pid0(item1:端口、item2:pid)
  19. /// </summary>
  20. /// <param name="port">端口号</param>
  21. /// <param name="fuzzy">模糊匹配</param>
  22. /// <returns></returns>
  23. public static List<Tuple<int, int>> FindByPort(int port, bool fuzzy = true)
  24. {
  25. var list = Find(port.ToString());
  26. if (ListTool.HasElements(list))
  27. {
  28. try
  29. {
  30. if (fuzzy)
  31. {
  32. return list.Where(x => x.Item1.ToString().Contains(port.ToString()) && x.Item2 != 0).ToList();
  33. }
  34. else
  35. {
  36. return list.Where(x => x.Item1 == port && x.Item2 != 0).ToList();
  37. }
  38. }
  39. catch { }
  40. }
  41. return null;
  42. }
  43. /// <summary>
  44. /// 查询列表(item1:端口、item2:pid)
  45. /// </summary>
  46. /// <param name="content">查询内容</param>
  47. /// <returns></returns>
  48. public static List<Tuple<int, int>> Find(string content)
  49. {
  50. List<Tuple<int, int>> result = null;
  51. var list = CMDProcessTool.Execute($"netstat -ano|findstr \"{content}\"");
  52. if (ListTool.HasElements(list))
  53. {
  54. result = new List<Tuple<int, int>>();
  55. foreach (var item in list)
  56. {
  57. if (!string.IsNullOrWhiteSpace(item) &&
  58. (item.StartsWith("TCP") || item.StartsWith("UDP")))
  59. {
  60. try
  61. {
  62. Regex regex = new Regex(@"\s+");
  63. string[] block = regex.Split(item);
  64. if (ListTool.HasElements(block) && block.Length >= 3)
  65. {
  66. string[] s = block[1].Split(':');
  67. if (ListTool.HasElements(s) && s.Length >= 2)
  68. {
  69. int _port = int.Parse(s[s.Length - 1]);
  70. int _pid = int.Parse(block[block.Length - 1]);
  71. if (!result.Any(x => x.Item1 == _port && x.Item2 == _pid))
  72. {
  73. Tuple<int, int> _tuple = new Tuple<int, int>(_port, _pid);
  74. result.Add(_tuple);
  75. }
  76. }
  77. }
  78. }
  79. catch { }
  80. }
  81. }
  82. }
  83. return result;
  84. }
  85. /// <summary>
  86. /// 随机获取可用的端口号
  87. /// </summary>
  88. /// <param name="count">需要的端口号个数</param>
  89. /// <param name="start">起始端口</param>
  90. /// <returns></returns>
  91. public static List<int> GetAvailablePorts(byte count, int start = 52800)
  92. {
  93. if (count > 0)
  94. {
  95. List<int> ports = new List<int>();
  96. int startPort = start;
  97. List<Tuple<int, int>> list = Find(":");
  98. for (var i = 0; i < count; i++)
  99. {
  100. if (!Ls.Ok(list) || !list.Any(x => x.Item1 == startPort))
  101. {
  102. ports.Add(startPort);
  103. startPort++;
  104. }
  105. if (startPort > 65535 || ports.Count() >= count) return ports;
  106. }
  107. }
  108. return null;
  109. }
  110. }
  111. }