1
This commit is contained in:
98
NetPanel.Bl/ExecuteBl.cs
Normal file
98
NetPanel.Bl/ExecuteBl.cs
Normal file
@@ -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 "";
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// win 执行命令
|
||||
/// </summary>
|
||||
/// <param name="command"></param>
|
||||
/// <returns></returns>
|
||||
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;
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// linux 执行命令
|
||||
/// </summary>
|
||||
/// <param name="command"></param>
|
||||
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;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -6,4 +6,9 @@
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\NetPanel.Entity\NetPanel.Entity.csproj" />
|
||||
<ProjectReference Include="..\NetPanel.Help\NetPanel.Help.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -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
|
||||
{
|
||||
|
||||
@@ -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
|
||||
{
|
||||
|
||||
@@ -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
|
||||
{
|
||||
@@ -24,6 +27,22 @@ 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 = "失败!";
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\NetPanel.Bl\NetPanel.Bl.csproj" />
|
||||
<ProjectReference Include="..\NetPanel.Entity\NetPanel.Entity.csproj" />
|
||||
<ProjectReference Include="..\NetPanel.Help\NetPanel.Help.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
Reference in New Issue
Block a user