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>
|
<Nullable>enable</Nullable>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\NetPanel.Entity\NetPanel.Entity.csproj" />
|
||||||
|
<ProjectReference Include="..\NetPanel.Help\NetPanel.Help.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@@ -1,6 +1,4 @@
|
|||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
using Microsoft.AspNetCore.Mvc;
|
|
||||||
using Newtonsoft.Json.Linq;
|
using Newtonsoft.Json.Linq;
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
@@ -8,7 +6,7 @@ using System.Collections.Generic;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace btApiOrWeb.Controllers
|
namespace NetPanel.Controllers
|
||||||
{
|
{
|
||||||
public class DataListController : Controller
|
public class DataListController : Controller
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using NetPanel.Entity;
|
using NetPanel.Entity;
|
||||||
using System;
|
using System;
|
||||||
@@ -8,7 +7,7 @@ using System.Diagnostics;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace btApiOrWeb.Controllers
|
namespace NetPanel.Controllers
|
||||||
{
|
{
|
||||||
public class HomeController : Controller
|
public class HomeController : Controller
|
||||||
{
|
{
|
||||||
@@ -40,7 +39,7 @@ namespace btApiOrWeb.Controllers
|
|||||||
{
|
{
|
||||||
return View();
|
return View();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,8 +10,11 @@ using System.Linq;
|
|||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using NetPanel.Help;
|
using NetPanel.Help;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
|
using System.Diagnostics;
|
||||||
|
using System.Text;
|
||||||
|
using NetPanel.Bl;
|
||||||
|
|
||||||
namespace btApiOrWeb.Controllers
|
namespace NetPanel.Controllers
|
||||||
{
|
{
|
||||||
public class LoginController : MyController
|
public class LoginController : MyController
|
||||||
{
|
{
|
||||||
@@ -21,9 +24,25 @@ namespace btApiOrWeb.Controllers
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public IActionResult GetUser()
|
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();
|
ReturnMsg enReturnMsg = new ReturnMsg();
|
||||||
enReturnMsg.Code = 0;
|
enReturnMsg.Code = 0;
|
||||||
enReturnMsg.Msg = "失败!";
|
enReturnMsg.Msg = "失败!";
|
||||||
|
|||||||
@@ -7,6 +7,7 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\NetPanel.Bl\NetPanel.Bl.csproj" />
|
||||||
<ProjectReference Include="..\NetPanel.Entity\NetPanel.Entity.csproj" />
|
<ProjectReference Include="..\NetPanel.Entity\NetPanel.Entity.csproj" />
|
||||||
<ProjectReference Include="..\NetPanel.Help\NetPanel.Help.csproj" />
|
<ProjectReference Include="..\NetPanel.Help\NetPanel.Help.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|||||||
Reference in New Issue
Block a user