From d889408119bda95a637ee0aca3217826efb8bf7d Mon Sep 17 00:00:00 2001 From: c Date: Sun, 23 Jul 2023 22:27:36 +0800 Subject: [PATCH] 1 --- NetPanel.Bl/ExecuteBl.cs | 98 ++++++++++++++++++++++ NetPanel.Bl/NetPanel.Bl.csproj | 5 ++ NetPanel/Controllers/DataListController.cs | 6 +- NetPanel/Controllers/HomeController.cs | 7 +- NetPanel/Controllers/LoginController.cs | 23 ++++- NetPanel/NetPanel.csproj | 1 + 6 files changed, 130 insertions(+), 10 deletions(-) create mode 100644 NetPanel.Bl/ExecuteBl.cs diff --git a/NetPanel.Bl/ExecuteBl.cs b/NetPanel.Bl/ExecuteBl.cs new file mode 100644 index 0000000..ae9662f --- /dev/null +++ b/NetPanel.Bl/ExecuteBl.cs @@ -0,0 +1,98 @@ +using NetPanel.Help; +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace NetPanel.Bl +{ + public class ExecuteBl + { + + public static string Exec(string command) + { + + if (OperatingSystem.IsWindows()) // 检查当前操作系统是否为 Windows + { + return ExecuteCommandOnWindows(command); + } + else if (OperatingSystem.IsLinux()) // 检查当前操作系统是否为 Linux + { + return ExecuteCommandOnLinux(command); + } + else + { + return ""; + } + } + + /// + /// win 执行命令 + /// + /// + /// + private static string ExecuteCommandOnWindows(string command) + { + + using var process = new Process + { + StartInfo = new ProcessStartInfo + { + FileName = "cmd.exe", + Arguments = "/C " + command, + RedirectStandardOutput = true, + RedirectStandardError = true, + //StandardErrorEncoding = Encoding.UTF8, + //StandardOutputEncoding = Encoding.UTF8, + UseShellExecute = false, + CreateNoWindow = true + } + }; + process.Start(); + string output = process.StandardOutput.ReadToEnd(); + string error = process.StandardError.ReadToEnd(); + process.WaitForExit(); + if (output.IsNull()) { + return error; + } + return output; + + } + + /// + /// linux 执行命令 + /// + /// + private static string ExecuteCommandOnLinux(string command) + { + + using var process = new Process + { + StartInfo = new ProcessStartInfo + { + FileName = "/bin/bash", + Arguments = "-c \"" + command + "\"", + RedirectStandardOutput = true, + RedirectStandardError = true, + //StandardErrorEncoding = Encoding.UTF8, + //StandardOutputEncoding = Encoding.UTF8, + UseShellExecute = false, + CreateNoWindow = true + } + }; + process.Start(); + string output = process.StandardOutput.ReadToEnd(); + string error = process.StandardError.ReadToEnd(); + process.WaitForExit(); + if (output.IsNull()) + { + return error; + } + return output; + + } + + } +} diff --git a/NetPanel.Bl/NetPanel.Bl.csproj b/NetPanel.Bl/NetPanel.Bl.csproj index 132c02c..9da64cd 100644 --- a/NetPanel.Bl/NetPanel.Bl.csproj +++ b/NetPanel.Bl/NetPanel.Bl.csproj @@ -6,4 +6,9 @@ enable + + + + + diff --git a/NetPanel/Controllers/DataListController.cs b/NetPanel/Controllers/DataListController.cs index 81dcc4c..b305818 100644 --- a/NetPanel/Controllers/DataListController.cs +++ b/NetPanel/Controllers/DataListController.cs @@ -1,6 +1,4 @@ - - -using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json.Linq; using System; @@ -8,7 +6,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; -namespace btApiOrWeb.Controllers +namespace NetPanel.Controllers { public class DataListController : Controller { diff --git a/NetPanel/Controllers/HomeController.cs b/NetPanel/Controllers/HomeController.cs index 6e83d92..b13583c 100644 --- a/NetPanel/Controllers/HomeController.cs +++ b/NetPanel/Controllers/HomeController.cs @@ -1,5 +1,4 @@ - -using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; using NetPanel.Entity; using System; @@ -8,7 +7,7 @@ using System.Diagnostics; using System.Linq; using System.Threading.Tasks; -namespace btApiOrWeb.Controllers +namespace NetPanel.Controllers { public class HomeController : Controller { @@ -40,7 +39,7 @@ namespace btApiOrWeb.Controllers { return View(); } - + } diff --git a/NetPanel/Controllers/LoginController.cs b/NetPanel/Controllers/LoginController.cs index 4997fc0..2f3e369 100644 --- a/NetPanel/Controllers/LoginController.cs +++ b/NetPanel/Controllers/LoginController.cs @@ -10,8 +10,11 @@ using System.Linq; using System.Threading.Tasks; using NetPanel.Help; using Newtonsoft.Json; +using System.Diagnostics; +using System.Text; +using NetPanel.Bl; -namespace btApiOrWeb.Controllers +namespace NetPanel.Controllers { public class LoginController : MyController { @@ -21,9 +24,25 @@ namespace btApiOrWeb.Controllers } - + public IActionResult GetUser() { + + + string output = ExecuteBl.Exec("ping www.baidu.com"); + + + // 解析输出行 + string[] lines = output.Split('\n'); + string[] memoryInfo = lines[1].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); + + // 获取已使用和总内存大小 + float totalMemory = float.Parse(memoryInfo[1]); + float usedMemory = float.Parse(memoryInfo[2]); + + // 计算使用率 + float usagePercentage = (usedMemory / totalMemory) * 100; + ReturnMsg enReturnMsg = new ReturnMsg(); enReturnMsg.Code = 0; enReturnMsg.Msg = "失败!"; diff --git a/NetPanel/NetPanel.csproj b/NetPanel/NetPanel.csproj index 7681c22..e239f4d 100644 --- a/NetPanel/NetPanel.csproj +++ b/NetPanel/NetPanel.csproj @@ -7,6 +7,7 @@ +